Saturday 27 August 2016

Create a Standard Netsuite Record Using Restlet API

Create a RESTlet with the following piece of code.

// Create a standard NetSuite record
function createRecord(datain)
{
    var err = new Object();
   
    // Validate if mandatory record type is set in the request
    if (!datain.recordtype)
    {
        err.status = "failed";
        err.message= "missing recordtype";
        return err;
    }
   
    var record = nlapiCreateRecord(datain.recordtype);
   
    for (var fieldname in datain)
    {
     if (datain.hasOwnProperty(fieldname))
     {
         if (fieldname != 'recordtype' && fieldname != 'id')
         {
             var value = datain[fieldname];
             // ignore other type of parameters​
             if (value && typeof value != 'object') 
             {
                 record.setFieldValue(fieldname, value);
             }
         }
     }
    }
    var recordId = nlapiSubmitRecord(record);
    nlapiLogExecution('DEBUG','id='+recordId);
   
    // returns the created record in JSON format​
     var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
    return nlobj;
}​

 Step 1 : Upload the script
















Step2 : Deploy the script

















































Remember to put the status to released if you want to access the end point externally. If you're in test mode then select "Testing" as the status. In "Testing mode" your API can't be accessed  outside. You also has to select the "Role" and "Employee" in order to give them to access your API.

Step 3 : Endpoint Generation








Step4 : How to access this API

    url = 'https://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=166&deploy=2'
    authorization = 'NLAuth nlauth_account=111111,nlauth_email=email@email.com,nlauth_signature=password,nlauth_role=correctRole' 
    req = urllib.request.Request(url)
    req.add_header('Authorization', authorization)
    req.add_header('Content-Type','application/xml')
    req.add_header('Accept','application/json')  
    // call the end point with appropriate playload with above headers in a POST call

No comments:

Post a Comment