locked
Creating Case using ODATA RRS feed

  • Question

  • Hi,

    I was trying to create case using odata.

    Below is the code

        var inc = new Object();
        inc .TicketNumber = "Test";
        var createdIncident = createRecordSync(inc , "IncidentSet");
        if (createdIncident ) {
        alert("New Account Created !!!; Id – "+createdIncident .Title);
        }

    function createRecordSync(entityObject, odataSetName) {
        var jsonEntity = window.JSON.stringify(entityObject);
        // Get Server URL
        var serverUrl = Xrm.Page.context.getServerUrl();
        //The OData end-point
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var createRecordReq = new XMLHttpRequest();
        var ODataPath = serverUrl + ODATA_ENDPOINT;
        createRecordReq.open('POST', ODataPath + "/" + odataSetName, false);
        createRecordReq.setRequestHeader("Accept", "application/json");
        createRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        createRecordReq.send(jsonEntity);
        var newRecord = JSON.parse(createRecordReq.responseText).d;
        return newRecord;
    }

    But createdIncident is returning undefined. can anyone tell me what is wrong in my code.


    • Edited by kishanraj Monday, November 18, 2013 10:21 AM
    Monday, November 18, 2013 10:20 AM

All replies

  • Kishanraj,

    You can not create Incident record without assigning Customer.. Try this

    var inc = new Object();
    inc.TicketNumber = "123";
    inc.CustomerId =
    {
    	Id: "4CA62D42-E0ED-E211-B17A-5473B29D2B0D",
    	LogicalName: "contact"
    }
    var createdIncident = createRecordSync(inc, "IncidentSet");
    if (createdIncident)
    {
    	alert("New Incident Created !!!; Id – " + createdIncident.IncidentId);
    }
    
    function createRecordSync(entityObject, odataSetName)
    {
    	var jsonEntity = window.JSON.stringify(entityObject);
    	// Get Server URL
    	var serverUrl = Xrm.Page.context.getServerUrl();
    	//The OData end-point
    	var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    	var createRecordReq = new XMLHttpRequest();
    	var ODataPath = serverUrl + ODATA_ENDPOINT;
    	createRecordReq.open('POST', ODataPath + "/" + odataSetName, false);
    	createRecordReq.setRequestHeader("Accept", "application/json");
    	createRecordReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	createRecordReq.send(jsonEntity);
    	var newRecord = JSON.parse(createRecordReq.responseText).d;
    	return newRecord;
    }



    • Proposed as answer by Yatin Babaria Monday, November 18, 2013 11:17 AM
    • Edited by Yatin Babaria Monday, November 18, 2013 11:18 AM
    Monday, November 18, 2013 11:16 AM
  • Hi,

    I am able to create case using odata, but not able to set value to lookup fields.

    for setting customer lookup i used below code and it worked

    var provider = new Object();
        provider.AccountId = lookupData.id;
        provider.Name = "Not Assigned";
        if (provider != null) {
            incident.CustomerId = { Id: provider.AccountId, LogicalName: "account", Name: provider.Name };
        }

    for setting another lookup which is a custom entity i used below code

    var problemType = new Object();
        problemType.smp_problemtypeId = ProblemTypeId;
        problemType.smp_problemtypename = ProblemTypeName;
        if (problemType != null) {
            problemType.smp_ProblemTypeId = { Id: problemType.smp_problemtypeId, LogicalName: "smp_problemtype", Name: problemType.smp_problemtypename };
        }

    the code is running with out exceptions, but lookup field is not set. what can be the wrong i did. Is there any other way to set value to a lookup which is a custom entity.

    Thanks,

    Kishan.

    Monday, November 18, 2013 12:24 PM
  • Kishan,

    I think you need to write like this. one more thing, attribute name (smp_problemtypeId) is case sensitive, make sure you are using correct name, it should be schema name of the attribute.

    var incident = new Object();

    incident.smp_problemtypeId = { Id: "<<Problem Type Record Id>>", LogicalName: "<<Problem Type Logical Name>>" }



    Monday, November 18, 2013 12:50 PM
  • Make sure you are referencing correct schema name try to debug your code and see lookup properties is setting correctly or not. 

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, November 18, 2013 8:46 PM
    Moderator