3

I have the following XML code that is filtering my Lookup Field in my Crm Dynamics form. The filter is used following the data that is entered into the Account Field. However, the account field can contain the & symbol, and when it does, an error occurs stating that the XML is not well formed.

Does anyone have any solutions to the problem?

function accountcontact()
{
    Xrm.Page.getControl("new_contactlookup").addPreSearch(function () { addcontactlookup(); });

    function addcontactlookup()
    {
        var accountID = Xrm.Page.getAttribute("new_companylookup");
        var AccountIDObj= accountID.getValue();

        if (AccountIDObj != null)
        {
            var fetchFilter1 = "<filter type='and'><condition attribute='parentcustomerid' uitype='" + AccountIDObj[0].entityType + "' operator='eq' value='" + AccountIDObj[0].id + "' uiname='" + AccountIDObj[0].name + "' /></filter>";
            Xrm.Page.getControl("new_contactlookup").addCustomFilter(fetchFilter1);
        }
    }
}
Leo Bedrosian
  • 3,659
  • 2
  • 18
  • 22
Sjharrison
  • 729
  • 3
  • 16
  • 39
  • 3
    It must be escaped to `&` Use a utility function (I assume JavaScript): [how to escape xml entities in javascript?](http://stackoverflow.com/questions/7918868/how-to-escape-xml-entities-in-javascript) – Alex K. Oct 20 '14 at 17:44
  • Why you would want to add a filter using javascript instead of filtering Contact by Account simply by modifying the form using OOB toools? – AdamV Oct 23 '14 at 17:28

1 Answers1

6

Some characters have special meaning in XML and ampersand (&) is one of them. Consequently, these characters should be substituted (ie use string replacement) with their respective entity references. Per the XML specification, there are 5 predefined entities in XML:

&lt;    <   less than
&gt;    >   greater than
&amp;   &   ampersand 
&apos;  '   apostrophe
&quot;  "   quotation mark

Alternatively, you can place "text" strings that might contain special characters within a CDATA section so XML parsers will not attempt to parse them. Example:

<SomeElement><![CDATA[This & will be ignored]]></SomeElement>
Leo Bedrosian
  • 3,659
  • 2
  • 18
  • 22