Sunday, September 16, 2018

Web Service > PHPToolkit > Sample Code > Search (Basic): Entity Records

This article applies to the PHPToolkit prior to release 2012.2.

 

As per the Suite Talk Web Services Platform Guide, The search operation is used to execute a search on a specific record type based on a set of criteria. You can search by defining search filter fields on the record, joined fields on an associated record, search return columns, or joined search return columns from an associated record. The results of the search can be complete records, or a select set of fields specified through search return columns.

 

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

 

This code snippet example, performs a search operation thru web service using PHPToolkit:

 

 <?php
       require_once 'PHPtoolkit.php';
       require_once 'login_info.php';
       global $myNSclient;

   # SEARCH EMPLOYEE
   # ===============
       //create a search value
       $strFirstname = "E";
       $employeeSearch = new nsComplexObject("EmployeeSearchBasic");
       $employeeSearch->setFields(array("firstName" => array( "operator" => "startsWith", "searchValue" => $strFirstname, "operatorSpecified" => true)));
       $myNSclient->setSearchPreferences(false);

       try
       {
           $searchResponse = $myNSclient->search($employeeSearch);
           $totalRecords = $searchResponse->totalRecords;
           if ($totalRecords > 0)
           {
             foreach ($searchResponse->recordList as $record)
             {
                 //Getting Employee's basic record
                 $employee_internalId = $record->getField('internalId');
                 $employee_firstname = $record->getField('firstName');
                 $employee_lastname = $record->getField('lastName');
                 echo "Internal Id: " . $employee_internalId . "<br />";
                 echo "First Name: " . $employee_firstname . "<br />";
                 echo "Last Name: " . $employee_lastname . "<br />";
             }
           }
           else
           {
             echo "No result found.";
           }
       }
       catch (Exception $e)
       {
           echo "Employee is not found. Please try again.";
           exit();
       }
?>           

 

 

The above code would generate the following SOAP request

 

SOAP Request:

<Envelope>
    <Body>
        <search>
            <searchRecord type="EmployeeSearchBasic">
                <firstName operator="startsWith">
                    <searchValue>E</searchValue>
                </firstName>
            </searchRecord>
        </search>
    </Body>
</Envelope>


SOAP Response:

<Envelope>
    <Header>
        <documentInfo>
            <nsId>WEBSERVICES_TSTDRV570143_0202201018812692351869588260_5d38a4</nsId>
        </documentInfo>
    </Header>
    <Body>
        <searchResponse>
            <searchResult>
                <status isSuccess="true"/>
                <totalRecords>15</totalRecords>
                <pageSize>50</pageSize>
                <totalPages>1</totalPages>
                <pageIndex>1</pageIndex>
                <searchId>WEBSERVICES_TSTDRV570143_0202201018812692351869588260_5d38a4</searchId>
                <recordList>
                    <record xsitype="Employee" internalId="2">
                        <entityId>Employee Sample</entityId>
                        <salutation>Mr</salutation>
                        <firstName>Employee1</firstName>
                        <middleName>M</middleName>
                        <lastName>EmployeeLastName</lastName>
                         ..................
                         ..................
                    </record>
                </recordList>
            </searchResult>
        </searchResponse>
    </Body>
</Envelope>

 

No comments:

Post a Comment