locked
Create a Case using REST Endpoint with JavaScript (CRM 2011) RRS feed

  • Question

  • Hi,

    I’m trying to create a new record using REST Endpoint with JavaScript. When trying to create a new Case  I get an error “Internal Server Error”.
    I also tried to create an Account and a Lead and both work, but not Case. Any suggestions?

    Example how I TRY to create an Case:

    var indicent = new Object(); //=> CASE
    indicent.Title= "Test";
    var jsonEntity = JSON.stringify(indicent);
    var serverUrl = Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + 'IncidentSet',
    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) {
                 alert("ok");
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("ajax call failed" + XmlHttpRequest.toString() + errorThrown.toString() +  textStatus.toString());
            }
        });
    }


    Example how I create an Account: (it works even missing required fields…)

    var account = new Object(); //=> ACCOUNT
    account.Name= "Test";
    var jsonEntity = JSON.stringify(account);
    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + 'AccountSet',
    data: jsonEntity,
    beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                 alert("ok");
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("ajax call failed" + XmlHttpRequest.toString() + errorThrown.toString() +  textStatus.toString());
            }
        });
    }

    Example how I create a Lead: (it works even missing required fields…)

    var lead = new Object(); //=> LEAD
    lead.Subject= "Test";var jsonEntity = JSON.stringify(lead);

    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: serverUrl + ODATA_ENDPOINT + "/" + 'LeadSet',
    data: jsonEntity,
    beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                 alert("ok");
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("ajax call failed" + XmlHttpRequest.toString() + errorThrown.toString() +  textStatus.toString());
            }

        });
    }

    Tuesday, June 5, 2012 3:35 PM

Answers

  • Hi,

    Try to include atleast customerid, while creating case record


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:24 PM
    Tuesday, June 5, 2012 4:07 PM
    Moderator
  • Hello,

    If I'm not wrong you should fill field CustomerId.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:24 PM
    Tuesday, June 5, 2012 4:07 PM
    Moderator
  • Thanks for your help.

    You (both) are right; I had to add another field (CustomerID):

    incident.CustomerId= {__metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" },
       Id: Guid,
       LogicalName: "account"};

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:27 PM
    Tuesday, June 5, 2012 5:27 PM

All replies

  • Hi,

    Try to include atleast customerid, while creating case record


    Conatact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:24 PM
    Tuesday, June 5, 2012 4:07 PM
    Moderator
  • Hello,

    If I'm not wrong you should fill field CustomerId.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:24 PM
    Tuesday, June 5, 2012 4:07 PM
    Moderator
  • Thanks for your help.

    You (both) are right; I had to add another field (CustomerID):

    incident.CustomerId= {__metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" },
       Id: Guid,
       LogicalName: "account"};

    • Marked as answer by Fran Lara Tuesday, June 5, 2012 5:27 PM
    Tuesday, June 5, 2012 5:27 PM