locked
Auto Populating Fields Based On A Lookup Change RRS feed

  • Question

  • I have an entity and it has a contact lookup field called c2_customer. On change of this lookup i am trying to populate some additional fields such as the address, mobile phone number of the contact/customer.

    Heres the code i have tried, it gives me no errors but it also doesent bring over the fields. They stay empty.

    function getContactDetails()
    {
        var lookUpObjectValue = Xrm.Page.getAttribute("c2_customer").getValue();
        if ((lookUpObjectValue != null))
        {
            var lookuptextvalue = lookUpObjectValue[0].name;
     
            var lookupid = lookUpObjectValue[0].id;
            //alert(lookupid);
     
     
        var serverUrl = Xrm.Page.context.getServerUrl();
     
        //The XRM OData end-point
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
     
     
        var odataSetName = "ContactSet";
     
        var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";
     
        //alert(odataSelect);
     
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
     
                var result_contact= data.d;
               
                //alert(result_contact.AccountNumber);
                            //replace the fields with the fields on your entity
    						Xrm.Page.getAttribute("c2_mobilephone").setValue(result_contact.mobilephone);
    						Xrm.Page.getAttribute("c2_street1").setValue(result_contact.address1_line1);
    						Xrm.Page.getAttribute("c2_street2").setValue(result_contact.address1_line2);
    						Xrm.Page.getAttribute("c2_street3").setValue(result_contact.address1_line3);
    						Xrm.Page.getAttribute("c2_county").setValue(result_contact.address1_county);
    						Xrm.Page.getAttribute("c2_city").setValue(result_contact.address1_city);
    						Xrm.Page.getAttribute("c2_postcode").setValue(result_contact.address1_postalcode);
    		
                          
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });
     
        }
     
       }

    I have imported the json and jquery file and published everything.



    Wednesday, July 31, 2013 12:08 PM

Answers

  • Hi,
    you need to use the schema name and not the logical name, try with:

    Xrm.Page.getAttribute("c2_mobilephone").setValue(result_contact.MobilePhone);
    Xrm.Page.getAttribute("c2_street1").setValue(result_contact.Address1_Line1);
    Xrm.Page.getAttribute("c2_street2").setValue(result_contact.Address1_Line2);
    Xrm.Page.getAttribute("c2_street3").setValue(result_contact.Address1_Line3);
    Xrm.Page.getAttribute("c2_county").setValue(result_contact.Address1_County);
    Xrm.Page.getAttribute("c2_city").setValue(result_contact.Address1_City);
    Xrm.Page.getAttribute("c2_postcode").setValue(result_contact.Address1_PostalCode);



    My blog: www.crmanswers.net

    • Marked as answer by KKHAN1991 Wednesday, July 31, 2013 12:42 PM
    Wednesday, July 31, 2013 12:21 PM

All replies

  • Hi,
    you need to use the schema name and not the logical name, try with:

    Xrm.Page.getAttribute("c2_mobilephone").setValue(result_contact.MobilePhone);
    Xrm.Page.getAttribute("c2_street1").setValue(result_contact.Address1_Line1);
    Xrm.Page.getAttribute("c2_street2").setValue(result_contact.Address1_Line2);
    Xrm.Page.getAttribute("c2_street3").setValue(result_contact.Address1_Line3);
    Xrm.Page.getAttribute("c2_county").setValue(result_contact.Address1_County);
    Xrm.Page.getAttribute("c2_city").setValue(result_contact.Address1_City);
    Xrm.Page.getAttribute("c2_postcode").setValue(result_contact.Address1_PostalCode);



    My blog: www.crmanswers.net

    • Marked as answer by KKHAN1991 Wednesday, July 31, 2013 12:42 PM
    Wednesday, July 31, 2013 12:21 PM
  • Ty that worked.

    Just a follow up question, how can i populate a lookfield field on this entity, which is based on a lookupfield inside the contact entity?

    For example the contact entity has a lookup field in the record which relates to another entity.I want the form that i am working on atm to get the details of that lookup when the contact lookup is changed. Also with option sets


    • Edited by KKHAN1991 Wednesday, July 31, 2013 1:14 PM
    Wednesday, July 31, 2013 12:33 PM