Problems with umlauts in CRMrestkit

May 8, 2013 at 9:48 AM
Hello there,

first at all I would like to say 'Thank you' for your great tool! It helps so much!
But now we have got a problem and we localized it: CRMrestkit has probably problems to work with special german characters like 'ß', 'ä', 'ü', 'ö'. We would like to filter our records with CRMRestkit function 'ByQuery' and give an alert about how much records the system found.
 function getVBCount() {
 //get values from Kundenanmeldung
 var varPLZ = Xrm.Page.getAttribute('prefix_postalcode').getValue();
 var varCity = Xrm.Page.getAttribute('prefix_city').getValue();
 var varStreet = Xrm.Page.getAttribute('prefix_street').getValue();
 var varHousenr = Xrm.Page.getAttribute('prefix_house_number').getValue();
 var varVBFound = Xrm.Page.getAttribute('prefix_consumption_point_not_found').getValue();
 var varAnzahl = 0;
 
 //filter for accounts
 var varfilter = "(prefix_zip_code eq '"+varPLZ +"' and prefix_city eq '"+varCity +"' and prefix_streetname eq '"+varStreet +"' and prefix_houseno eq '"+varHousenr +"'+)";
 
 if (varVBFound == false)
 {
 
 CrmRestKit.ByQuery( 'prefix_consumption_point', ['prefix_name'], varfilter, false ).then( function fnSuccess( data ) 
 
 {
    //alert(data.d.results.length);
    varAnzahl = data.d.results.length;
    
    if (varAnzahl > 0)
    {
        Xrm.Page.getAttribute("prefix_consumption_pointid").setRequiredLevel("required");
        alert('Anzahl der gefundenen Kunden: '+ varAnzahl +'')
    }
    else
    {
        Xrm.Page.getAttribute("prefix_consumption_pointid").setRequiredLevel("none");
    }
 })
 
 }
 else
 {
        Xrm.Page.getAttribute("prefix_consumption_pointid").setRequiredLevel("none");
 }
  
 }
 
This function is working without any issues, if there are no special characters in the attributes. But when a record contains a special character, e.g. "Pforzheimer Stra__ß__e" in field street, it does not work. If you put it in without a special character ("Pforzheimer Strasse", not the proper german way) it works.
Could you help us out?

Many greetings from Germany
Johannes
Coordinator
May 11, 2013 at 5:40 PM
Hello Johannes,

sorry for my late response. I was busy the last days (Father's Day...). I will perform some tests next week!

In case you need an urgend fix, try to encode the string with the following function:
function xmlEncode(strInput) {

        var c,
            encoded = '';

        if (strInput === null) {
            return null;
        }
        if (strInput === '') {
            return '';
        }

        for (var cnt = 0, max = strInput.length; cnt < max; cnt++) {

            c = strInput.charCodeAt(cnt);

            if (((c > 96) && (c < 123))
                || ((c > 64) && (c < 91))
                || (c === 32)
                || ((c > 47) && (c < 58))
                || (c === 46)
                || (c === 44)
                || (c === 45)
                || (c === 95)) {
                encoded = encoded + String.fromCharCode(c);
            }
            else {
                encoded = encoded + '&#' + c + ';';
            }
        }

        return encoded;
    }
Have a nice weekend


Greating form Hameln /Germany

Daniel
Coordinator
May 13, 2013 at 8:49 AM
Hello Johannes,

ok fixed, the problem was caused by not using "encodeURIComponent".

With the latests version (2.5.1) the probelm is solved.

Regards,

Daniel
May 13, 2013 at 9:30 AM
Hey Daniel,

thanks for your support! We updated our restkit webressource to version 2.5.1. But now the whole function ByQuery is not working anymore, we don't get any records, even when they don't include characters. We haven't touched the code of our function, it's exactly the same and it worked with 2.5 (except the umlauts ;)).
Could you check that again?

Greetings to Hameln from Stuttgart,
Johannes
Coordinator
May 13, 2013 at 9:40 AM
Hi Johannes,

that is strange, all my unit-tests are green. Your query does not return any records and no error message is thrown?

Could you please execute the unit-tests and see if any error occurs?

Daniel
Coordinator
May 13, 2013 at 10:17 AM
Edited May 13, 2013 at 10:17 AM
Hi again,

I just updated the unit-test to reflect your query:
QUnit.test( 'Retrieve record with muliple condtions and german special characters (sync-mode)', function () {
            
            var ctx = this,
                street = 'foobar_äöüß 12',
                zipCode = '31840',
                city = 'Hessisch Oldendorf',
                houseNumber = '34',
                columns = ['Name', 'Address1_Line1', 'Address1_City'],
                filter = "(Address1_PostalCode eq '" + zipCode
                        + "' and Address1_City eq '" + city
                        + "' and Address1_Line1 eq '" + street
                        + "' and Address1_Line2 eq '" + houseNumber + "')",
                account = {
                    Name: 'John Doe',
                    Address1_Line1: street,
                    Address1_Line2: houseNumber,
                    Address1_PostalCode: zipCode,
                    Address1_City: city
                };

            // arrange - create a create a record that contains germany special characters
            CrmRestKit.Create( 'Account', account, false ).then( function ( data ) {
                // save the id for the teardown method
                ctx.createAccountsSet.push( data.d.AccountId );

            }, onRestError );

            // action - load the just created record byQuery
            CrmRestKit.ByQuery( 'Account', columns, filter, false ).then( function ( data ) {

                // assert - found on record
                QUnit.equal( data.d.results.length, 1, 'Expected one, found:', data.d.results.length );

            }, onRestError );
        } );
And I received an syntax error with the original filter:
filter = "(Address1_PostalCode eq '" + zipCode
                        + "' and Address1_City eq '" + city
                        + "' and Address1_Line1 eq '" + street
                        + "' and Address1_Line2 eq '" + houseNumber + "'+)",
The very last "+" is not correct, once removed the query works.


Daniel
May 13, 2013 at 11:07 AM
Hi Daniel,

great work! Somehow it worked before the CRMrestkit with the unnecessary '+'. So everything is working fine now, thanks for adding umlaut-support! +1 !

Kind regards,
Johannes
Coordinator
May 13, 2013 at 11:59 AM
You are welcome!