How to run SERVER Side only API on Client Side or Using Client Scripts in Netsuite?
Hi All,
As we all know server side script can not run on client side, it means inside client script we can not use that,
Some times we want to do some specific task and that need server side programming , now question is how to run server side api on client,
I can tell you how to run server side script on client side, but this do not mean I am going to use Server Side api directly in client side and run.
No this is not a case.
As we all know Suitelets can be used for Showing data as well as transferring data.
So we can use suitelets and using suitelet we can run our server side logic.
For Example:
you want to delete a file on click of a button, you want to use "nlapiDeleteFile" but this is server side programming,
Now what you should do to achieve this functionality ,
write a suitelet where fid is parameter value, get value and delete that file and return appropiate methods.
If it fails return false if pass return true. Do not forget to use try catch and inside catch return false so that script can know if error came on calling url.
Suitelet to Run:
function clientServerSuitelet(request, response)
{
// you can check mehod here if GET or POST
try{
var fileid = request.getParameter('fid');
if(fileid)
{
var deleted = nlapiDeleteFile(fileid);
if(deleted > 0)
{
response.write(true);
}
else
{
response.write(false);
}
}
}
catch(e)
{
var err = '';
if ( e instanceof nlobjError )
{
err = 'System error: ' + e.getCode() + '\n' + e.getDetails();
}
else
{
err = 'Unexpected error: ' + e.toString();
}
var errmsg = 'Error: ';
errmsg+= '\n' + err;
nlapiLogExecution( 'ERROR', 'Error is: ', errmsg);
response.write(false);
}
}
Client Script To Call:
function client_deteleteFile()
{
try
{
var url = nlapiResolveURL('SUITELET', 'customscript_mySuitelet_id', 'customdeploy_mysuitelet') + '&fid=nlapiGetFieldValue('fieldid_YOU_Can_CHange_Logic_And_Add_Your_Logic_Here')';
var response = nlapiRequestURL(url);
var body = response.getBody();
if(body == true || body == 'true')
{
alert('File deletion successful');
}
else
{
alert('Unable to delete file.');
}
}
catch(e)
{
//your logic to see message
}
}
Comments
Post a Comment
Thanks for you message, please join us on Facebook and Linkedin