Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 9244

Auto Populating Fields Based On A Lookup Change

$
0
0

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.




Viewing all articles
Browse latest Browse all 9244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>