Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Having problems creating entity records with javascript, JSON and Odata.

$
0
0

Hi,

I use the code below that creates a new post related to the opportunity but when I try to set the fields using JSON object I only get failure all the time.

function CreateOpportunityMonth() {

var opportunityMonthId;
var context = Xrm.Page.context;
var clientUrl = context.getClientUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var JSONOpp = {};
// Array for lookup to opportunity record
var value = new Array();
value[0] = new Object();
value[0].id = Xrm.Page.data.entity.getId();
value[0].name = Xrm.Page.getAttribute("name").getValue();
value[0].entityType = "opportunity";

/////////////////////////////////////////////////////////////
// Specify the ODATA entity collection
var ODATA_EntityCollection = "/new_opportunitymonthSet";
/////////////////////////////////////////////////////////////

// Define attribute values for the CRM object you want created
JSONOpp.new_name = "TEST";

// Relates opportunity to the new post
JSONOpp.new_opportunitymonthsId = { Id: value[0].id, LogicalName: "opportunity", Name: value[0].name };
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(JSONOpp);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: clientUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
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("success");
var NewCRMRecordCreated = data["d"];
opportunityMonthId = NewCRMRecordCreated.new_opportunitymonthid;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
}

When I want to set the other field values like amount, currency, month etc I only get failure.

Examples of what doesnt work:

JSONOpp.new_amount = "1000";

JSONAcc.new_invoiced = { Value: 2 }; // Picklist

var OMamount = Xrm.Page.getAttribute("new_opportunitymonthvalue").getValue();

JSONOpp.new_amount = OMamount;

etc

I've used this blog as help: www.inogic.com/.../set-values-of-all-data-types-using-web-api-in-dynamics-crm


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images