SOAP.Retrieve can't retrieve lookup value?

Jan 28, 2015 at 2:30 PM
Edited Jan 28, 2015 at 2:31 PM
I'm relatively new to Dynamics CRM and I'm slowly learning how to do things. Honestly, if this library didn't exist I wouldn't even try to use SOAP or REST to get data from a related record. But since it does (thank you!) there's something I'm trying to do that isn't working when I think it should. What I want to do is, on a Case form, display both the related contact's information and also the contact's related account's information. I've created a contact quickview form and an account quickview form that displays the information I want, however it's currently impossible to nest the account quickview form inside the contact quickview form. So instead what I'm trying to do is add some javascript that copies the contact's account field to an account field that's on the case form and have the account quickview form link to this field.

In the org I'm trying to setup I'm using two custom fields on the case form that are a lookup to a contact record ("new_contact") and a lookup to an account record ("new_account"). With the contact forms I've created a custom field that's a lookup to an account record ("new_companyname"). What the javascript code below is supposed to do is, whenever the case's contact lookup field is changed it uses SOAP.Retreive to look up the contact's account lookup field and copy it's value to the case's account lookup field, however the code isn't working correctly.
function CopyContactsAccount()
{
    var contact = Xrm.Page.getAttribute("new_contact").getValue();
    if (contact==null)
    {
        Xrm.Page.getAttribute("new_account").setValue(null);
    }
    else
    {
        var retrievedcontact = XrmServiceToolkit.Soap.Retrieve("contact", contact[0].id, ["new_companyname"]);
        Xrm.Page.getAttribute("new_account").setValue(retrievedcontact.attributes['new_companyname'].value);
    }
    Xrm.Page.getAttribute("new_account").fireOnChange();
}
On the case record when I delete the contact field the account field is cleared just fine by the first half of the 'if' statement, but when I set the contact field to a contact record which has the account field set, the case's account field just stays cleared. So the second half of the 'if' statement isn't working. There are no error messages and I've learned from using Alert statements that retrievedcontact.attributes['new_companyname'].value is set to null but retrievedcontact.attributes['new_companyname'].id is not null. Also, if I retriev the contact record's email address that works just fine. Can anyone tell me why SOAP.Retrieve cannot retrieve the value in a lookup field and does anyone have any ideas of what I can do to fix it?
Jan 28, 2015 at 2:52 PM
I forgot to mention I am on Dynamics CRM online.
Jan 28, 2015 at 9:52 PM
I have a similar scenario I just tried implementing. With emails, when I click the Reply button the email draft it creates automatically sets the 'From' field to the logged-in user. Instead I'd prefer it to reply from the same email address the original email was sent to. At my company, we want all emails to & from our agents to come from specific email addresses. I already have it setup so that our users have access to send emails on behalf of these addresses. I just added the parentactivityid field to the email form and I'm trying to use the code below to change the 'From' field using the email forms onLoad event.
function SetFromAddress()
{
    if (Xrm.Page.getAttribute("statuscode").getValue()==1) // only edit if email is a Draft (option 1)
    {
        var parentfield = Xrm.Page.getAttribute("parentactivityid").getValue();
        var parentemail = XrmServiceToolkit.Soap.Retrieve("email", parentfield[0].id, ["to"]);
        Xrm.Page.getAttribute("from").setValue(parentemail.attributes['to'].value);
        Xrm.Page.getAttribute("from").fireOnChange();
    }
}
Like my post above, this code doesn't work and neither does it give an error message. Strangely enough, though, when I had made a mistake and had Soap.Retreive retrieve the 'From' field instead of the 'To' field from the parent activity, this code worked just fine and changed the 'From' field of the reply. if I use the alert function to test if parentemail.attributes['to'].value is null it returns false.

The alternative to doing this I suppose would be to try and edit/replace the commands behind the email form's Reply, Reply All, and Forward buttons using RibbonWorkbench but I'm not sure where to begin figuring that out.
Feb 5, 2015 at 9:42 PM
Edited Feb 5, 2015 at 9:44 PM
I solved my initial problem of retrieving the lookup value. Below is the code that I got to work. Apparently I just didn't know enough about JavaScript.
function CopyContactsAccount()
{
    var contact = Xrm.Page.getAttribute("new_contact").getValue();
    if (contact==null)
    {
        Xrm.Page.getAttribute("new_account").setValue(null);
    }
    else
    {
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("contact", contact[0].id, ["new_companyname"]);
        var contactsAccount = retrievedContact.attributes["new_companyname"];

        var copyAccount = new Array();
        copyAccount[0] = new Object();
        copyAccount[0].id = contactsAccount.id;
        copyAccount[0].name = contactsAccount.name;
        copyAccount[0].entityType = "account";

        Xrm.Page.getAttribute("new_account").setValue(copyAccount);
    }
    Xrm.Page.getAttribute('new_account').fireOnChange();
}
With the second question, I'm no longer trying to use the email form's OnLoad event to change the From field. Instead I'm trying to use the toolkits email create feature to just completely replace the default reply, replyall, and forward functions. This way I won't see the form temporarily load with the default value before OnLoad can change it.
Coordinator
Feb 8, 2015 at 5:38 PM
Hi There.

With the toolkit, Lookup Field returned with id, type, name attributes. For example, retrievedContact.attributes["new_companyname"].id

I do recommend you have a look at the SDK and document section of the toolkit about how to use the toolkit in CRM and how to assign value for a lookup field.

https://xrmservicetoolkit.codeplex.com/documentation

Kind Regards,
Jaimie