Tuesday, February 14, 2017

How to Set Lookup Field on Quick Create with Lookup Field of Parent Form

Here I will give an example of how to set lookup filed on quick create form with lookup field value of parent form in Microsoft Dynamic CRM Development.

Suppose we have Account lookup field in Quick create Form. And there I want to set account lookup with current account from the parent form. This example will help you to do that.

For this example, I have created a JavaScript web-resource add to the entity form. 

You can get the Id and name of a lookup record of the parent form with the following script. I used it to retrieve the Account field from a parent Role form, but it works the same way for all entities.

We want to set a specific Customer field on the Contact record depending on which +new button is clicked on the Parent form. We can also do this on the Quick Create form’s on-load event and use some parameters that are available from the parent window calling the Quick Create form.

There are two parameters that are available on the Quick Create form when called from a look-up control (CreateFromId and CreateFromType) that will determine which entity/record it is being called from. 

These are available as part of the Xrm.Page.Context.getQueryStringParameters() from the Quick Create form on load, see below code.

 function Form_Onload()   
 {  
 var contextParams = window.parent.Xrm.Page.context.getQueryStringParameters();  
 if (contextParams._CreateFromType != null&& contextParams._CreateFromType == "10017")   
 {  
    //To get the correct frame references, I've used code like this:  
 var contentFrame = top.document.getElementById("contentIFrame0");  
 if (contentFrame != null)   
 {  
    //lookup object of current account record  
 var clientLookup =contentFrame.contentWindow.Xrm.Page.getAttribute("New_accountid");  
 if (clientLookup != null)  
 {  
 var clientValue = clientLookup.getValue();  
 if (clientValue != null)   
 {  
 var object = new Array();  
 object[0] = new Object();  
 object[0].id = clientValue[0].id;  
 object[0].name = clientValue[0].name;  
 object[0].entityType = clientValue[0].entityType;  
    //set the new_account(Account look-up) from the case form to the ParentCustomerId (Parent Customer) field on the Quick Create form  
 window.parent.Xrm.Page.getAttribute("parentcustomerid").setValue(object);  
 }  
 }  
 }  
 }  
 }  

When you click on New in Contact Name lookup then the account will be displayed as selected in quick contact create from. Please check below images.



Read More:

0 comments