0

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?

  • Remove `uiname='" + serviceName + "' uitype='new_services'`, you don't need this for custom filters on views – Sxntk Dec 26 '16 at 15:43
  • The problem is I need the view to filter its results depending on the selected service. Without that line it will return all contacts, regardless of their relation to the selected service. Is this not possible? – Carlos Farmer Jan 09 '17 at 14:55
  • What I'm telling you is to remove that characters, not the entire line. Like this: `"" +` – Sxntk Jan 10 '17 at 03:45
  • Thank you for your comment. I modified the line as you recommended but it still generates an error. Other than that one line does my code seem correct? Is it possible for me to achieve the goal I'm striving for? – Carlos Farmer Jan 12 '17 at 20:05
  • You have `""` + two times – Sxntk Jan 12 '17 at 21:40
  • Thank you again for commenting. I saw that mistake yesterday while working on this and removed it but it still gives me the error. – Carlos Farmer Jan 13 '17 at 13:35
  • Update your question please with the new code – Sxntk Jan 13 '17 at 13:41
  • I have updated my question to reflect the changes I've made in the code. Thank you for your continued interest in this issue. – Carlos Farmer Jan 13 '17 at 14:19

1 Answers1

1

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

Sxntk
  • 845
  • 14
  • 29