web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Auto-populating values in Quickcreate form using Mapping

(0) ShareShare
ReportReport
Posted on by 240

Hello, 

I want to explore the option of using Mapping to auto-populate fields in Quick Create form. I have a quickcreate form of entity Time Entry. I have two fields in form, Account and Contract, both looking upto Account and Contract entity respectively. I want my Contract field to be auto-filled based on the value chosen in the Account field. I have established a N:1 relationship between Contract:Account. And have also created a Mapping between the Account Name and Contract Name. Yet when I open a new Quickcreate form and chose the Account, the value of Contract is not populated. Do I have to do anything additional here?

Thank you. 

I have the same question (0)
  • Suggested answer
    JayBuddhdevtest Profile Picture
    275 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    Hi Aneeqa,

    The out of the box mapping won't work in this scenario as you are wanting to have the lookup updated within the quick create form. You would have to use a JavaScript, here is a blog post from PowerObjects that helps with your scenario:

    https://www.powerobjects.com/blog/2015/08/12/adding-javascript-to-quick-create-forms-in-crm-2015/

    However this thread is old (2015 CRM), but you can utilize the logic and can use the new Scripts.

    Please let me know if you have any questions/concerns.

    Thanks

    Jay

  • Linn Zaw Win Profile Picture
    3,407 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    As Jay Buddhdev mentioned, the mapping will only work if you open the quick create form from the Time Entry sub-grid/associated view on the Account form. The mapping will not work for empty quick create form and update the Account lookup value.

    You will have to add onChange JavaScript of the Account lookup to retrieve the required data with Xrm.WebApi.retrieveRecord and populate the Contract lookup. 

  • Aneeqa Pervaiz Profile Picture
    240 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    Thanks for the quick reply.

    So I have the JS code, but it is not running as it should. My entity which has the quick create form is Time Entry, the two entities I am working with is Account and Contract - both are individual entities and also exist in Time Entry entity as fields which look up to the main entities. Below is the schema name for all:

    Account Entity: account

    Field name in Account entity for name: name

    Contract Entity: msdyn_project

    Field name in Contract entity for name: msdyn_subject

    Name of Account and Contract fields in Time Entry entity (where the quickcreate form is)

    Account: new_account

    Contract: msdyn_project

    I will again point out that what I am looking to achieve is that in the quickcreate form, when the account is chosen (looked up to account entity), then the subsequent contract should be populated. Below is my JS Code. Can you please help me out with the Xrm.WebApi.retrieveMultipleRecords(....... and see if the rest of it is correct.

    function new_populateContract(executionContext) {

       var formContext = executionContext.getFormContext();

       if (formContext.getAttribute("new_account").getValue() !== null) {

           // Get account id

           var accountId = formContext.getAttribute("new_account").getValue()[0].id;

           accountId = accountId.replace(/\{|\}/gi, "").toLowerCase();

           // Retrieve associated contract of account

           Xrm.WebApi.retrieveMultipleRecords("msdyn_project", "?$select=??????????).then(

               function success(result) {

                   if (result.entities.length > 0) {

                       for (var i = 0; i < result.entities.length; i++) {

                           formContext.getAttribute("msdyn_project").setValue(result.entities[i]);

                       }

                   }

                   else {

                        // Clear contract field if no contract is associated with account

                       formContext.getAttribute("msdyn_project").setValue("");

                   }

               },

               function (error) {

                   console.log(error.message);

               }

           );

       } else {

           // Clear contract field if account is set to be null

           formContext.getAttribute("msdyn_project").setValue("");

       }

    }

  • Suggested answer
    Linn Zaw Win Profile Picture
    3,407 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    Here is the WebAPI query for Xrm.WebApi.retrieveMultipleRecords. You can use FetxhXML Builder tool to get the Web API query too.

    "?$select=msdyn_subject&$filter=_msdyn_customer_value eq '"   accountId   "'"

    One more thing is there is no double quote required for clearing the lookup field value

    formContext.getAttribute("msdyn_project").setValue();

    Here is the complete function

    function new_populateContract(executionContext)
    {
    	var formContext = executionContext.getFormContext();
    	if (formContext.getAttribute("new_account").getValue() !== null)
    	{
    		// Get account id
    		var accountId = formContext.getAttribute("new_account").getValue()[0].id;
    		accountId = accountId.replace(/\{|\}/gi, "").toLowerCase();
    		// Retrieve associated contract of account
    		Xrm.WebApi.retrieveMultipleRecords("msdyn_project", "?$select=msdyn_subject&$filter=_msdyn_customer_value eq '"   accountId   "'").then(
    		function success(result)
    		{
    			if (result.entities.length > 0)
    			{
    				formContext.getAttribute("msdyn_project").setValue([ { id: result.entities[0].msdyn_projectid, name: result.entities[0].msdyn_subject, entityType: "msdyn_project" } ]);
    			}
    			else
    			{
    				// Clear contract field if no contract is associated with account
    				formContext.getAttribute("msdyn_project").setValue();
    			}
    		},
    		function (error)
    		{
    			console.log(error.message);
    		});
    	}
    	else
    	{
    		// Clear contract field if account is set to be null
    		formContext.getAttribute("msdyn_project").setValue();
    	}
    }

  • Aneeqa Pervaiz Profile Picture
    240 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    Hello.

    I am still getting an error when I use the code above.

    TE-QC.PNG

    Please tell me what I am missing now. I assumed that the line - Retrieve Multiple Records - was fixed. Please correct me if it wasn't. 

    Thanks. 

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    Hi Aneeqa,

    You can achieve it by JS.

    Adding JS in quick create is similar to main form, you just need to go to quick create form and add your js over there and select event , add function.

    1.write JS code.

    There is a N:1 relationship between Contract and Account, so if you choose an account, it will find more than one contract records., so I choose the top 1 record to populate field in my sample code.

    Also, the contract entity is changed with contact entity because I do not have contract records, so you can change then with fields and entity that you need when you test code.

    function populatelookupfield(executionContext) {
        var formContext = executionContext.getFormContext();
        var account = formContext.getAttribute("new_accountnameid").getValue();
        var accountid = account[0].id.replace('{', '').replace('}', '');
        Xrm.WebApi.retrieveMultipleRecords("msdyn_project", "?$select=msdyn_subject&$filter=_msdyn_customer_value eq "   accountid   "").then(
            function success(result) {
                var contractValue = new Array();
                contractValue[0] = new Object();
                contractValue[0].id = result.entities[0].msdyn_projectid; 
                contractValue[0].name = result.entities[0].msdyn_subject; 
                contractValue[0].entityType = "msdyn_project"; 
                formContext.getAttribute("msdyn_project").setValue(contractValue);
            },
            function(error) {
                console.log(error.message);
            }
        );
    }

    2. Add your JavaScript code in a Script web resource.

    34038.png

    3. Associate Script web resource to a form.

    (1) Go to settings > Customization > Customize the system > entity.

    (2) Select Quick Create Form.

    0880.png

    (3) Add the Script web resource in the Form Properties.

    6428.png

    remember to check the "Pass execution..." option, 

    otherwise we will receive the error: cannot read property 'getformcontext' of undefined.

    (The first line of code: executionContext.getFormContext();)

    3157.png

    Regards,

    Clofly

  • Verified answer
    Linn Zaw Win Profile Picture
    3,407 on at
    RE: Auto-populating values in Quickcreate form using Mapping

    If you are getting "Cannot read property 'getFormContext' of undefined" error, please make sure that you check "Pass execution context as first parameter" checkbox in the event handler properties.

    pastedimage1593584512029v1.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 193

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 70

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 69 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans