Microsoft Dynamics Blog

Welcome to Armanino’s Microsoft Dynamics Blog, where you’ll find information on topics across Microsoft Dynamics 365 ERP and CRM, Dynamics GP, Power BI, PowerApps, Flow and more. Use these tips, tricks and insights to get the most out of your applications! Want these articles delivered straight to your inbox? Subscribe to our newsletter below.

Friday, June 14, 2013

Lookupsingle.aspx issues in Microsoft CRM after Rollup 12

Posted by Nathan Vorhees

With Rollup (RU) 12, all lookupsingle.aspx behavior has changed! Two items to note:

  1. All pop windows now cause a post back
  2. The format of the data returned form lookupsingle.aspx has changed

In RU 12, the format of lookupsingle.aspx now returns all the fields related to the item selected, not just the GUID. Secondly, you will need to return `false` to the form after the lookup to prevent a post back of the form – otherwise you get an annoying “leave this page” message.

Overall implementation:

Add a button to the contact form which looks up addresses of the selected parent account/customer:

Click the button to get lookupsingle.aspx filtered for that account.

Select the address – and then the fields on the form get populated.

You will need the JSON library from the CRM SDK for the code provided to work. (SDKSampleCodejsrestendpointjqueryrestdataoperationsjqueryrestdataoperationsscriptsjson2.js)

Add a button to your form to call lookupsingle.aspx
Add a yes/no field to the form, attach this function, onload call it :
ConvertToButton(“new_lookupaddress”, “Lookup Address”, “100%”, lookupAddress);

1)    Create the function lookupAddress.

2)    Add the popup function

String it all together with some testing and you are good to go. QED.

Code to be included :


function contact_onLoad(){
        var formType = Xrm.Page.ui.getFormType();
        switch (formType){
                        case FORM_TYPE_CREATE:
                        case FORM_TYPE_UPDATE:
                                        ConvertToButton(“am_lookupaddress”, “Lookup Address”, “100%”, lookupAddress);
                                        break;
        }
        showSubRoleSections();
}
// CRM 2011 Style Button
// Creates a button from a form field that is a two option item
function ConvertToButton(fldName, btnLabel, btnWidth, evt){
        var btn = ‘<button id=”btn_’ + fldName + ‘” ‘ +
                        ‘ style=”width:’ + btnWidth + ‘” ‘ +
                        ‘ class=”ms-crm-Button” ‘ +
                        ‘ onmouseover=”Mscrm.ButtonUtils.hoverOn(this);” ‘ +
                        ‘ onmouseout=”Mscrm.ButtonUtils.hoverOff(this);” ‘ +
                        ‘>’ + btnLabel + ‘</button>’;
   
        var ctrl = Xrm.Page.ui.controls.get(fldName)._control;
        // Add the new button
        ctrl.get_element().innerHTML += btn;
         // Hide the textbox
        ctrl.get_element().firstChild.style.display = ‘none’;
        // Hide the label (optional)
        // Xrm.Page.ui.controls.get(‘pager’).setLabel(”);
        // Add Event to the newly created button
        ctrl.get_element().childNodes[1].attachEvent(‘onclick’, evt);
}
// Function that is called from the OnClick on the button
function lookupAddress(){
        // insert any pre validation here only call if parent customer id is present
        var parent = Xrm.Page.getAttribute(“parentcustomerid”).getValue();
        if (parent != null) {
                        getAddress(Xrm.Page.getAttribute(“parentcustomerid”));
        }
        // RU 12 NEEDS TO RETURN FALSE TO PREVENT POSTBACK
        return false;
}
// Field mapping is the form field and the source Field Schema name.
var ADDR_FIELD_MAPPING = [[“name”, “Name”],
                                                                                          [“line1”, “Line1”],
                                                                                          [“line2”, “Line2”],
                                                                                          [“line3”, “Line3”],
                                                                                          [“city”,    “City”],
                                                                                          [“stateorprovince”,   “StateOrProvince”],
                                                                                          [“postalcode”,     “PostalCode”],
                                                                                          [“country”, “Country”],
                                                                                          [“telephone1”,   “Telephone1”]];
// Function that is called from the OnClick on the button
function lookupAddress(){
        // insert any pre validation here only call if parent customer id is present
        var parent = Xrm.Page.getAttribute(“parentcustomerid”).getValue();
        if (parent != null) {
        accountLookup = Xrm.Page.getAttribute(“parentcustomerid”);
        var eventSrc = accountLookup.getName();
        var targetPrefix = “address1_”;
        if (targetPrefix != null && accountLookup.getValue() != null){
                        var selectedAddress = popupAddressLookup(accountLookup.getValue()[0].id, accountLookup.getValue()[0].type);
                       
                        if (selectedAddress != null){
                                        // selectedAddress now is an array of the items we want city [name/value], country etc
                                        // for each field in the field mapping array above – loop through and populate the data.               
                                                        for (var i = 0; i < ADDR_FIELD_MAPPING.length; i++){
                                                                        var targetAttribute = Xrm.Page.getAttribute(targetPrefix + ADDR_FIELD_MAPPING[i][0]);
                                                                        //var sourceValue = accountObject[ADDR_FIELD_MAPPING[i][1]];
                                                                        var sourceValue = selectedAddress[ADDR_FIELD_MAPPING[i][0]].value;
                                                                        try{
                                                                                        targetAttribute.setValue(sourceValue);
                                                                        }
                                                                        catch(err)
                                                                        {}            
                                                        }
                                        //}
                        }
        }
        }
        // THIS IS THE IMPORTANT LINE TO ADD FOR RU12!
        return false;
}
//show overall address popup
function popupAddressLookup(lookupId, lookupType){
                        // open popup for address lookup.
                        var url = ‘/’ + Xrm.Page.context.getOrgUniqueName() +
                        ‘/_controls/lookup/lookupsingle.aspx?class=BrowseCustomerAddress&objecttypes=1071&browse=1&parentType=’ +
                                        lookupType+ ‘&parentId=’ + lookupId+ ‘&ShowNewButton=1&ShowPropButton=1&DefaultType=1071’;
        // shows a modal window to select the addresses
                       
        var selectedItem = window.showModalDialog(url, null, ‘dialogWidth:600px;dialogHeight:400px;resizable:yes;menubar=0;status=1;location=0;toolbar=0;scrollbars=1;’);
        try{
                        // RU 11 just returned the GUID of the selected item
                                        //return selectedItem.items[0].id;
                                       
                                        // RU 12 returns all the values in a string
                                        // selectedAddress.items[0] has id and key values
                                        // build and excute lookup query.
                                        var test = JSON.parse(selectedItem);
                                        var addressFields = JSON.parse(test.items[0].keyValues);
                                        return addressFields;
        }
        catch (err) {
                        return null;
        }
}

COMMENTS

comments powered by Disqus
« | »