locked
CRM 2011: Load a Lookup field from another Lookup field's attribute value. RRS feed

  • Question

  • I just want a contact lookup on a form, but then I want to also put a disabled lookup field next to it that contains the contacts parent account and load it via JavaScript.  I cannot get the JavaScript down.

    Here's the JavaScript function I have come up with.  But it doesn't work.  The outter logic works, but I cannot get the attribute of the entity, which should contain the account id of the contact's parent account.  But it always shows up undefined no mater how I adjust or rewrite the code.  I think I am missing something simple here. 

    I am passing in the contact's GUID, it pulls the contact, but I cannot get the GUID of the parentcustomerid, which is the GUID for the parent account for this contact.

    I suspect the line [lookupValue[0].id = attribute.AccountId;] is the issue.  I don't know how to get the GUID from the contact that has been returned from the SDK call.

     RetrieveContact = function (id) {
            if (debug) { alert('RetrieveContact Id:' + id); }
    
            var lookupValue = new Array();
    
            SDK.JQuery.retrieveRecord(
                id,
                "Contact",
                null, null,
                function (contact) {
                    for(attribute in contact) {
                        switch (attribute) {
                            case "ParentCustomerId":
                                alert('found attribute');
                                lookupValue[0] = new Object();
                                lookupValue[0].id = attribute.AccountId;
                                lookupValue[0].name = attribute.name;
                                lookupValue[0].entityType = "Account";
    
                                if (debug) { alert('loaded lookup'); } 
                                Xrm.Page.getAttribute("new_parentaccount").setValue(lookupValue)                                            
                                break;
                            default:
                                break;
                        };
                    }
                },
                errorHandler
            );
        }
    Any thoughts or suggestions here? This seems simple but I cannot get it to work. I think I might be missing something in regards to how to pull the attribute from the contact to get the GUID, which is what I need to set the parent account lookup field.


    Jon Gregory Rothlander

    Thursday, April 17, 2014 9:45 PM

Answers

  • Hi Jon,

    Here is some working sample code which is similar to your needs, modify it to suite your requirements.

    function RetrieveContact (ContactId) {
     SDK.JQuery.retrieveRecord(
         ContactId,
         "Contact",
         null, null,
         function (contact) {
    	var lookup = new Array();
    	lookup[0] = new Object();
    	lookup[0].id = contact.ParentCustomerId.Id;
    	lookup[0].name = contact.ParentCustomerId.Name;
    	lookup[0].entityType = "Account";
    	Xrm.Page.getAttribute("new_parentaccount").setValue(lookup) 
         },
         errorHandler
       );
    }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by jonrothlander Friday, April 18, 2014 8:57 PM
    Friday, April 18, 2014 2:10 AM

All replies

  • Hi Jon,

    Here is some working sample code which is similar to your needs, modify it to suite your requirements.

    function RetrieveContact (ContactId) {
     SDK.JQuery.retrieveRecord(
         ContactId,
         "Contact",
         null, null,
         function (contact) {
    	var lookup = new Array();
    	lookup[0] = new Object();
    	lookup[0].id = contact.ParentCustomerId.Id;
    	lookup[0].name = contact.ParentCustomerId.Name;
    	lookup[0].entityType = "Account";
    	Xrm.Page.getAttribute("new_parentaccount").setValue(lookup) 
         },
         errorHandler
       );
    }


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by jonrothlander Friday, April 18, 2014 8:57 PM
    Friday, April 18, 2014 2:10 AM
  • Also, make sure that you enable the look up for which you are setting value if you set it to read only through form customization.

    Xrm.Page.getControl("new_parentaccount").setDisabled(false)
    
    Xrm.Page.getAttribute("new_parentaccount").setValue(lookupValue)
    
    Xrm.Page.getControl("new_parentaccount").setDisabled(true)

    • Edited by Ahmad Pirani Friday, April 18, 2014 2:44 PM removed additional line
    Friday, April 18, 2014 2:43 PM
  • Thanks.  I removed my for loop and coded it directly like you have here and it worked fine.

    Jon


    Jon Gregory Rothlander

    Friday, April 18, 2014 8:57 PM