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 a Record from Javascript using web api query

(0) ShareShare
ReportReport
Posted on by 6,211

Hi All, 

I want to create a new record from the below two query, when i set the lookup value i am facing error, but when i use query with out lookup field it works fine. i have check the schema name, below are two was i have check,i am having system admin rights.

1) 

var entity = {};
entity.name = "test";
entity["new_marketid@odata.bind"] = "/new_markets(xxxxxxxx-EF23-E411-A48E-xxxxxxxxx)";

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts",
    data: JSON.stringify(entity),
    beforeSend: function(XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
        XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: true,
    success: function(data, textStatus, xhr) {
        var uri = xhr.getResponseHeader("OData-EntityId");
        var regExp = /\(([^)]+)\)/;
        var matches = regExp.exec(uri);
        var newEntityId = matches[1];
    },
    error: function(xhr, textStatus, errorThrown) {
        Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
    }
});


2)

var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = "xxxxxxxx-EF23-E411-A48E-xxxxxx";
lookupValue[0].name = "A&E";
lookupValue[0].entityType = "new_markets";
var entity = new XrmServiceToolkit.Soap.BusinessEntity("account");
entity.attributes["name"] = "test"; 
entity.attributes["new_marketid"] = lookupValue;
var guid = XrmServiceToolkit.Soap.Create(entity);

Both the code not working when using lookup value.

Any help would be appreciated.

Regards,

Shahbaaz

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Priyesh Profile Picture
    7,396 User Group Leader on at

    Hi,

    If you are on D365 v9, use this - docs.microsoft.com/.../xrm-webapi

    It's simpler.

    Hope this works.

  • necsa Profile Picture
    3,455 on at

    Hi Shahbaaz,

    Please get as reference following solution

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

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

    1) Seems your code is correct , only thing make sure you provided correct lookup name  and the entity name with additional "s". If the entity name is "new_markets" then put "new_marketss"

    var entity = {};
    entity.name = "test";
    entity["new_marketid@odata.bind"] = "/new_marketss(xxxxxxxx-EF23-E411-A48E-xxxxxxxxx)";
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts",
        data: JSON.stringify(entity),
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function (data, textStatus, xhr) {
            var uri = xhr.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        },
        error: function (xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });
    


    2) Try with this - You need to add "Set" after entity name .

    Using SOAP

    ===================

    var lookupValue = new Array();
    lookupValue[0] = new Object();
    lookupValue[0].id = "xxxxxxxx-EF23-E411-A48E-xxxxxx";
    lookupValue[0].name = "A&E";
    lookupValue[0].entityType = "new_marketsSet";
    var entity = new XrmServiceToolkit.Soap.BusinessEntity("account");
    entity.attributes["name"] = "test";
    entity.attributes["new_marketid"] = lookupValue;
    var guid = XrmServiceToolkit.Soap.Create(entity);
    


    Using REST
    =========================== var json = {}; jsonDT.new_marketid = { Id: "xxxxxxxx-EF23-E411-A48E-xxxxxx", LogicalName: "new_marketsSet", Name: "A&E" }; json.name = "test"; XrmServiceToolkit.Rest.Create( jsonCOI, "new_marketsSet", function (result) { }, function (error) { alert("error"); }, false );


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

    You can check here sample code for all type of field  -

    www.inogic.com/.../set-values-of-all-data-types-using-web-api-in-dynamics-crm

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    Thanks Necdet and Wagh for the reply, still facing same issue.

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    Thanks Goutam for the response, entity name is "new_market" and when i used it in the query i passed it as "new_markets" only, but still no luck with all the request above

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    Used below code also, no luck,

    var entity = {};

    entity.name = "tets";

    entity["new_marketid@odata.bind"] = "/new_markets(xxxxx-EF23-E411-A48E-xxxxx)";

    var stringJSONAcc = JSON.stringify(entity);

    $.ajax({

    type: "POST",

    contentType: "application/json; charset=utf-8",

    datatype: "json",

    url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts",

    data: stringJSONAcc,

    beforeSend: function(XMLHttpRequest) {

    XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");

    XMLHttpRequest.setRequestHeader("OData-Version", "4.0");

    XMLHttpRequest.setRequestHeader("Accept", "application/json");

    },

    async: true,

    success: function(data, textStatus, xhr) {

    var uri = xhr.getResponseHeader("OData-EntityId");

    var regExp = /\(([^)]+)\)/;

    var matches = regExp.exec(uri);

    var newEntityId = matches[1];

    },

    error: function(xhr, textStatus, errorThrown) {

    Xrm.Utility.alertDialog(textStatus + " " + errorThrown);

    }

    });

  • gdas Profile Picture
    50,091 Moderator on at

    Hi Shahbaaz,

    Can please share the error message which are you getting.

  • Verified answer
    Sreevalli Profile Picture
    3,256 on at

    Hi Shahbaaz,

    you have to use schema name inplace of field name and entity plural name along with guid id.

    sample codesnip,

    "new_Account@odata.bind": "/accounts(" + selectedInvestor.items[i].id.replace("{", "").replace("}", "") + ")",

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    @Goutam : getting 400 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