Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

problem creating new record using POST and NODE.JS http request

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I'm trying to insert a new record into a custom entity with the Web API using Node.js and the "http" object.   I don't want to use the client SDK and AJAX because it's in transition.  My goal is to use generic JS and JQuery against the Web API.  

1) I have been successful using as is it to retrieve records 

2) I have been successful using XMLHTTPRequest 

3) If I try using the following code I get no errors, no response and the record is not inserted.. nothing happens. but if I use the same code using a GET instead of a POST it works fine.

I'm looking at the http.request content and it looks fine.  Any ideas?

function insertRecord(requestheaders, entity, callback) {

	var tempRec = {};
	...assign values
	var dataRec = JSON.stringify(tempRec);
	
	var myhost = 'mytenant.api.crm.dynamics.com';  
	var mypath = "/api/data/v8.2/awl_reactcustomercampaignstatuses";	
	//set the crm request parameters. Note that requestheaders contains a valid token
	var crmrequestoptions = {
		'host': myhost,
		'path': mypath,
		//'url': myhost+mypath,
		'method': 'POST',
		'headers': requestheaders,
"Content-Length": Buffer.byteLength(dataRec) }; //make the web api request var myrequest = https.request(crmrequestoptions, function(response) { //make an array to hold the response parts if we get multiple parts var responseparts = []; response.setEncoding('utf8'); response.on('data', function(chunk){ //add each response chunk to the responseparts array for later responseparts.push(chunk); }); response.on('end', function(){ //once we have all the response parts, concatenate the parts into a single string var completeresponse = responseparts.join(''); //parse the response JSON var collection = JSON.parse(completeresponse).value; if (collection==null){ console.log('No Rows Returned') }else{ console.log(completeresponse); }; }); }); myrequest.on('error', function(e) { console.error(e); });
myrequest.write(dataRec); //close the web api request myrequest.end(); };

here's the request text  .....................

{ host: 'mytenant.api.crm.dynamics.com',
path: '/api/data/v8.2/my_customercampaignstatuses',
method: 'POST',
headers:
{ Authorization: 'Bearer XXX......',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8' },
data: '{"my_PeopleID@odata.bind":"/contacts(<GUID>)","my_CampaignID@odata.bind":"/campaigns(<GUID>)","my_dmapprovalcode":"AP-234","my_emailapprovalcode":"EM-002","my_campaignurl":"www.my.com/CampaignInfoTest","my_campaigntfn":"8001234567","my_offeredamount":210872.33,"my_offeredrate":3.99,"my_offeredterm":360,"my_startdate":"2018-02-28T05:00:00.000Z","my_expiredate":"2018-03-30T04:00:00.000Z","my_mycreated":"2018-02-26T22:56:56.539Z","my_myupdated":"2018-02-26T22:56:56.539Z"}' }


No Rows Returned

*This post is locked for comments

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans