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

Announcements

No record found.

News and Announcements icon
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)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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.

  • Frosty8467 Profile Picture
    50 on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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 + ")"

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Frosty8467 Profile Picture
    50 on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans