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 CRM (Archived)

Create Child records for an existing Parent Record

(0) ShareShare
ReportReport
Posted on by 50

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.

*This post is locked for comments

I have the same question (0)
  • Frosty8467 Profile Picture
    50 on at
    RE: Create Child records for an existing Parent Record

    Goutam, Ravi and Aric.

    Thanks Guys you are legends.

    Goutam - you were right the code needs the Schema name which has capitals in it

    Aric - you were correct about the plural name.

    So thanks guys for helping me out you have no idea how much I appreciate your help.

    Matt

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: Create Child records for an existing Parent Record

    Please also make sure that you are using the plural name of the related entity. Your code says:

    "new_qualifyfunctionid@odata.bind":"/new_clockworxqualifying(" + new_qualifying_functionsid + ")"

    Shouldn't plural name for new_clockworxqualifying be new_clockworxqualifyings?

    Please check your entity definition what the plural entity name is and modify accordingly.

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Create Child records for an existing Parent Record

    Hi ,

    Could you please confirm "new_qualifyfunctionid" field exists in your child entity.

    Seems the id you have mentioned not found in the  child entity. Please recheck.

    "new_qualifyfunctionid@odata.bind":"/new_clockworxqualifying(" + new_qualifying_functionsid + ")"

  • Frosty8467 Profile Picture
    50 on at
    RE: Create Child records for an existing Parent Record

    Thanks for the quick replies I have tried your suggestion but got this error returned trying to add the records now.

    Field2.PNG

    var new_qualifying_functionsid= Xrm.Page.data.entity.getId();
    var new_name = new_category + " " + new_taskname
    var objQualifyingFunction = {
    "new_function": new_taskname,
    "new_category": new_category,
    "new_name": new_name,
    "new_qualifyfunctionid@odata.bind":"/new_clockworxqualifying(" + new_qualifying_functionsid + ")"
    }
    Xrm.WebApi.createRecord("new_clockworxqualifyingfunction", objQualifyingFunction )

    Table Names 

    Parent: new_clockworxqualifying

    Child: new_clockworxqualiyingfunction

    Hope you can help I am obviously missing something.

    Here is a copy of the 1:N relationship 

    8182.Relationship.PNG

    And the field in the entity

    7080.Field.PNG

    Field2.PNG

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Create Child records for an existing Parent Record

    Hi ,

    You need to pass lookup id below way here-

    var objQualifyingFunction = {
    "new_function": new_taskname,
    "new_category": new_category,
    "new_name": new_name
    "new_PrimaryIncident@odata.bind":"/incidents(10000000-0000-0000-0000-000000000000)" //Need to pass like this replace lookup id and entity name
    
    }

    Also refer below url -

    community.dynamics.com/.../223433

    https://www.inogic.com/blog/2016/04/set-values-of-all-data-types-using-web-api-in-dynamics-crm-through-c/

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Create Child records for an existing Parent Record

    Hi,

    Where are you setting the value to associate your child to the parent? Ther must be a lookup in your child entity which you need to set when you create a child record.

    Something like this "primarycontactid@odata.bind":

    =============

    var data =

       {

           "name": "Sample Account",

           "primarycontactid@odata.bind": "/contacts(465b158c-541c-e511-80d3-3863bb347ba8)"

       }

    =========

    Hope this helps.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans