locked
CRM Online Create Custom Record Using Javascript RRS feed

  • Question

  • I have a button in a ribbon that creates a a record of a custom entity.

    The custom entity (called Broadcast, schema: new_broadcast) has a name and a required Recipient (new_recipient) field which is a lookup of system user. The only other required field is owner, but I assume its create automatically.

    I'm struggling to get my javascript correct to create this. For testing, I am manually inputting the Guid of the systemuser.

    The error I'm getting is the generic 'Internal Server Error'

    function create() {
        var broadcast = new Object();
        var user = new Object();
    
        user.Id = "3599b578-9a79-45ed-bb1c-074354ee0c61";
        user.LogicalName = "systemuser";
        user.Name = "Chris Crawshaw";
    
        broadcast.new_Recipient = user;
        broadcast.new_name = "Activity Report";
    
        //Parse the entity object into JSON
        var jsonEntity = window.JSON.stringify(broadcast);
        alert(jsonEntity);
        // Get Server URL
        var serverUrl = Xrm.Page.context.getServerUrl(); // GetGlobalContext().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: serverUrl + ODATA_ENDPOINT + "/" + "new_broadcast",
            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);
                */
                //alert("Expressware Recruitment Configured");
                //}
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error on the creation of record; Error – " + errorThrown + " - " + textStatus);
            }
        });
    }

    Monday, October 22, 2012 1:21 PM

Answers

  • Hi,

    it should be:

    url: serverUrl + ODATA_ENDPOINT + "/" + "new_broadcastSet",

    instead of:

    url: serverUrl + ODATA_ENDPOINT + "/" + "new_broadcast"
    By the way, if you want to check the exact error messages, you can use Fiddler to check out the whole response from the server.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.



    Monday, October 22, 2012 1:41 PM

All replies

  • Hi,

    it should be:

    url: serverUrl + ODATA_ENDPOINT + "/" + "new_broadcastSet",

    instead of:

    url: serverUrl + ODATA_ENDPOINT + "/" + "new_broadcast"
    By the way, if you want to check the exact error messages, you can use Fiddler to check out the whole response from the server.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.



    Monday, October 22, 2012 1:41 PM
  • Awesome, very helpful.

    Also thanks for the mention of fiddler - I didn't think of that.

    Monday, October 22, 2012 1:58 PM