Friday, November 9, 2018

Sample RESTlet in PHP for initializing a record to create a transaction (e.g. Billing a sales order)


For RestLet, much like what we do for Web Services, there are cases in which we would like to create an application that creates a transaction with its values initialized from another record - for example, billing a sales order that creates an invoice record.

Below is a sample RestLet script.

function BillOrder(datain){

    var invoice = nlapiTransformRecord('salesorder', datain.id, 'invoice');

    var invoiceID = nlapiSubmitRecord(invoice, true);

    return invoiceID;

}


You can test the script using a GET Function and calling it through PHP. Below is a sample PHP code to call the RestLet and display the internal ID of the invoice created.

<?php

// Set up your NetSuite credentials
$account    = "NS_ACCOUNT_NUMBER";
$email      = "email_address_to_NetSuite_Login";
$pass       = "NetSuite_password";
$role_id    = "3"; // 3 is the standard role for administrator

$content_type = "application/json"; //other possible value: "text/plain"

$host ="https://rest.netsuite.com/app/site/hosting/restlet.nl?script=359&deploy=1"; // External URL generated after you deploy the RestLet script

// Create Header using NetSuite credentials above

$headerString = "Authorization: NLAuth nlauth_account=". $account . ", " .

                        "nlauth_email=" . $email . ", " .

                        "nlauth_signature=" . $pass . ", " .

                        "nlauth_role=" . $role_id . "\r\n".

                  "Host: rest.netsuite.com \r\n" .

                  "Content-Type:" . $content_type;  

$arrOptions = array(
        'http'=>array(
        'header'=> $headerString,
        'method'=>"GET",
        'timeout'=>300
        ));

$context = stream_context_create($arrOptions);

$soInternalID = 4;

$response = file_get_contents($host . "&id=".$soInternalID, false, $context);

if (!$response){
      echo "Error: Invalid Response.";
} else {
    echo $response;
}

?>

No comments:

Post a Comment