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:

webresourctype

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

<HTML xmlns=”http://www.w3.org/1999/xhtml”><HEAD><TITLE></TITLE&gt;

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

<STYLE type=text/css>

select.ms-crm-SelectBox

{   padding-top: 0px;

width: 100%;

color: #000000;

font-size: 11px;

}

</STYLE>

<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

GetCustomerAddress();

}

function GetCustomerAddress() {

if (window.parent.Xrm.Page.data.entity.attributes.get(_Sourcefield).getValue() != null) {

var ParentID = window.parent.Xrm.Page.data.entity.attributes.get(_Sourcefield).getValue()[0].id;

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

var AddressReq = new XMLHttpRequest();

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

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

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

AddressReq.send();

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 (window.parent.Xrm.Page.data.entity.attributes.get(_Targetfield).getValue() != null) {

var value = window.parent.Xrm.Page.data.entity.attributes.get(_Targetfield).getValue();

SetValue(value);

}

}

function AddressOnchange(value) {

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

window.parent.Xrm.Page.data.entity.attributes.get(_Targetfield).setValue(value);

}

else{window.parent.Xrm.Page.data.entity.attributes.get(_Targetfield).setValue(null);}

}

function ClearSelectOptions() {

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

dropdown.options.length = 0;

}

function SetValue(Value) {

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

}

</SCRIPT>

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

<BODY contentEditable=true onload=Onload()>

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

<TBODY>

<TR>

<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.

webresourceparameter

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

                       prefix_moreaddress,customerid

moreaddressfield

  • 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

opportunitymoreaddress

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

Enoy !!!

Advertisements

1 Comment

Filed under MS CRM 2011, Uncategorized

One response to “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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s