I am trying to create child records for an existing parent entity using java script but having no luck.
On a form for the parent entity (new_clockworxqualifying) when a user clicks a certain option I want the system to retrieve all the records for a third entity (new_clockworxsubtask) and add them to my child entity (new_clockworxqualifyingfunction). I can do all this except create the relationship between the parent entity and the child entities. I end up with a lot of orphan entities.
All three tables are custom entities if that makes a difference. I have also created a 1:N relationship between the Parent and the Child.
Here is the code I am calling at on_change event of the field on the form
function RetrieveAllSubTaskRecords() {
Xrm.WebApi.retrieveMultipleRecords("new_clockworxsubtask", "?$select=new_category,new_taskname").then(
function success(result) {
for (var i = 0; i< result.entities.length; i++) {
CreateNewQualifyingFunction(result.entities[i].new_category,result.entities[i].new_taskname);
}
},
function (error) {
// Handle error conditions
Xrm.Utility.alertDialog(error.message, null);
});
}
function CreateNewQualifyingFunction(new_category,new_taskname){
var outputText = "Category\t\t\tTask Name\n---------------------------------------------------\n";
outputText += new_category+ "\t\t" + new_taskname + "\n";
//Xrm.Utility.alertDialog(outputText, null);
var new_qualifying_functionsid= Xrm.Page.data.entity.getId();
var ParentName = Xrm.Page.getAttribute("new_name").getValue();
var new_name = new_category + " " + new_taskname;
var objQualifyingFunction = {
"new_function": new_taskname,
"new_category": new_category,
"new_name": new_name
}
Xrm.WebApi.createRecord("new_clockworxqualifyingfunction", objQualifyingFunction ).then(
function success(result) {
alert("clockworx qualifying function created with ID: " + result.id);
// perform operations on record creation
},
function (error) {
alert(error.message);
// handle error conditions
}
);
}
Any Help will be greatly appreciated.