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.
@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?
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.
@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:
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)" }
User | Count |
---|---|
252 | |
107 | |
88 | |
51 | |
44 |