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);
        }
    }
}
2
  • 3
    It must be escaped to &amp; Use a utility function (I assume JavaScript): how to escape xml entities in javascript? Commented Oct 20, 2014 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? Commented Oct 23, 2014 at 17:28

1 Answer 1

7

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.