For this example, we will look at using a button click to populate form fields. We can use such a scenario when we need a calculation to take place on the form, or when we need to bring data from a related record. For example, we have a sub-account, and we need to bring and populate the address of the parent account. Automating this task will save the user a few minutes per record and will create a much better user experience.
For the purpose of this recipe, we can either use one of the pre-existing solutions, or create a new one. Make sure you have the proper permissions to allow you to customize the system as required. This example will work in both Online and On-Premise environments.
In order to create this functionality, perform the following steps:
new_getaddress
.GetAddressFromParent
. We will come back to the function content after the button is added to the ribbon.customizations.xml
file for editing.accountribbon.xml
).Actions
section of the Main
tab. So look for the tab defined with the following:<Tab Id="Mscrm.Form.account.MainTab …
Mscrm.Form.account.MainTab.Actions.Control
customizations.xml
file change the CustomActions
section to look as follows:<CustomActions> <CustomAction Id="OC.account.Form.Main.CustomAction" Location="Mscrm.Form.account.MainTab.Actions.Controls._children" Sequence="15" > <CommandUIDefinition> <Button Id="OC.account.Form.Main.Button" Command="OC.account.Form.Main.Command" LabelText="Copy Address" ToolTipTitle="Copy from Parent" ToolTipDescription="Copy Address from Parent Account" TemplateAlias="o1" /> </CommandUIDefinition> </CustomAction> </CustomActions>
TemplateAlias
. You would add the two images to the solution as well.Image16by16="$webresource:new_btn_16.png" Image32by32="$webresource:new_btn_32.png"
CommandDefinitions
section, and change it to look as follows:<CommandDefinitions> <CommandDefinition Id="OC.account.Form.Main.Command"> <EnableRules> </EnableRules> <DisplayRules> </DisplayRules> <Actions> <JavaScriptFunction Library="$webresource:new_getaddress" FunctionName="GetAddressFromParent" /> </Actions> </CommandDefinition> </CommandDefinitions>
function GetAddressFromParent() { try { var ParentAccount = Xrm.Page.getAttribute("parentaccountid"); if(ParentAccount != null) { var ParentAccountValue = ParentAccount.getValue(); if(ParentAccountValue != null) { var ParentAccountId = ParentAccountValue[0].id; var serverUrl = Xrm.Page.context.getServerUrl(); var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + ParentAccountId + "')?$select=Name,Address1_Line1,Address1_Line2,Address1_Line3,Address1_City,Address1_PostalCode"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: odataSelect, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { var org = data.d; //Change form data // alert("TEST: Country: " + org.Address1_Country[0].name + "; Province: " + org.Address1_StateOrProvince[0].name); Xrm.Page.data.entity.attributes.get("address1_name").setValue(org.Name); Xrm.Page.data.entity.attributes.get("address1_line1").setValue(org.Address1_Line1); Xrm.Page.data.entity.attributes.get("address1_line2").setValue(org.Address1_Line2); Xrm.Page.data.entity.attributes.get("address1_line3").setValue(org.Address1_Line3); Xrm.Page.data.entity.attributes.get("address1_city").setValue(org.Address1_City); Xrm.Page.data.entity.attributes.get("address1_postalcode").setValue(org.Address1_PostalCode); }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); } } ); } } } catch() {} }
There are two major steps in this recipe we followed. First off, we add a ribbon button. While adding the button, we define the button properties as well as the associated function that is called when the button is clicked. Secondly, we define a custom function. This function requires the OData and JSON libraries. Do make sure that these libraries are added to your solution. The function presented retrieves the ID of the parent account, and makes a call to get some of the address properties of the parent account. Once this information is retrieved, it is populated into the current account address fields.
18.221.182.150