Get Customer address in Sales entities using HTML web resource

We got a requirement to show customer address (more address) in opportunity entity, so that customer can select bill to address from there. So I thought to create a html web resource to show customer address based on the customer selected in opportunity form, but this web resource can be used in any of the sales entity to fetch more address based on customer. In this post I am going to provide steps to create html drop down to show customer address:

  • Create a html web resource and name it like below:


  • Select Type as Web Page (HTML) and click on Text Editor.
  • Paste below code there:

<HTML xmlns=””><HEAD><TITLE></TITLE>

<META content=IE=8 http-equiv=X-UA-Compatible>

<STYLE type=text/css>

{   padding-top: 0px;

width: 100%;

color: #000000;

font-size: 11px;



<SCRIPT src=”../ClientGlobalContext.js.aspx”></SCRIPT>

<SCRIPT type=text/jscript src=”../new_Json2.js”></SCRIPT>

<SCRIPT type=text/javascript>

var _Targetfield;

var _Sourcefield;

function Onload() {

var fieldcollection = new Array();

_Targetfield = GetGlobalContext().getQueryStringParameters().data;

var fields = _Targetfield.split(“,”);

_Targetfield = fields[0];

_Sourcefield = fields[1];

//to check if there is already value



function GetCustomerAddress() {

if ( != null) {

var ParentID =[0].id;

var ServerURL = window.parent.Xrm.Page.context.getServerUrl() + “/xrmservices/2011/organizationdata.svc”;

var AddressReq = new XMLHttpRequest();“GET”, ServerURL + “/CustomerAddressSet?$filter=ParentId/Id eq (guid'” + ParentID + “‘)”, false);

AddressReq.setRequestHeader(“Accept”, “application/json”);

AddressReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);


if (AddressReq.readyState == 4 /* complete */) {

if (AddressReq.status == 200) {

var retrieved = this.JSON.parse(AddressReq.responseText).d;

var Results = retrieved.results;

if (Results.length > 0) {

var AddressSelect = document.getElementById(“Addressname”);

for (var i = 0; i < Results.length; i++) {

if (Results[i].AddressNumber > 2) {

var option = document.createElement(“option”);

option.text = Results[i].Name;

option.value = Results[i].Name; ;

AddressSelect.add(option, AddressSelect.options[null]);


if ( != null) {

var value =;




function AddressOnchange(value) {

if (value != null && value != “”) {;




function ClearSelectOptions() {

var dropdown = document.getElementById(“Addressname”);

dropdown.options.length = 0;


function SetValue(Value) {

document.getElementById(“Addressname”).value = Value;



<META charset=utf-8></HEAD>

<BODY contentEditable=true onload=Onload()>

<TABLE style=”WIDTH: 100%; HEIGHT: 100%” cellSpacing=0 cellPadding=0>



<TD width=”100%” vAlign=top><SELECT id=Addressname onchange=AddressOnchange(this.value) height=”4″> <OPTION selected value=”” text=””></OPTION></SELECT> </TD></TR></TBODY></TABLE></BODY></HTML>

  • Save and Publish web resource.
  • Create another Jscript web resource and use below code there

function GetCustomerAddress() {

Xrm.Page.getControl(“WebResource_moreaddress“).getObject().contentWindow.window.GetCustomerAddress(); //change name of moreaddress there


  • Add a txt attribute in opportunity entity which will store selected customer address name. (I have created a text field with More Address name)
  • Open opportunity form and place web resource there like below.


  • Make sure to pass Target field and Source field in below format



  • Add your Jsccript web resource to opportunity entity form and call your method onchange of customerid field. // I am using customerid field here
  • Save and publish your customization and when you will try to create opportunity record you should be able to see customer address (more address if any) like below


Note: If you are going to copy code please make sure to change quotes.

Enoy !!!

2 thoughts on “Get Customer address in Sales entities using HTML web resource

  1. Hongbo

    Great example! We often have requirements that the user select data from outer resources.
    We usually do it by create a CRM picklist, and hack the CRM form to populate the picklist with outside data, and then save the selected data to a CRM text field.
    Now I change to use regular HTML dropdownlist hosted in web resources and avoid the “unsupported” hack, thanks to this article.


Leave a Reply

Your email address will not be published. Required fields are marked *