Zachary Dahan
Zachary Dahan

Reputation: 1499

How to access RESTlet SuiteScript parameters using GET method

I was stuck on a problem which probably plenty of new SuiteScript hackers will.

As writted on the official doc of SuiteScript p. 243, there's this JS for retrieve a record with GET method.

// Get a standard NetSuite record
function getRecord(datain) {
    return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769

But, when I was trying the EXACT snippet on NetSuite side, datain.recordtypewas undefined. (and return should only return text, BTW.)

Fortunately, I've found the solution by myself. Check my answer below.

Upvotes: 3

Views: 4883

Answers (1)

Zachary Dahan
Zachary Dahan

Reputation: 1499

In this snippet (the same as above) ...

function getRecord(datain) {
    return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769

SuiteScript was filling datain not as an object nor JSON but as a string (for reason I still ignore.)

What you have to do so is just parse it before, then access the JSON with dot notation.

function getRecord(datain) {
    var data = JSON.parse(datain); // <- this
    return "This record is a " + data.recordtype + " and the ID is " + data.id;
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769

I've changed the return statement in the solution because SuiteScript gives me error when I try to return something what isn't a text.

OR

As egrubaugh360 said, specify the Content-Type is application/json on your query script (the one who make call to your SuiteScript script)

So it'll give something like this if you're dealing with Node.js like me :

var options = {
    headers: {
        'Authorization': "<insert your NLAuth Authentification method here>",
        "Content-Type" : "application/json" // <- this
    }
}

https.request(options, function(results) {
    // do something with results.
}

Hope this will help someone.

Upvotes: 5

Related Questions