Fetching data from Accounts in Quotes using jQuery and REST Service

Goal
To create a new Quote. Choose the Potential Customer, which is none other than an Account. Get the address associated with the account and display in the Bill to and Ship to address.

Step 1 : Navigate to Settings >> System Customization >> Customize the System >> Web Resources

Step 2 : Create a new JScript type resource, name it as AddressScript.js. A default new_ will get appended to the name. So the resulted file name will be new_AddressScript.js.
Click on the Text Editor button to open the editor popup and place the following script into the editor.

function FetchAddress() {
        // Get the AccountID from the page         var attrValue = Xrm.Page.data.entity.attributes.get('customerid').getValue();        
        if (attrValue != null)         {               for (var i = 0; i < attrValue.length; i++)               {                   attrValueId=attrValue[i].id;               }         }
      // Specify the Columns to be retrieved from Account
  var colsToRetrieve = "Address1_AddressId, Address1_City,  Address1_County,Address1_Line1,Address1_Line2,Address1_PostalCode"
    // Make the ajax call to the REST web service to get the Address Details
    $.ajax({          type: "GET",         contentType: "application/json; charset=utf-8",         datatype: "json",         url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'"+ attrValueId + "')?$select="+colsToRetrieve,         beforeSend: function (XMLHttpRequest) {             //Specifying this header ensures that the results will be returned as JSON.             XMLHttpRequest.setRequestHeader("Accept", "application/json");         },         success: function (data, textStatus, XmlHttpRequest) {                  if(data.d!=null)                  {                       var accounts = data.d;                       Xrm.Page.data.entity.attributes.get('shipto_line1').setValue(accounts["Address1_Line1"]);                       Xrm.Page.data.entity.attributes.get('shipto_line2').setValue(accounts["Address1_Line2"]);                       Xrm.Page.data.entity.attributes.get('shipto_city').setValue(accounts["Address1_City"]);                       Xrm.Page.data.entity.attributes.get('shipto_postalcode').setValue(accounts["Address1_PostalCode"]);
                      Xrm.Page.data.entity.attributes.get('billto_line1').setValue(accounts["Address1_Line1"]);                       Xrm.Page.data.entity.attributes.get('billto_line2').setValue(accounts["Address1_Line2"]);                       Xrm.Page.data.entity.attributes.get('billto_city').setValue(accounts["Address1_City"]);                       Xrm.Page.data.entity.attributes.get('billto_postalcode').setValue(accounts["Address1_PostalCode"]);
                  }                   else                   {                       alert('Result is null');                   }         },         error: function (request, textStatus, errorThrown) {                   alert("Error occurred");         }     });
}

 Step 3 : Now save and publish the Web resource.

Step 4 :   Navigate to Quotes from view from Settings >> System Customization >> Customize the System >> Entities >> Quote >> Forms

Step 5 : Add the following libraries in Form Properties jQuery.1.5.2.min.js new_AddressScript.js (Newly created one, mentioned above)

Step 6 : Chose the “Potential Customer” from the control dropdown and “OnChange” from the event dropdown.

Step 7 : Call the FetchAddress() function. Save and Publish the customization.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!