cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Javascript create record with lookup in Model Driven app

Hello,

 

I'm building a model driven app and want to create an associated record in a different table when a button is clicked in a form. 

 

I have the button in the form working. My Javascript gets called and I can create a record in another table. Where I come unstuck is attempting to add the lookup field in the new record back to this existing record. I've attempted to do so via an odata bind, but I suspect I've messed this up. 

 

Some background info:

-My current form is in table cr2d2_projectlist. The primary field in this table is cr2d2_projectname. The GUID for the record is 9EA72A21-4DC0-EB11-BACC-002248151FC1.

-The table I'm adding the record to is cr2d2_projectapprovals. The primary field in this table is cr2d2_name. The field that is a lookup back to the project list is cr2d2_associatedproject.

 

Here is the JS, commented out so I could post this.

//  try {

//    var entityName = "cr2d2_projectapprovals"; //Set the table name

//    //setup the JSON for creating the new record. It works if the second field isn't in the JSON

//    var data = {

//      "cr2d2_name": "Some name",

//      "cr2d2_associatedproject@odata.bind": "/cr2d2_projectlist(9EA72A21-4DC0-EB11-BACC-002248151FC1)" }
//    Xrm.WebApi.createRecord(entityName, data).then(
//      function success(result) { Xrm.Utility.alertDialog("Success"); },
//      function (error) { Xrm.Utility.alertDialog("Error: "+error.message); }
//    );
//  }
//   catch (e) { Xrm.Utility.alertDialog(e.message); }
//  }

 

 

 

 

I presume it is something basic. I keep getting odata errors back, I've tried several format options for getting the related field data in but am unsure how to proceed. Any ideas?

 

Damian.

 

3 REPLIES 3
Mira_Ghaly
Dual Super User
Dual Super User

@Anonymous 

Your Main Table is Project List and this entity has a 1: N Relation with another Table Project Approvals.

From Project List you want to add a new Project Approval is this correct?

 

If this post helps you with your problem, please mark your as Accepted solution.If you like my response, please give it a Thumbs Up.

Blog: here
Anonymous
Not applicable

Hello,

 

I want to add a record in my Project Approvals Table. There is a field in the Project Approvals table called "Associated Project", which is a lookup Field back to the Project List table. The "Associated Project" field is the one I am struggling to populate.

 

Regards,

Damian.

Mira_Ghaly
Dual Super User
Dual Super User

@Anonymous 

I have simulated your structure at my side and I would like to suggest a simpler approach to achieve what you want.

 

On your Project List Form ; Add a new Component Sub Grid and Select Project approvals -> Save and Publish Your form and then you can Add Project Approvals as below:

Mira_Ghaly_0-1622368703196.png

Let me know if this idea works for you or Not , but the idea is to make use of the out of the box functionality as much as possible!

IF you want the Javascript

//      "cr2d2_associatedproject@odata.bind": "/cr2d2_projectlist(9EA72A21-4DC0-EB11-BACC-002248151FC1)" }

 

Replace with.. 

      "cr2d2_AssociatedProject@odata.bind": "/cr2d2_projectlist(9EA72A21-4DC0-EB11-BACC-002248151FC1)" }

 

 

If this post helps you with your problem, please mark your as Accepted solution.If you like my response, please give it a Thumbs Up.

Blog: here

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Top Kudoed Authors
Users online (1,803)