PHP code example of firebear / netsuite-php

1. Go to this page and download the library: Download firebear/netsuite-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

firebear / netsuite-php example snippets




use NetSuite\NetSuiteService;

$config = array(
   // bservices.netsuite.com",
   "email"    => "[email protected]",
   "password" => "mySecretPwd",
   "role"     => "3",
   "account"  => "MYACCT1",
   "app_id"   => "4AD027CA-88B3-46EC-9D3E-41C6E6A325E2",
   // optional -------------------------------------
   "logging"  => true,
   "log_path" => "/var/www/myapp/logs/netsuite"
);

$service = new NetSuiteService($config);



use NetSuite\NetSuiteService;

$service = new NetSuiteService();

// To use your own defined data center URL (or sandbox, for instance):
$config['host'] = 'https://123456789.suitetalk.api.netsuite.com';

// To allow the service to get the correct URL for your account on the fly,
// use the legacy webservices url.
$config['host'] = 'https://webservices.netsuite.com';

use NetSuite\Classes\GetRequest;
use NetSuite\Classes\RecordRef;

$request = new GetRequest();
$request->baseRef = new RecordRef();
$request->baseRef->internalId = "123";
$request->baseRef->type = "customer";

$getResponse = $service->get($request);

if ( ! $getResponse->readResponse->status->isSuccess) {
    echo "GET ERROR";
} else {
    $customer = $getResponse->readResponse->record;
}

use NetSuite\Classes\SearchStringField;
use NetSuite\Classes\CustomerSearchBasic;
use NetSuite\Classes\SearchRequest;

$service->setSearchPreferences(false, 20);

$emailSearchField = new SearchStringField();
$emailSearchField->operator = "startsWith";
$emailSearchField->searchValue = "j";

$search = new CustomerSearchBasic();
$search->email = $emailSearchField;

$request = new SearchRequest();
$request->searchRecord = $search;

$searchResponse = $service->search($request);

if (!$searchResponse->searchResult->status->isSuccess) {
    echo "SEARCH ERROR";
} else {
    $result = $searchResponse->searchResult;
    $count = $result->totalRecords;
    $records = $result->recordList;

    echo $count . " records were found.";
}

use NetSuite\Classes\Customer;
use NetSuite\Classes\RecordRef;
use NetSuite\Classes\AddRequest;

$customer = new Customer();
$customer->lastName = "Doe";
$customer->firstName = "John";
$customer->companyName = "ABC company";
$customer->phone = "123456789";
$customer->email = "[email protected]";

$customer->customForm = new RecordRef();
$customer->customForm->internalId = -8;

$request = new AddRequest();
$request->record = $customer;

$addResponse = $service->add($request);

if (!$addResponse->writeResponse->status->isSuccess) {
    echo "ADD ERROR";
} else {
    echo "ADD SUCCESS, id " . $addResponse->writeResponse->baseRef->internalId;
}

use NetSuite\Classes\AddRequest;
use NetSuite\Classes\CustomFieldList;
use NetSuite\Classes\RecordRef;
use NetSuite\Classes\SalesOrder;
use NetSuite\Classes\StringCustomField;

$sale = new SalesOrder();

// Associate a customer record with this order
$sale->entity = new RecordRef();
$sale->entity->type = 'customer';
$sale->entity->internalId = $myCustomerInternalId;

// Set the date of the order
$sale->tranDate = $myOrderDate;

// Set the shipping method and price for the order
$sale->shipMethod = new RecordRef();
$sale->shipMethod->internalId = $myShipMethodId;
$sale->shippingCost = $myShippingTotal;

// Look at the SalesOrder class definition for a list of all the available
// properties and their types that you can use on a sales order in NetSuite.
// You'll need to add items, addresses, status, etc.

// Create a sample string-type custom field to the order which represents
// the ID of the order in our source platform:
$cfOrderNum = new StringCustomFieldRef();
$cfOrderNum->scriptId = 'custbody_order_num';
$cfOrderNum->value = $myOrderNumber;

// Collect all custom fields into an array and add the list of fields to the
// order add request:
$customFields[] = $cfOrderNum;
$sale->customFieldList = new CustomFieldList();
$sale->customFieldList->customField = $customFields;

// Submit the sales order create request
$request = new AddRequest();
$request->record = $sale;
$response = $service->add($request);

if (!$addResponse->writeResponse->status->isSuccess) {
    echo "ADD ERROR";
} else {
    echo "ADD SUCCESS, id " . $addResponse->writeResponse->baseRef->internalId;
}

use NetSuite\Classes\AddRequest;
use NetSuite\Classes\InitializeRecord;
use NetSuite\Classes\InitializeRef;
use NetSuite\Classes\InitializeRequest;
use NetSuite\Classes\ItemFulfillmentPackage;
use NetSuite\Classes\ItemFulfillmentPackageList;

// Initialize an item fulfillment from an existing Sales Order
$reference = new InitializeRef();
$reference->type = InitializeRefType::salesOrder;
$reference->internalId = $mySalesOrderInternalId;

$record = new InitializeRecord();
$record->type = InitializeType::itemFulfillment;
$record->reference = $reference;

$request = new InitializeRequest();
$request->initializeRecord = $record;

$response = $service->initialize($request);

if (!$response->readResponse->status->isSuccess) {
    echo "INIT ERROR";
}

$itemFulfillment = $response->readResponse->record;

$package = new ItemFulfillmentPackage();
$package->packageWeight = 1;
$package->packageTrackingNumber = $myTrackingNumber;

$packageList = new ItemFulfillmentPackageList();
$packageList->package = $package;
$itemFulfillment->packageList = $packageList;

$request = new AddRequest();
$request->record = $itemFulfillment;

$response = $service->add($request);

if (!$response->writeResponse->status->isSuccess) {
    echo "ADD ERROR";
} else {
    echo "ADD SUCCESS, id " . $addResponse->writeResponse->baseRef->internalId;
}

$service->setLogPath('/path/to/logs');

$service->logRequests(true);  // Turn logging on.

$service->logRequests(false); // Turn logging off.

$config = array(
   //  "2019_1",
   "host"           => "https://webservices.netsuite.com",
   "account"        => "MYACCT1",
   "consumerKey"    => "0123456789ABCDEF",
   "consumerSecret" => "0123456789ABCDEF",
   "token"          => "0123456789ABCDEF",
   "tokenSecret"    => "0123456789ABCDEF",
   // optional -------------------------------------
   "signatureAlgorithm" => 'sha256', // Defaults to 'sha256'
);

$service = new NetSuiteService($config);

composer