function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Trevor RoyTrevor Roy 

Calling APEX REST API from NetSuite Script

I've been able to get my APEX REST class working just fine in the Workbench and via cUrl calls.
But what I really want to do is have a script in our NetSuite environment make a call to my REST function.
I've been looking at the forcetk.js project but I'm unclear as to whether it's what I need or if I need to write something of my own jquery based code to do the authorization and make the call to my REST class.

Does anyone have any experience doing this and have any advice?
Best Answer chosen by Trevor Roy
Trevor RoyTrevor Roy

Hi susanta, yes I was able to determine a way to make this work, eventually.
While the forcetk.js can do a lot of things it wasn't really what I needed to have happen for my very simple SuiteScript requirements. All I needed was to pass a boolean into a Salesforce record. Otherwise I used the nlapiRequestURL() SuiteScript function to do all of the OAuth security stuff and then make my REST request.

It's been a while since I worked on this code so the details may be lacking, basically all I had to do was add an extra argument to my request to force PATCH to be accepted. IIRC, this is because SF wants you to make small changes using PATCH but NetSuite doesn't want to do anything but POST.

My request ended up looking like this:

oauthData.instance_url + '/services/data/v.<api_version>/sobjects/<obj_name>/<obj_sf_id>?_HttpMethod=PATCH'

where everything in <> is up to whatever api version and object you are working with.


 

All Answers

BalajiRanganathanBalajiRanganathan
I believe you can call only the standard salesforce rest api using forcetk.js with out using a proxy.
to call custom rest api, you need a proxy. you could use proxy.php that comes with the forcetk.js project
else you can develop a server side component on your netsuire enviroment to call the rest api.
 
susanta sahususanta sahu

When i call a APEX REST API i get below error 
[{"errorCode":"NOT_ACCEPTABLE","message":"Accept header specified in HTTP request is not supported: text/*"}]

Need Help ...

Hi Trevor, 
Are you able to Call APEX REST API from NetSuite Scripts now ??
if yes let me know how to make it a success.

@Balaji 
i have to call custom API, Can you help me more on how can i use a proxy that too in NetSuite 
 

Trevor RoyTrevor Roy

Hi susanta, yes I was able to determine a way to make this work, eventually.
While the forcetk.js can do a lot of things it wasn't really what I needed to have happen for my very simple SuiteScript requirements. All I needed was to pass a boolean into a Salesforce record. Otherwise I used the nlapiRequestURL() SuiteScript function to do all of the OAuth security stuff and then make my REST request.

It's been a while since I worked on this code so the details may be lacking, basically all I had to do was add an extra argument to my request to force PATCH to be accepted. IIRC, this is because SF wants you to make small changes using PATCH but NetSuite doesn't want to do anything but POST.

My request ended up looking like this:

oauthData.instance_url + '/services/data/v.<api_version>/sobjects/<obj_name>/<obj_sf_id>?_HttpMethod=PATCH'

where everything in <> is up to whatever api version and object you are working with.


 

This was selected as the best answer
Nand AroraNand Arora
I was getting a similar error message as Susanta Sahu
[{"errorCode":"NOT_ACCEPTABLE","message":"Accept header specified in HTTP request is not supported: text/plain"}]

Changing the return type of the POST method, in my REST service, from "String" to "void" helped me resolve the issue.

Basically, there are two possible ways to resolve this issue
  1. Client-Side: If you have access to change the request header at the client-side, you can simply change the "accept" parameter from whatever its value is ( it was "text/plain" in my case) to */*. This the recommended approach and should work at all times.
  2. Server-Side: Although sometimes it's not possible to change the client request's header. In that case, you can try and change the return type of the POST method of your service (assuming you are using the POST method). It worked in my case but I'm not sure if it is true for everyone.
I hope it helps.