1
votes

I am trying to do use the post method for a simple suitescript program, i am very new to this.

In Netsuite i have written a suitescript as follows.

function restPost()
 {   
     var i = nlapiLoadRecord('department', 115); 

     var memo = nlapisetfieldvalue('custrecord225', ' ');// this is a customfield, which i want to populate the memo field, using rest client in firefox

     var recordId = nlapiSubmitRecord(i);

}

i have created a script record and uploaded this suitescript and even copied the external URL to paste it in restclient.

In Restclient(firefox plugin), pasted the external URL, i have given the method as post, header authorization given, content-type: application/json, and in body i put in {"memo":"mynamehere"};

In this the error i get is

message": "missing ) after argument list

I even tried it by writting other suitescript programs the errors i get is as follows:

Unexpected token in object literal (null$lib#3) Empty JSON string Invalid data format. You should return TEXT.

I am kinda new to the programming world, so any help would be really good.

4
Your method restPost doesn't have any parameters. Check if you're missing them. You generally need some reference to the request object, and the data it contains.TonyH
I have modified my code and put in a parameter(datain) function restPost(datain) { var i = nlapiLoadRecord('department', 115); var memo = nlapisetfieldvalue('custrecord225', ' '); var recordId = nlapiSubmitRecord(i); } the error i get now is SyntaxError: Expected end of stream at char 21Vin

4 Answers

1
votes

I think you are trying to create a RESTlet for POST method. Following is the sample code for POST method -

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];
         if (value && typeof value != 'object') // ignore other type of parameters
         {
             record.setFieldValue(fieldname, value);
         }
     }
 }
}
var recordId = nlapiSubmitRecord(record);
nlapiLogExecution('DEBUG','id='+recordId);

var nlobj = nlapiLoadRecord(datain.recordtype,recordId);
return nlobj;
}

So after deploying this RESTlet you can call this POST method by passing following sample JSON payload -

{"recordtype":"customer","entityid":"John Doe","companyname":"ABCTools Inc","subsidiary":"1","email":"[email protected]"}

For Authorization you have to pass request headers as follows -

var headers = {
           "Authorization": "NLAuth nlauth_account=" + cred.account + ", nlauth_email=" + cred.email + 
                            ", nlauth_signature= " + cred.password + ", nlauth_role=" + cred.role,
           "Content-Type": "application/json"};
1
votes

I can understand your requirement and the answer posted by Parsun & NetSuite-Expert is good. You can follow that code. That is a generic code that can accept any master record without child records. For Example Customer Without Contact or Addressbook.

I would like to suggest a small change in the code and i have given it in my solution.

Changes Below

var isExistRec = isExistingRecord(objDataIn);
            var record = (isExistRec) ? nlapiLoadRecord(objDataIn.recordtype, objDataIn.internalid, {
                recordmode: 'dynamic'
            }) : nlapiCreateRecord(objDataIn.recordtype);

//Check for Record is Existing in Netsuite or Not using a custom function

function isExistingRecord(objDataIn) {
        if (objDataIn.internalid != null && objDataIn.internalid != '' && objDataIn.internalid.trim().length > 0)
            return true;
        else
            return false;
    }    

So whenever you pass JSON data to the REStlet, keep in mind you have to pass the internalid, recordtype as mandatory values.

Thanks
Frederick

0
votes

I believe you will want to return something from your function. An empty object should do fine, or something like {success : true}.

0
votes

Welcome to Netsuite Suitescripting @Vin :)

I strongly recommend to go through SuiteScript API Overview & SuiteScript API - Alphabetized Index in NS help Center, which is the only and most obvious place to learn the basics of Suitescripting.

nlapiLoadRecord(type, id, initializeValues)

Loads an existing record from the system and returns an nlobjRecord object containing all the field data for that record. You can then extract the desired information from the loaded record using the methods available on the returned record object. This API is a core API. It is available in both client and server contexts.

     function restPost(dataIn) {   
          var record = nlapiLoadRecord('department', 115); // returns nlobjRecord
          record.setFieldValue('custrecord225', dataIn.memo); //  set the value in custom field
          var recordId = nlapiSubmitRecord(record);
          return recordId;
        }