Soap.Retrieve does not return 'parentcustomerid' field's entity type

Nov 10, 2015 at 3:52 PM
I'm trying to create a small piece of JavaScript that automatically sets the Customer field on the Case form when the Contact field changes. Below is my code. The problem I found out is that the XrmServiceToolkit.Soap.Retrieve function doesn't return the entitytype of the field, which could be an account OR a contact. The function does return the record name and ID just fine.
function ContactChange()
{
    var contact = Xrm.Page.getAttribute("primarycontactid").getValue();
    if (contact==null)
    {
        Xrm.Page.getAttribute("customerid").setValue(null);
    }
    else
    {
        alert("contact not null");
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("contact", contact[0].id, ["parentcustomerid"]);
        var contactsAccount = retrievedContact.attributes["parentcustomerid"];
        
        alert(contactsAccount.entityType);      // Returns 'unknown'
        
        Xrm.Page.getAttribute("customerid").setValue(contactsAccount);
    }
    Xrm.Page.getAttribute("customerid").fireOnChange();
    Xrm.Page.ui.refreshRibbon();
}
Is there a way to retrieve the field's entity type without doing trial and error with database queries until a search for a contact or an account with the feild's GUID returns something?
Nov 12, 2015 at 3:22 PM
I debugged and fixed a similar misbehaviour with a colleague a last week. The reason was that the attribute "parentcustomerid" is type of AliasedValue whose actual value was an EntityReference (you get the entity Name through the property "logicalName"). XST has a bug when processing the XML Response. I don't know exactly if it will help you, but we'll see.

Here is the faulty code: https://gist.github.com/anonymous/0a15a625583c5b68bc11
Nov 13, 2015 at 4:12 PM
Hi pbyte,

Thanks for the reply. I am familiar with the unique nature of Customer type fields such as 'customerid' in the case entity and 'parentcustomerid' in the contact entity. However I'm not code-savvy enough to incorporate the example code you point to, into XrmServiceToolkit. So I'm trying other alternatives. I tried building a little FetchXML query to return the 'parentcustomerid' field of the Case's Contact but it has the exact same issue that Soap.Retrieve has. It returns the guid and the name of the record that's in the field, but it does not return the entityType so I cannot use it to set the case's customerid field. I'm working on some additional code which just adds another SOPA query to see if an Account exists with the ID returned in the previous SOAP request. I'll post a copy when I'm finished.
Nov 13, 2015 at 4:44 PM
Here's the result I came up with:
function ContactChange()
{
    var contact = Xrm.Page.getAttribute("primarycontactid").getValue();
    if (contact==null)
    {
        Xrm.Page.getAttribute("customerid").setValue(null);
    }
    else
    {
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("contact", contact[0].id, ["parentcustomerid"]);
        var parentCustomer = retrievedContact.attributes["parentcustomerid"];
        var Customer = [];
        Customer[0] = new Object();
        Customer[0].id = parentCustomer.id;
        Customer[0].name = parentCustomer.name;
        Customer[0].entityType = "account";
        if (XrmServiceToolkit.Soap.Retrieve("account", parentCustomer.id)==null)
        {
            Customer[0].entityType = "account";
        }
        Xrm.Page.getAttribute("customerid").setValue(Customer);
    }
    Xrm.Page.getAttribute("customerid").fireOnChange();
    Xrm.Page.getAttribute("primarycontactid").setValue(contact);
    Xrm.Page.ui.refreshRibbon();  // Refresh the ribbon to show/hide buttons
}
One quick note for anyone reading this, I'm using Xrm.Page.getAttribute("customerid").fireOnChange() to update a Quick View form, but doing this automatically clears the Contact field. Rather than find an alternate way of refreshing the Quick View form, I just reset the contact field to what it was in the line below. The above works just fine in my initial testing.
Nov 16, 2015 at 2:40 PM
Edited Nov 16, 2015 at 2:42 PM
My mistake. I was debugging another piece of JavaScript recently that also used Soap.Retrieve and it didn't return the entity type attribute for that field either. I'm guessing the function doesn't do it for any field, but it would be nice if XrmServiceToolkit did return the entity type that way the result can be used to directly set the value of a field on a form without having to create a new array and object.