Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create record using sdk.rest.js

Posted on by 12,070 Super User 2024 Season 1

Hi all,

I am creating record from js. I used the following code:

function Configuration() {
debugger;

var account = {};
account.Name = "Test Account Name";
account.Description = "This account was created by the JavaScriptRESTDataOperations sample.";

//Set a picklist value

account.PreferredContactMethodCode = { Value: 2 }; //E-mail

//Set a money value

account.Revenue = { Value: "2000000.00" }; //Set Annual Revenue

//Set a Boolean value

account.DoNotPhone = true; //Do Not Allow

//Add Two Tasks
var today = new Date();
var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3); //Set a date three days in the future.

var LowPriTask = { Subject: "Low Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 0} }; //Low Priority Task
var HighPriTask = { Subject: "High Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 2} }; //High Priority Task
account.Account_Tasks = [LowPriTask, HighPriTask]

//Create the Account
SDK.REST.createRecord(
account,
"Account",
function (account) {
alert("Test");
},
errorHandler
);
}
function errorHandler(error) {
alert("Test from error handler");
}

and It is giving me below error:

CallJSfromHtml.png

What is the mistake in my code?

Thank You

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Create record using sdk.rest.js

    Hi Abdul Wahab,

    Try to add the scripts as shown below.

       <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
       <script src="../Json2.js" type="text/javascript"></script>
       <script src="../SDK.REST.js" type="text/javascript"></script>
       <script src="../createAndUpdateConfiguration.js" type="text/javascript"></script>

    Hope this helps.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Create record using sdk.rest.js

    Here is how your html should look like:

    <!DOCTYPE html>
    <html>
    <head>
       <meta charset="utf-8" />
       <title>Formstack and Quote 2 Cart Integration With CRM</title>
       <meta charset=utf-8>
        <script src="../../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
       <script src="../Scripts/Json2.js" type="text/javascript"></script>
       <script src="../Scripts/SDK.REST.js" type="text/javascript"></script>
       <script src="../Scripts/createAndUpdateConfiguration.js" type="text/javascript"></script>
    </head>
    <body  contentEditable=true bgColor=#ffffff>
       <table>
           <tbody>
               <tr>
                   <td><font color=#000000>AW :</font></td>
                   <td><input size="45" id=AW ></td>
               </tr>
                   <td>
                       <button onclick="Configuration()"> Set </button>
                   </td>
           </tbody>
       </table>
    </body>
    </html>


    That should work. And for your understaing how it works check this - www.webdevelopersnotes.com/relative-and-absolute-urls

  • Verified answer
    Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: Create record using sdk.rest.js

    If you are using absolute path for all the .js files (like SDK.REST.js), that should be fine as it was in the context before, so you can leave it as before (no change), for testing.

    It is available in the path

    /WebResources/ClientGlobalContext.js.aspx

    Depends upon your html resource you need to change the ClientGlobalContext.js.aspx (relative) path.

    After testing change everything to relative path.

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Create record using sdk.rest.js

    Hi Andrii

    When I used as you suggested in point 1 I have the following error:

    CallJSfromHtml1.png

    This is my html address: http://win-AW:801/AW/WebResources/aw_/Pages/FormStackCart2QuoteIntegrationConfigurationPage.html

    Thank You

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Create record using sdk.rest.js

    1. Don't use absolute urls. Instead of using "win-aw/.../SDK.REST.js&quot; in src you should use something like "aw_/Scripts/SDK.REST.js".

    2. What's address of your Html form?

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Create record using sdk.rest.js

    Hi Andrii Gopalan

    This is my code:

    <!DOCTYPE html>

    <html lang="en" xmlns="www.w3.org/.../xhtml&quot;>

    <head>

       <meta charset="utf-8" />

       <title>Formstack and Quote 2 Cart Integration With CRM</title>

       <meta charset=utf-8>

        <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>

       <script src="win-aw/.../Json2.js&quot; type="text/javascript"></script>

       <script src="win-aw/.../SDK.REST.js&quot; type="text/javascript"></script>

       <script src="win-aw/.../createAndUpdateConfiguration.js&quot; type="text/javascript"></script>

    </head>

    <body  contentEditable=true bgColor=#ffffff>

       <table>

           <tbody>

               <tr>

                   <td><font color=#000000>AW :</font></td>

                   <td><input size="45" id=AW ></td>

               </tr>

                   <td>

                       <button onclick="Configuration()"> Set </button>

                   </td>

           </tbody>

       </table>

    </body>

    </html>

    I add this reference.

    Thank You

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Create record using sdk.rest.js

    Abdul,

    Looks like you forgot to add ClientGlobalContext reference.

    Add following script reference to your file:

    <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>

  • Suggested answer
    Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: Create record using sdk.rest.js

    Please refer this link

    https://community.dynamics.com/crm/f/117/t/214204

    msdn.microsoft.com/.../gg328541.aspx

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Create record using sdk.rest.js

    Hi Andrii

    I used it in my Html Webresource.

    Thank You

  • Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Create record using sdk.rest.js

    Hi Gopalan,

    I run it from my html file.

    Thank You

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans