Lookup Address for Custom Entities in CRM 2011

Customer address entity in CRM 2011 stores all the addressed for contacts and accounts. I recently got a requirement to create all address fields on opportunity and a custom entity. Customer also wanted to populate these addresses based on a address lookup similar to the option on quote,order and invoice entity. I wanted to do this as a custom silver light application but then found a way to reuse the existing address lookup logic in out of the box CRM. I am sharing this piece of code with you for putting address lookup on opportunity entity. You can change the code to display the same option on any custom entity.

STEP 1: Create the address fields on opportunity entity. For this example I created address1 fields as follows:

1.) new_address1_line1

2.) new_address1_line2

3.) new_address1_line3

4.) new_address1_city

5.) new_address1_state

6.) new_address1_zip

7.) new_address1_country

Then, exact same fields were created for address2.

STEP 2: Copy this script to a new web resource in your CRM solution.

function CustomLookup () {

var aoItems = getFieldValue(“new_accountid”);

if (aoItems == null) {

alert(“Account is not Selected”);

return;

}

var _object = openStdDlg(“/sfa/quotes/dlg_lookupaddress.aspx?headerForm=1&parentType=1&parentId=” + aoItems[0].id + “&willCall=0”, “LookupAddress”, 500, 330, true);

if (object) {

PopulateBillToAddress (object);

PopulateShipToAddress (object);

}

}

function PopulateBillToAddress(object) {

if (object.BillTo) {

setFieldValue(“new_address1_name”, object.Address.Name);

setFieldValue(“new_address1_line1”, object.Address.Line1);

setFieldValue(“new_address1_line2”, object.Address.Line2);

setFieldValue(“new_address1_line3”, object.Address.Line3);

setFieldValue(“new_address1_city”, object.Address.City);

setFieldValue(“new_address1_state”, object.Address.StateOrProvince);

setFieldValue(“new_address1_zip”, object.Address.PostalCode);

setFieldValue(“new_address1_country”, object.Address.Country);

}

}

function PopulateShipToAddress(object) {

if (object.ShipTo) {

setFieldValue(“new_address2_name”, object.Address.Name);

setFieldValue(“new_address2_line1”, object.Address.Line1);

setFieldValue(“new_address2_line2”, object.Address.Line2);

setFieldValue(“new_address2_line3”, object.Address.Line3);

setFieldValue(“new_address2_city”, object.Address.City);

setFieldValue(“new_address2_state”, object.Address.StateOrProvince);

setFieldValue(“new_address2_zip”, object.Address.PostalCode);

setFieldValue(“new_address2_country”, object.Address.Country);

}

}

function setFieldValue(fieldName, fieldValue) {

Xrm.Page.getAttribute(fieldName).setValue(fieldValue);

}

STEP 3: You will have to add the following XML to your ribbondiffxml in customization file to add the button.

<RibbonDiffXml>

<CustomActions>

<CustomAction Id=”sample.Form.opportunity.MainTab.Workflow.CustomAction” Location=”Mscrm.Form.opportunity.MainTab.Workflow.Controls._children” Sequence=”10″>

<CommandUIDefinition>

<Button Command=”sample.Form.opportunity.MainTab.Workflow.Command” CommandType=”General” Id=”sample.Form.opportunity.MainTab.Workflow.Button” Image16by16=”/_imgs/SFA/LookupAddress_16.png” Image32by32=”/_imgs/SFA/LookupAddress_32.png” LabelText=”$LocLabels:sample.Form.opportunity.MainTab.Workflow.LabelText” TemplateAlias=”o1″ ToolTipDescription=”$LocLabels:sample.Form.opportunity.MainTab.Workflow.ToolTip” ToolTipTitle=”$LocLabels:sample.Form.opportunity.MainTab.Workflow.LabelText” />

</CommandUIDefinition>

</CustomAction>

</CustomActions>

<Templates>

<RibbonTemplates Id=”Mscrm.Templates”></RibbonTemplates>

</Templates>

<CommandDefinitions>

<CommandDefinition Id=”sample.Form.opportunity.MainTab.Workflow.Command”>

<EnableRules>

<EnableRule Id=”Mscrm.CustomcheckRole” />

</EnableRules>

<DisplayRules />

<Actions>

<JavaScriptFunction Library=”$webresource:new_JscriptLibrary” FunctionName=” CustomLookup ” />

</Actions>

</CommandDefinition>

</CommandDefinitions>

<RuleDefinitions>

<TabDisplayRules />

<DisplayRules />

<EnableRules>

<EnableRule Id=”Mscrm.CustomcheckRole”>

<FormStateRule State=”Existing” />

</EnableRule>

</EnableRules>

</RuleDefinitions>

<LocLabels>

<LocLabel Id=”sample.Form.opportunity.MainTab.Workflow.LabelText”>

<Titles>

<Title languagecode=”1033″ description=”Look Up Address” />

</Titles>

</LocLabel>

<LocLabel Id=”sample.Form.opportunity.MainTab.Workflow.ToolTip”>

<Titles>

<Title languagecode=”1033″ description=”Select an address and mark it as Bill To, Ship To, or both” />

</Titles>

</LocLabel>

</LocLabels>

</RibbonDiffXml>

Important Notes:

1.) Do not forget to include new_JscriptLibrary on your form under form properties.

2.) new_accountid is a field on the form to look for additional addresses

13 thoughts on “Lookup Address for Custom Entities in CRM 2011

Add yours

  1. Mark,
    The solution mentioned by Alex is great but it is going to create an aspx page in the ISV folder. i would recommend you creating an HTML or SilverLight web resource for the same.

  2. I tried this on the Contact Entity as my client wants to pull the Account Addresses into the contact form. The Jscript should work by removing the New_ on the fields. However the RibbonDiff code is for Opportunity. I added the contact to a solution and exported the XML. I tried to parse your ribbon diff code in so the tags matched but the import failed due to the fact you are referencing Opportunity. What would I need to change on the Ribbon Diff to get this to work on Contact? Thanks.

  3. Thanks, this is a great solution. It’s a shame that dealing with the built-in address entity in CRM is such a faff. I used your solution and copied the dlg_lookupaddress.aspx code into the /isv/ folder to customize the form slightly, since the ‘ship to’/’bill to’ wasn’t relevant for me.

    1. I am curious how you customized the dlg_lookupaddress.aspx code. I am trying to populate the shipto address on the quotedetail form based on a new_contactid field. I too would like to eliminate the “ship to”/”bill to” checkboxes and lookup only contact addresses (type=2).

      1. Hi Mark,
        I did not modify the dialog window in this blog post but only used the existing features to populate custom fields. You can create a custom dialog using web resource if you wish to create a custom dialog for users to populate address information.

        Ashish

        1. Right. Using your example I was able to create a custom lookup to populate the ship to address information on the quotedetail form specifying … parentType=2&parentId=” + aoItems[0].id (where id is a new_contactid) … Thank you.

          What I’m trying to sort out is how I might be able to create a web resource that functions like dlg_lookupaddress.aspx but eliminates the “Bill To Address” and “Ship To Address” checkboxes as I am only interested in populating the ship to address on the quotedetail form based on passing a new_contactid.

          Essentially, the same behaviour as the out of the box [Lookup Address] button on the quotedetail form — but based on the quotedetail.new_contactid instead of quote.customerid.

          My sense is that is what Alex Marshall did based on his 11-28-12 reply.

          -MarkS

  4. Hiren,
    openStdDlg is out of the box lookup code provided for address lookup. You don’t have to create any custom webpage to use this functionality. Simply copy the code and it will work for you. You can also get to the URL used in openStdDlg by using developer toolbar and selecting the lookup tag on quote form in CRM.

    Let me know if you need additional help.

  5. Hi Ashish,
    I have created a custom optionSet called ‘Country’ & ‘State’ in Address entity and wants to display those option set values in Order entity when the user selects the ‘Bill To’ & ‘Ship To’ option from ‘Look Up Address’ button. Can you please assist me?
    Regards,
    Jay.

      1. Hi Ashish,
        Good Job, Works great!! but in my case iI have a question, as well as I populate the address fields, its possible to set the address number or a custom field of address entity? I try to write
        setFieldValue(“new_addressnumber”, object.Address.AddressNumber);

        or alert(object.Address.AddressNumber);
        but the result is undefinded.

        Could you please assist me?

        Thanks in advance,
        Regards,
        Alberto

Leave a reply to Rick Cancel reply

Create a website or blog at WordPress.com

Up ↑