Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Javascript : variable undefined

Posted on by 75

Hello guys,

Need your help to lead me what's wrong with my JavaScript code. Forgive me because I have no experience in JavaScript, I only get this from the web with addition of my needed.

function CreateRecord() {

var globalContext = Xrm.Utility.getGlobalContext();
var userId = globalContext.userSettings.userId;
var today = new Date();

try {
var entityName = "xyz_orders"; //Entity Logical Name

//Default company
Xrm.WebApi.online.retrieveRecord("systemuser", userId, "?$expand=cdm_Company($select=cdm_companyid,cdm_name)").then(
function success(result) {
if (result.hasOwnProperty("cdm_Company")) {
var cdm_Company_cdm_companyid = result["cdm_Company"]["cdm_companyid"];
var cdm_Company_cdm_name = result["cdm_Company"]["cdm_name"];
var companyLookup = new Array();
companyLookup[0] = new Object();
companyLookup[0].id = cdm_Company_cdm_companyid;
companyLookup[0].name = cdm_Company_cdm_name;
companyLookup[0].entityType = "cdm_company";
var companycode = companyLookup;
}
},
function(error) {
console.log(error.message);
}
);


//Data used to Create record
var data = {
"xyz_description": "New order",
"xyz_company": companycode,
"xyz_date": today
};
Xrm.WebApi.createRecord(entityName, data).then(
function success(result) {
Xrm.Utility.alertDialog("Success");
},
function (error) {
Xrm.Utility.alertDialog("Error creating header");
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}

The error is actually what I highlighted in red, companycode. It seems when it is run, it is said company code is not defined. I thought to defined variable is only adding "var" in front of it ?

Your kind help is appreciated.

Thanks

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Javascript : variable undefined

    Hi Pradeep,

    Finally, it is succeeded. 

    Thank you very much for your patience guiding me through all this..

    And about the tool : CRM Rest Builder, looks great although I need time to learn how to use that with all the choices. Thank you for your suggestion, Mehdi. I will search in YouTube whether there is some good tutorial for me.

    Thank you all, God Bless.

  • Verified answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Javascript : variable undefined

    Hi,

    Please check now with below code:

    function CreateRecord() {
    
       var globalContext = Xrm.Utility.getGlobalContext();
    
       var userId = globalContext.userSettings.userId;
    
       var today = new Date();
    
       try {
    
           var entityName = "jm_expense"; //Entity Logical Name
    
           //Default company
    
           Xrm.WebApi.online.retrieveRecord("systemuser", userId, "?$expand=cdm_Company($select=cdm_companyid,cdm_name)").then(
    
               function success(result) {
    
                   if (result.hasOwnProperty("cdm_Company")) {
    
                       var cdm_Company_cdm_companyid = result["cdm_Company"]["cdm_companyid"];
    
                       var cdm_Company_cdm_name = result["cdm_Company"]["cdm_name"];
    
                       var companyLookup = new Array();
    
                       companyLookup[0] = new Object();
    
                       companyLookup[0].id = cdm_Company_cdm_companyid;
    
                       companyLookup[0].name = cdm_Company_cdm_name;
    
                       companyLookup[0].entityType = "cdm_company";
    
                       var companycode = companyLookup;
    
                       //Data used to Create record
                       
                       var data = {
    
                           "jm_description": "New order",
    
                           "jm_date": today
    
                       };
    
                       if (cdm_Company_cdm_companyid) {
                           data["jm_Company@odata.bind"] = "/cdm_companies("   cdm_Company_cdm_companyid   ")";
                       }
    
                       Xrm.WebApi.createRecord(entityName, data).then(
    
                           function success(result) {
    
                               Xrm.Utility.alertDialog("Success");
    
                           },
    
                           function (error) {
    
                               Xrm.Utility.alertDialog("Error saving header");
    
                           }
    
                       );
    
                   }
    
               },
    
               function (error) {
    
                   console.log(error.message);
    
               }
    
           );
    
       }
    
       catch (e) {
    
           Xrm.Utility.alertDialog(e.message);
    
       }
    
    }

    Thanks,
    Pradeep

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Javascript : variable undefined

    Do not hesitate to use the CRM REST Builder tool, it can be useful to correct the error

  • Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Javascript : variable undefined

    Hi,

    Can you please try by comment lookup object code and then check?

    With this we will get idea that we are getting error for lookup field.

    Thanks,

    Pradeep

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Javascript : variable undefined

    Hi MedWong,

    I'm glad you were able to solve your problem.

    There is another way to generate this kind of code easily. For my part, I use the CRM REST Builer tool. It saves my life every time I want to generate my queries.

    Please refer to this blog: www.wipfli.com/.../tc-crm-rest-builder-dynamics-crms-web-api

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Javascript : variable undefined

    Hi,

    Unfortunately, it is still went to my custom error, like in my previous message.

    Here is my full script, wondering where am I doing it wrongly :

    function CreateRecord() {

       var globalContext = Xrm.Utility.getGlobalContext();

       var userId = globalContext.userSettings.userId;

       var today = new Date();

       try {

           var entityName = "jm_expense"; //Entity Logical Name

           //Default company

           Xrm.WebApi.online.retrieveRecord("systemuser", userId, "?$expand=cdm_Company($select=cdm_companyid,cdm_name)").then(

               function success(result) {

                   if (result.hasOwnProperty("cdm_Company")) {

                       var cdm_Company_cdm_companyid = result["cdm_Company"]["cdm_companyid"];

                       var cdm_Company_cdm_name = result["cdm_Company"]["cdm_name"];

                       var companyLookup = new Array();

                       companyLookup[0] = new Object();

                       companyLookup[0].id = cdm_Company_cdm_companyid;

                       companyLookup[0].name = cdm_Company_cdm_name;

                       companyLookup[0].entityType = "cdm_company";

                       var companycode = companyLookup;

                       //Data used to Create record

                       var data = {

                           "jm_description": "New order",

                           "jm_company@odata.bind":  "/cdm_companies(" + cdm_Company_cdm_companyid+")",

                           "jm_date": today

                            };

                       Xrm.WebApi.createRecord(entityName, data).then(

                           function success(result) {

                               Xrm.Utility.alertDialog("Success");

                           },

                           function (error) {

                               Xrm.Utility.alertDialog("Error saving header");

                           }

                       );

                   }

               },

               function (error) {

                   console.log(error.message);

               }

           );

       }

       catch (e) {

           Xrm.Utility.alertDialog(e.message);

       }

    }

    Thanks,

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Javascript : variable undefined

    Hi,

    Thank you for your quick response. Found it.

    Here is my finding :

    Schema name : jm_company

    pastedimage1632901992106v1.png

    ---> This is from my entity's field which is the target of created record.

    pluralentityname : cdm_companies

    pastedimage1632902215744v2.png

    ---> This is the table cdm_company 

    So with that information, whether this is correct then ?

    var data = {
    "jm_description": "New order",
    "jm_company@odata.bind":  "/cdm_companies(" + cdm_Company_cdm_companyid+")",
    "jm_date": today
    };

    Thanks

  • Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Javascript : variable undefined

    Hi,


    Please search as shown in below screen:

    pastedimage1632901484216v1.png

    After this you need connect to CRM.

    Thanks,
    Pradeep.

  • MedWong Profile Picture
    MedWong 75 on at
    RE: Javascript : variable undefined

    Hi,

    I'm sorry, for SchemaName, now I have located it and know exactly what I supposed to use. However for Xrmtoolbox, this is something new to me. I just download it, but may advice which tool I should install ? Because it look like I didn't have Xrmtoolbox for Microsoft Dataverse and Dynamics 365

    .pastedimage1632901018200v1.png

    Thanks,

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Javascript : variable undefined

    //Data used to Create record
    var data = {
    "xyz_description": "New order",
    "xyz_company": companycode,
    "xyz_date": today
    };
    Xrm.WebApi.createRecord(entityName, data).then(
    function success(result) {
    Xrm.Utility.alertDialog("Success");
    },
    function (error) {
    Xrm.Utility.alertDialog("Error creating header");
    }
    );

    In above code, the "xyz_company" is of lookup type and to set lookup value during creation of record we need to follow below syntax:

    "schemaNameofLookup@odata.bind"="/pluralentityName(GUID)";

    To get shemaname we need to go to field customization and there we will find schema name column as shown below:

    pastedimage1632891494244v1.png


    And to get puralname of entity then open Xrmtoolbox and serach metadata browser then select entity and get value from entitysetname column as shown below:

    pastedimage1632891701602v3.png

    Now, once we have both then during record i will write code in below way:
    new_funds@odata.bind="/fundses(recordGUID)";

    So, In your code check the schema and entity plural name.

    Hope this helps.

    Thanks,
    Pradeep.

    Please mark this as VERIFIED. if it 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans