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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Create 2 records in Javascript with one of the records related to the other

(0) ShareShare
ReportReport
Posted on by 120

Hello guys,

I'm trying to implement the below scenario using Javascript: I want to make a function that creates a record (let's say for table A) and then one more record (for a different table, let's say B) which has a lookup field to the first one. All in one go. I am using two Xrm.WebApi.createRecord functions, the 2nd one inside the success callback of the 1st one. My problem is that after the 1st insert , my code cannot retrieve/find it so I can relate it during the second insert.

Any ideas?

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hello,

    in success callback use result.id

    refer to this example:

    docs.microsoft.com/.../createrecord

  • Giannis Tsak Profile Picture
    120 on at

    I'm doing that. That's the way I'm trying to bind it.

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Please share your code.

  • Giannis Tsak Profile Picture
    120 on at

    function AddInvoiceHeader(arrIds,entitlementid)
    {	
    	var data =
    		{
    			"new_name": "Case Invoice Header",
    			//"new_entitlementid@odata.bind" : "/entitlements("  entitlementid  ")"
    		}
    	
    	Xrm.WebApi.createRecord("new_caseinvoice", data).then(
    		function success(result) {
    			setTimeout(function(){ AddInvoiceLines(arrIds, result.id); }, 3000);
    		},
    		function (error) {
    			console.log(error.message);
    		}
    	);		
    }
    
    function AddInvoiceLines(arrIds, caseinvid)
    {
    	for (var i = 0; i < arrIds.length; i  ) 
    	{  								
    		var caseid = arrIds[i];		
    		var data =
    		{
    			"new_name": "Case Invoice Line",
    			"new_caseinvoiceid@odata.bind": "/new_caseinvoices("  caseinvid  ")",
    			"new_incidentid@odata.bind": "/incidents("  caseid  ")"		
    		}
    		Xrm.WebApi.createRecord("new_caseinvoiceline", data).then(
    		function success(result) {
    			console.log("Line Created");
    			Xrm.Utility.alertDialog("Line Created");
    		},
    		function (error) {
    			console.log(error.message);
    
    		}
    		);			
    	} 
    }

    ArrIds is an array of incident type guids. The 2 tables I'm using is new_caseinvoice and new_caseinvoiceline and I'm trying to create the typical header-lines format.

  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hello,

    Maybe something is wrong with NavigationPropertyBinding.

    Take a look to my code, I try to create a contact and bind with an account:

    var data =
    {
        "fistname": "contact mel",
        "lastname": "Line",
        "parentcustomerid_account@odata.bind": "/accounts(3f749846-ca1b-ea11-a812-000d3a337847)",
    }
    
    
    Xrm.WebApi.createRecord("contact", data).then(
        function success(result) {
            console.log(result);
        },
        function (error) {
            console.log(error.message);
        }
    );
    

    So the trick to find the correct NavigationPropertyBinding for the contact entity go to:

    https://YourOrganisation.crm.dynamics.com/api/data/v9.0/$metadata#contact/$entity and go to your EntitySet nood, in my case it's contact.

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    I found the right NavigationPropertyBinding path:

    <NavigationPropertyBinding Path="parentcustomerid_account" Target="accounts"/> --> parentcustomerid_account that I use in my JS code.

    Hope this helps

  • Giannis Tsak Profile Picture
    120 on at

    Thanks for your reply Mehdi.

    I've checked the NavigationPropertyBinding and it is okay.

    I replaced the body of the second function with a simple record retrieve of the result.id and it doesn't seem to find it. It's like the header record at the time the 2nd function runs, has not been created yet.

  • meelamri Profile Picture
    13,216 User Group Leader on at

    Please remove the alertDialog from your code. Run again and share your console output.

  • Giannis Tsak Profile Picture
    120 on at

    I've re-created the tables and I re-wrote the script from scratch + got rid of the alert messages and the timeout function and now it works. I can only assume that it indeed had something to do with the NavigationPropertyBinding as you mentioned and I missed something during my check. Regardless, thanks for the help.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans