Skip to main content

Notifications

Announcements

No record found.

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

Create a Record in Javascript in CRM 2015

Posted on by 25

Hi Guys, 

I'm sure there is already an old thread for this, but for some reason i can't seem to get the search on the website to narrow down to what i'm lookign for. 

I'm faced with an older CRM 2015 version on prem. 

I need to create a simple javascript function that will take information from the current "on page" record and create a new record from in a different entity.

I know i need to use XMLHttprequest but i don't know how to do it. 

Can someone help with this ?

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Create a Record in Javascript in CRM 2015

    Make sure to close this thread.

  • RE: Create a Record in Javascript in CRM 2015

    Thanks Guys!

    I used the RESt Builder!

    What a fantastic tool!!!

  • Verified answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Create a Record in Javascript in CRM 2015

    Hi,

    In addition to that you can also download Rest Builder which will help you to write request for you, You can configure which field you want to set and then generate query for the same.

    github.com/.../CRMRESTBuilder

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Create a Record in Javascript in CRM 2015

    Hi whydoihavetocreateanotherlogin,

    This article explains how to create a record using OData and jQuery in Dynamics CRM 2015. You will need to edit the JavaScript to pass your desired parameters into the createRecord function. It will only take a small amount of editing to meet your requirement.

    // This function creates record by making OData call
    function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
        //Parse the entity object into JSON
        var jsonEntity = window.JSON.stringify(entityObject);
        // Get Server URL
        var url = "";
        if (Xrm.Page.context.getClientUrl) {
            //Post UR 12
            url = Xrm.Page.context.getClientUrl();
        }
        else {
            //Pre UR 12
            url = Xrm.Page.context.getServerUrl();
        }
        //The OData end-point
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        //Asynchronous AJAX function to Create a CRM record using OData
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: url   ODATA_ENDPOINT   "/"   odataSetName,
            data: jsonEntity,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (successCallback) {
                    successCallback(data.d, textStatus, XmlHttpRequest);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                if (errorCallback) {
                    errorCallback(XmlHttpRequest, textStatus, errorThrown);
                }
                else {
                    alert("An error has occured while creating the record; Error – "   errorThrown);
                }
            }
        });
    }

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