Error using addCustomView in CRM 2016

1.8k views Asked by At

There are two entities in the CRM, 'Contacts' and a custom entity called 'Services', that are connected with a many-to-many relationship. On a separate form, in the entity 'Service Activity', there is a service field and a contact field. My goal is to filter the contact field with the selected service but only if the service field is populated. If it is populated I want to add a custom view to the contact field that only shows contacts that are connected to the specified service record. Otherwise the contacts field only shows the default view.

Here is my code:

function filtroRecurso()
{
    var servicioEd = Xrm.Page.data.entity.attributes.get("new_servicio");
    if (servicioEd.getValue() != null)
        {
            var serviceId  = servicioEd.getValue()[0].id;
            var serviceName = servicioEd.getValue()[0].name;
            var viewId = "{00000000-0000-0000-0000-000000000001}";
            var entityName = "contact";
            var viewDisplayName = "Custom View";
            var fetchXml;

            fetchXml = "<fetchxml>" +  
                "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" + 
                "<entity name='contact'>" + 
                "<attribute name='fullname' />" + 
                "<attribute name='new_contacttype' />" + 
                "<attribute name='telephone1' />" + 
                "<attribute name='new_prepacademico' />" + 
                "<attribute name='new_municipio' />" + 
                "<attribute name='new_modalidades' />" + 
                "<attribute name='emailaddress1' />" + 
                "<attribute name='address1_line2' />" +
                "<attribute name='address1_line1' />" + 
                "<attribute name='contactid' />" + 
                "<order attribute='fullname' descending='false' />" + 
                "<filter type='and'>" + 
                "<condition attribute='statecode' operator='eq' value='0' />" + 
                "<condition attribute='new_contacttype' operator='eq' value='100000019' />" + 
                "</filter>" + 
                "<link-entity name='connection' from='record1id' to='contactid' alias='ab'>" + 
                "<filter type='and'>" + 
                "<condition attribute='record2id' operator='eq' value='" + serviceId + "' />" + 
                "</filter>" + 
                "</link-entity>" + 
                "</entity>" + 
                "</fetch>" + 
                "</fetchxml>";

            var layoutXml = "<layoutxml>" + 
                "<grid name='resultset' object='2' jump='fullname' select='1' preview='1' icon='1'>" + 
                "<row name='result' id='contactid'>" + 
                "<cell name='fullname' width='150' />" + 
                "<cell name='new_contacttype' width='100' />" + 
                "<cell name='new_prepacademico' width='100' />" + 
                "<cell name='new_modalidades' width='100' />" + 
                "<cell name='telephone1' width='100' />" + 
                "<cell name='emailaddress1' width='150' />" + 
                "<cell name='address1_line1' width='150' />" + 
                "<cell name='address1_line2' width='150' />" + 
                "<cell name='new_municipio' width='100' />" +
                "</row>" +
                "</grid>" + 
                "</layoutxml>";

            Xrm.Page.getControl("new_maestros").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
        }
}

I retrieved the fetchXml and layoutXml using Saved Queries from a custom view I created. In the view, I filtered by a specific Service record to test that it would return the correct Contact records, which it did, but in my code I replace the specific Service name and id with the name and id retrieved from the record specified in the service field that is on the form.

When service field is empty the contacts field is set to its default view. But if the service field is populated the form returns an error when you try to choose a Contact record. It doesn't help that the error is the generic "An error has occurred" one that offers no information.

Is my code incorrect or is my goal unattainable?

1

There are 1 answers

0
Sxntk On BEST ANSWER

This is how your fetch should work

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>  
    <entity name='contact'>  
        <attribute name='fullname' />  
        <attribute name='new_contacttype' />  
        <attribute name='telephone1' />  
        <attribute name='new_prepacademico' />  
        <attribute name='new_municipio' />  
        <attribute name='new_modalidades' />  
        <attribute name='emailaddress1' />  
        <attribute name='address1_line2' /> 
        <attribute name='address1_line1' />  
        <attribute name='contactid' />  
        <order attribute='fullname' descending='false' />
        <filter type='and'>
            <condition attribute='statecode' operator='eq' value='0' />
            <condition attribute='new_contacttype' operator='eq' value='100000019' />
        </filter>
        <link-entity name='connection' from='record1id' to='contactid' alias='ab'>
            <filter type='and'>
                <condition attribute='record2id' operator='eq' value='YourGUID' />
            </filter>
        </link-entity>
    </entity>
</fetch>

We remove the <fetchxml> and </fetchxml>, comparing the uiname='" + serviceName + "' uitype='new_services' and "<attribute name='contactid' />" was duplicate