Sample Restlet

/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define(["N/record"],
(record) => {
/**
* Defines the function that is executed when a GET request is sent to a RESTlet.
* @param {Object} requestParams - Parameters from HTTP request URL; parameters passed as an Object (for all supported
* content types)
* @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
* Object when request Content-Type is 'application/json' or 'application/xml'
* @since 2015.2
*/
const get = (requestParams) => {
try{
let recordObj = record.load({
id:requestParams.id,
type:record.Type.CUSTOMER
});
let responseObj = {};
responseObj.email = recordObj.getValue("email");
responseObj.phone = recordObj.getValue("phone");
responseObj.name = recordObj.getValue("name");
return responseObj;
}
catch(err)
{
return {status:"ERROR",error:err}
}
}

/**
* Defines the function that is executed when a PUT request is sent to a RESTlet.
* @param {string | Object} requestBody - The HTTP request body; request body are passed as a string when request
* Content-Type is 'text/plain' or parsed into an Object when request Content-Type is 'application/json' (in which case
* the body must be a valid JSON)
* @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
* Object when request Content-Type is 'application/json' or 'application/xml'
* @since 2015.2
*/
const put = (requestBody) => {
try {

let recordObj = record.load({
id: requestBody.id,
type: record.Type.CUSTOMER
});

responseObj.email ? recordObj.setValue("email", responseObj.email):"";
responseObj.phone ? recordObj.setValue("phone", responseObj.phone):"";
responseObj.name ? recordObj.setValue("name", responseObj.name):"";

let id = recordObj.save();

return {id:id,status:"SUCCESS"}

}catch(err)
{
return {status:"ERROR",error:err}
}
}

/**
* Defines the function that is executed when a POST request is sent to a RESTlet.
* @param {string | Object} requestBody - The HTTP request body; request body is passed as a string when request
* Content-Type is 'text/plain' or parsed into an Object when request Content-Type is 'application/json' (in which case
* the body must be a valid JSON)
* @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
* Object when request Content-Type is 'application/json' or 'application/xml'
* @since 2015.2
*/
const post = (requestBody) => {

try {
let recordObj = record.create({
type: record.Type.CUSTOMER
});

responseObj.email ? recordObj.setValue("email", responseObj.email):"";
responseObj.phone ? recordObj.setValue("phone", responseObj.phone):"";
responseObj.name ? recordObj.setValue("name", responseObj.name):"";

let id = recordObj.save();
return {id:id,status:"SUCCESS"}
}catch(err)
{
return {status:"ERROR",error:err}
}

}

/**
* Defines the function that is executed when a DELETE request is sent to a RESTlet.
* @param {Object} requestParams - Parameters from HTTP request URL; parameters are passed as an Object (for all supported
* content types)
* @returns {string | Object} HTTP response body; returns a string when request Content-Type is 'text/plain'; returns an
* Object when request Content-Type is 'application/json' or 'application/xml'
* @since 2015.2
*/
const doDelete = (requestParams) => {
try {
let id = record.delete({
type: record.Type.CUSTOMER,
id: requestParams.id,
});
return {id:id,status:"SUCCESS"}
}catch(err)
{
return {status:"ERROR",error:err}
}

}

return {get, put, post, delete: doDelete}

});
<restlet scriptid="customscript_sample_restlet">
    <isinactive>F</isinactive>
    <name>Sample Restlet</name>
    <notifyowner>F</notifyowner>
    <scriptfile>[/SuiteScripts/Sample Script/Sample - Restlet.js]</scriptfile>
    <scriptdeployments>
        <scriptdeployment scriptid="customdeploy_sample_restlet">
            <isdeployed>T</isdeployed>
            <loglevel>DEBUG</loglevel>
            <status>RELEASED</status>
            <title>Sample Restlet</title>
        </scriptdeployment>
    </scriptdeployments>
</restlet>

Leave a comment

Your email address will not be published. Required fields are marked *