locked
How get created record Id of CRM entity RRS feed

  • Question

  • I am Using CRM 2011 IFD

    I am Creating Record Using OData Jquery JavaScript

    I required newly created record id that I have to use to open that created record.

    function CreateBikePlan(account,oppProduct,BikeShop) {
        var serverUrl = Xrm.Page.context.getServerUrl().toString();
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var ODATA_EntityCollection = "/new_bikeplanSet";
      
        var objBikePlan = new Object();
        // set the name of BikePlan
        if(oppProduct[0].ProductId.Id!=null)
         {
           objBikePlan.new_name =oppProduct[0].ProductId.Name+"-"+BikeShop[0].new_BikeShop.Name;
           objBikePlan.new_BikeNameProduct={Id:oppProduct[0].ProductId.Id, Name:oppProduct[0].ProductId.Name, LogicalName:oppProduct[0].ProductId.LogicalName};
         }
    
        // Parse the entity object into JSON 
        var jsonEntity = window.JSON.stringify(objBikePlan);
    
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
            data: jsonEntity,
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (response) {
                if (response != null && response.d != null) {
                    var NewRecordCreated = data;
                    alert(NewRecordCreated.new_bikeplanId.tostring());
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
            }
        });
    }


    Muhammad Sohail


    • Edited by sohail450 Tuesday, April 22, 2014 12:38 PM
    Tuesday, April 22, 2014 12:37 PM

All replies

  • Hi

    you can try

    var Id = Xrm.Page.data.entity.getId();

    or

    http://mahenderpal.wordpress.com/2012/11/21/retrieve-primary-entity-and-related-entity-data-using-odata-in-ms-crm-2011/

    Bye

    Al


    Alessandro Graps

    Tuesday, April 22, 2014 2:19 PM
  • From your code looks like you are creating a record of a custom entity called new_bikeplan

    the Id is inside the response, this part:

        success: function (response) {
                if (response != null && response.d != null) {
                   // code executed after the record is created
                }
            }

    in your code you are checking the data variable, but you need to check the object response.d, so your code will be

        success: function (response) {
                if (response != null && response.d != null) {
                    var NewRecordCreated = response.d.new_bikeplanId;
                    alert(NewRecordCreated);
                }
            }


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Proposed as answer by Anupam Bishui Friday, April 25, 2014 11:16 AM
    Tuesday, April 22, 2014 2:57 PM