Tuesday, September 11, 2018

Web Services> PHPToolkit > Add and AddList Code Samples

Note: This article applies to the PHP Toolkit before version 2012.2.

As per the Suite Talk Web Services Platform Guide; The Add operation is used to add a new instance of a record in NetSuite. It is similar to the AddList operation except that it allows only one record to be added at a time.

This Article outlines samples for performing an add and addList using the PHPToolkit using the customer record.

Note : This article requires existing knowledge of Web Services and PHP.


   require_once '../PHPtoolkit.php';
   require_once 'login_info.php';

   global $myNSclient;


   # ADD A CUSTOMER
   # ===============

    // create array of customer fields
    $customerFields = array (
        'isPerson'          => true,
        'firstName'         => 'myFirstName',
        'lastName'          => 'myLastName',
        'companyName'       => 'myCompanyName',
        'phone'             => '555-555-1234',
        'email'             => 'myEmail@myEmail.com',
        );

    // create Customer record
    $customer = new nsComplexObject('Customer');
  
    // set fields
    $customer->setFields($customerFields);

    // perform add operation and store nsWriteResponse object in $addResponse variable
    $addResponse = $myNSclient->add($customer);

    // handle add response
    if (!$addResponse->isSuccess) {

     echo "<font color='red'><b>" . $addResponse->statusDetail[0]->message . "</b></font>";

    } else {

     echo "<font color='green'><b>SUCCESS!!!</b></font>";

    }
 


The above code would generate the following SOAP request

<Body>
<add>
<record xsitype="Customer">
<isPerson>true</isPerson>
<firstName>myFirstName</firstName>
<lastName>myLastName</lastName>
<companyName>myCompanyName</companyName>
<phone>555-555-1234</phone>
<email>myEmail@myEmail.com</email>
</record>
</add>
</Body>


AddList Sample:

      require_once '../PHPtoolkit.php';
    require_once 'login_info.php';

    global $myNSclient;


    # ADDLIST SAMPLE
    # ===============


    // create array of customer fields
    $customer1Fields = array (
        'isPerson'            => true,
        'firstName'           => 'myFirstName1',
        'lastName'            => 'myLastName1',
        'companyName'         => 'myCompanyName1',
        'phone'               => '555-555-1234',
        'email'               => 'myEmail1@myEmail.com',
        );


    $customer2Fields = array (
        'isPerson'            => true,
        'firstName'           => 'myFirstName2',
        'lastName'            => 'myLastName2',
        'companyName'         => 'myCompanyName2',
        'phone'               => '555-555-1234',
        'email'               => 'myEmail2@myEmail.com',
        );

    // create Customer records
    $customer1 = new nsComplexObject('Customer');
    $customer1->setFields($customer1Fields);

    $customer2 = new nsComplexObject('Customer');
    $customer2->setFields($customer2Fields);


    // perform addlist operation
    $addResponse = $myNSclient->addList(array($customer1, $customer2));

    // handle add response
    if (!$addResponse->isSuccess) {

        echo "<font color='red'><b>" . $addResponse->statusDetail[0]->message . "</b></font>";

    } else {

        echo "<font color='green'><b>SUCCESS!!!</b></font>";

    }
...


...
  The above code would generate the following SOAP request

 <Body>
<addList>
<record xsitype="Customer">
<isPerson>true</isPerson>
<firstName>myFirstName1</firstName>
<lastName>myLastName1</lastName>
<companyName>myCompanyName1</companyName>
<phone>555-555-1234</phone>
<email>myEmail1@myEmail.com</email>
</record>
<record xsitype="Customer">
<isPerson>true</isPerson>
<firstName>myFirstName2</firstName>
<lastName>myLastName2</lastName>
<companyName>myCompanyName2</companyName>
<phone>555-555-1234</phone>
<email>myEmail2@myEmail.com</email>
</record>
</addList>
</Body>

 



No comments:

Post a Comment