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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create related record using javascript

(0) ShareShare
ReportReport
Posted on by 452

This is my first bit of code written for dynamics crm.

I started with this sample of how to create an account.  Worked like a charm.

https://szuyublog.wordpress.com/2016/02/22/microsoft-dynamics-crm-2016-web-api-create-an-entity-using-http-post-request/

Then I copied the working code and am trying to create a record in a different entity but of course it is not working.

Can someone tell me how to put error handling in so I know what the issue is.

I have psa installed and trying to create a record in msdyn_contractlinescheduleofvalue

I get all the values I want to insert from the salesorder/salesorderdetailid and then try to create the record

it never goes past this line of code     req.onreadystatechange = function ()

Then it returns a message box that says [Object Object]

How do I learn what I am doing wrong?

Here is my code:

function getSalesOrderIdthenCreatecontractlinescheduleofvalue() {
if(Xrm.Page.ui.getFormType() !==1 ) {
debugger;

//get all the values needed to create record

var contractId = Xrm.Page.getAttribute('salesorderid').getValue()[0].id;
var contractline = Xrm.Page.data.entity.getId();
var invoicedate = new Date();
var invoicestatus = '192350000';
var linename = "test";
var transactionclassification = '192350003';
var transactiontypecode = '192350004';


var entityType = "msdyn_contractlinescheduleofvalue";
var clientURL = Xrm.Page.context.getClientUrl();
var clsv = {};
clsv["msdyn_contract"] = contractId;
clsv["msdyn_contractline"] = contractline;
clsv["msdyn_name"] = linename;
clsv["msdyn_invoicedate"] = invoicedate;
clsv["msdyn_invoicestatus"] = invoicestatus;
clsv["msdyn_transactionclassification"] = transactionclassification;
clsv["msdyn_transactiontypecode"] = transactiontypecode;

CreateEntity(clientURL, entityType, clsv);
}
}

function CreateEntity(clientURL, entityType, entityData) {

var req = new XMLHttpRequest();
req.open("POST", encodeURI(clientURL + "/api/data/v8.2/" + entityType) , true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {                                           ///never goes into this function

       if (this.readyState == 4 /* complete */) {
           req.onreadystatechange = null; //avoids memory leaks


           if (this.status == 204) {
               var entityUri = this.getResponseHeader("OData-EntityId");
               alert(entityUri);

               console.log("Created " + entityType + " with URI " + entityUri);

           }

           else {

               var error = JSON.parse(this.response).error;
               alert(error);
               console.log(error.message);

           }

       }

   };

   req.send(JSON.stringify(entityData));


}

*This post is locked for comments

I have the same question (0)
  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Try checking what the value of this.response is before your parse it.

    Since you have your debugger window open, put a breakpoint there, and see what the actual error is.

    All [Object object] means is that your result is an object with children, so you can try and see the children there and pinpoint the actual error.

  • FallingThroughTheCracks Profile Picture
    452 on at

    I put this in at several places and it always returns "undefined"

    Does this mean something?

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

    Possible that the req.send throws an exception.

    Can you validate your data before your reach that point?

    The one thing that I see is that you are creating the following values as string but they should be integers.

    var invoicestatus = '192350000';

    var transactionclassification = '192350003';

    var transactiontypecode = '192350004';

    Change them to

    var invoicestatus = 192350000;

    var transactionclassification = 192350003;

    var transactiontypecode = 192350004;

    Validate the other data, and put a breakpoint in the following line:

    if (this.readyState == 4 /* complete */)

    Let me know...

  • FallingThroughTheCracks Profile Picture
    452 on at

    ok now I have an actual error.

    this.status = 404 and the error is

    error.message "Resource not found for the segment 'msdyn_contractlinescheduleofvalue'."

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

    Check if this helps you out, but not sure...

    community.dynamics.com/.../unknown-error-or-resource-not-found-for-the-segment-msdyn-fpsaction-in-schedule-board-in-field-service-in-dynamics-365

    Was there anything else in the response besides the error?

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans