Simple search for Netsuite Suitetalk API and PHP Toolkit - php

I'm trying to make a simple search with Netsuite Suitetalk API and the latest PHP Toolkit version.
Here is my code:
$service = new NetSuiteService();
$service->setSearchPreferences(false, 20);
$typeSearchField = new SearchStringField();
$typeSearchField->operator = SearchStringFieldOperator::is;
$typeSearchField->searchValue = "SalesOrder";
$search = new TransactionSearchBasic();
$search->recordType = $typeSearchField;
$request = new SearchRequest();
$request->searchRecord = $search;
$searchResponse = $service->search($request);
However, this request returns all records from Netsuite, not just the SalesOrders. Any idea what I'm doing wrong?
Update:
here is the working version, with the help of eliseobeltran. I had to change also $search->recordType with $search->type
$service = new NetSuiteService();
$service->setSearchPreferences(false, 20);
$SearchEnumMultiSelectField = new SearchEnumMultiSelectField(); //set up multiselect field to which IDs will be put
$SearchEnumMultiSelectField->searchValue = Array('_salesOrder'); //put IDs of transactions to search for as a search value
$SearchEnumMultiSelectField->operator = 'anyOf'; //set operator according to which search will be executed. Values are anyOf/noneOf
$search = new TransactionSearchBasic();
$search->type = $SearchEnumMultiSelectField;
$request = new SearchRequest();
$request->searchRecord = $search;
$searchResponse = $service->search($request);

Use "_salesOrder".
$SearchEnumMultiSelectField = new SearchEnumMultiSelectField(); //set up multiselect field to which IDs will be put
$SearchEnumMultiSelectField->searchValue = Array('_salesOrder'); //put IDs of transactions to search for as a search value
$SearchEnumMultiSelectField->operator = 'anyOf'; //set operator according to which search will be executed. Values are anyOf/noneOf

Related

Why am I getting a The field type's enum value is invalid for this search message?

Here is the situation.
I'm trying to get the proper class name of the serialized inventory item, however, when I'm doing a search (see the code below):
$type = new SearchEnumMultiSelectField();
$type->operator = 'anyOf';
$type->searchValue = array('serializedInventoryItem');
$search->type = $type;
$invetoryRef = new RecordRef();
$invetoryRef->internalId = '522216';
$params = new SearchMultiSelectField();
$params->operator = 'anyOf';
$params->searchValue = array($invetoryRef);
$search->serializedInventoryItem = $params;
However, when I do a search, I'm getting the following message: The field type's enum value is invalid for this search.
Why would I be getting the following error message?
Thanks!
Kevin
Found a solution, instead of using the code above, I used the following code:
$service = new NetSuiteService();
$request = new GetRequest();
$request->baseRef = new RecordRef();
$request->baseRef->internalId = 522216;
$request->baseRef->type = 'serializedInventoryItem';
$getResponse = $service->get($request);
That resolved the issue.

how to use multiple filter in google analytics reporting api v4?

https://developers.google.com/analytics/devguides/reporting/core/v4/samples for find data from google analytic.
I want to find data using multiple dimension filter but not able to do it.
i am using the code.
function buildSimpleSegment($segmentName, $dimension, $dimensionFilterExpression) {
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create Dimension Filter.
$dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
$dimensionFilter->setDimensionName($dimension);
$dimensionFilter->setOperator("EXACT");
$dimensionFilter->setExpressions(array($dimensionFilterExpression));
//print_r($dimensionFilter);die;
// Create Segment Filter Clause.
$segmentFilterClause = new Google_Service_AnalyticsReporting_SegmentFilterClause();
$segmentFilterClause->setDimensionFilter($dimensionFilter);
// Create the Or Filters for Segment.
$orFiltersForSegment = new Google_Service_AnalyticsReporting_OrFiltersForSegment();
$orFiltersForSegment->setSegmentFilterClauses(array($segmentFilterClause));
// Create the Simple Segment.
$simpleSegment = new Google_Service_AnalyticsReporting_SimpleSegment();
$simpleSegment->setOrFiltersForSegment(array($orFiltersForSegment));
// Create the Segment Filters.
$segmentFilter = new Google_Service_AnalyticsReporting_SegmentFilter();
$segmentFilter->setSimpleSegment($simpleSegment);
// Create the Segment Definition.
$segmentDefinition = new Google_Service_AnalyticsReporting_SegmentDefinition();
$segmentDefinition->setSegmentFilters(array($segmentFilter));
// Create the Dynamic Segment.
$dynamicSegment = new Google_Service_AnalyticsReporting_DynamicSegment();
$dynamicSegment->setSessionSegment($segmentDefinition);
$dynamicSegment->setName($segmentName);
// Create the Segments object.
$segment = new Google_Service_AnalyticsReporting_Segment();
$segment->setDynamicSegment($dynamicSegment);
return $segment;
}
function getReport($analyticsreporting) {
// Create the DateRange object.
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
if (isset($_GET['startdat']) && isset($_GET['enddate'])) {
$dateRange->setStartDate($_GET['startdat']);
$dateRange->setEndDate($_GET['enddate']);
} else {
$dateRange->setStartDate(date('Y-m-d', strtotime(date('Y-m-d') . ' - 1 month')));
$dateRange->setEndDate(date('Y-m-d'));
}
$domain = $_GET['domain'];
$VIEW_ID = "xyz";
// Create the Metrics object.
$totalevent = new Google_Service_AnalyticsReporting_Metric();
$totalevent->setExpression("ga:totalEvents");
$totalevent->setAlias("totalEvent");
$source = new Google_Service_AnalyticsReporting_Dimension();
$source->setName("ga:source");
$eventAction = new Google_Service_AnalyticsReporting_Dimension();
$eventAction->setName("ga:eventAction");
$eventLabel = new Google_Service_AnalyticsReporting_Dimension();
$eventLabel->setName("ga:eventLabel");
$eventCategory = new Google_Service_AnalyticsReporting_Dimension();
$eventCategory->setName("ga:eventCategory");
// Create the segment dimension.
$segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
$segmentDimensions->setName("ga:segment");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges(array($dateRange));
$request->setDimensions(array($source, $eventAction, $eventLabel, $eventCategory, $segmentDimensions));
$request->setMetrics(array($totalevent));
$sourceSegment = buildSimpleSegment("Source", "ga:source", $domain);
$videoSegment = buildSimpleSegment("Category", "ga:eventCategory", "JW Player Video Plays");
$request->setSegments(array($sourceSegment, $videoSegment));
// Call the batchGet method.
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($request));
return $response = $analyticsreporting->reports->batchGet($body);
}
A bit late but still useful, I think, because I couldn't find any answer to it on SO.
To define multiple filters you need to instance the class Google_Service_AnalyticsReporting_DimensionFilter multiple times. For example:
$filter1 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter1->setDimensionName('first_dimension_name');
$filter1->setOperator('first_operator');
$filter1->setExpressions('first_expression');
$filter2 = new Google_Service_AnalyticsReporting_DimensionFilter();
$filter2->setDimensionName('second_dimension_name');
$filter2->setOperator('second_operator');
$filter2->setExpressions('second_expression');
Then define the filter clause which tie the filters defined above:
$filter_clause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$filter_clause->setOperator('your_filter_operator');
$filter_clause->setFilters(array($filter1, $filter2));
Then do the request as usual.
The answer #Maurizio provided is great but I needed a solution for a variable number of campaigns. Client may track one or five. Here's where I landed in case it helps anyone dealing with Google's (fairly terrible API documentation).
// Create DimensionFilters for each campaign.
// note: $campaigns is an array supplied above this code and used below
$dimension_filters = array(); // set up an empty array
for($i = 0; $i < count($campaigns); $i++){
${"dimensionFilter$i"} = new Google_Service_AnalyticsReporting_DimensionFilter();
${"dimensionFilter$i"}->setDimensionName('ga:campaign');
${"dimensionFilter$i"}->setOperator('EXACT');
${"dimensionFilter$i"}->setExpressions(array($campaigns[$i]));
$dimension_filters[] = ${"dimensionFilter$i"}; // push our new DimensionFilter instance onto the array
}
// Create the DimensionFilterClauses
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters(array($dimension_filters)); // now supply our final array of DimensionFilter instances

Netsuite PHP API: how to update custom field on item record

I have looked at some answers on here and tried putting them into action in my script; but it's not working, and I'm not sure why.
I am trying to update a custom field on an already-existing inventory item. Here is what I have so far
<?php
require_once '../PHPToolkit/NetSuiteService.php';
$service = new NetSuiteService();
$item = new InventoryItem();
$item->internalId = 72309;
$customFieldList = new CustomFieldList();
$customField = new StringCustomFieldRef();
$customField->value = utf8_encode("12345");
$customField->internalId = 'custitem_testinput';
$customFieldList->customField[] = $customField;
$item->customFieldList = $customFieldList;
$request = new UpdateRequest();
$request->record = $item;
$response = $service->update($request);
?>
I'm trying to pull the item record up by its internalID, and then update just 1 of its many custom fields.
This code doesn't error out, but it doesn't seem to do anything either. Looking at the var_dump of the objects, I can see an item object with just the parameters I've set populated, and everything else null.
What am I doing wrong?
This is a basic example showing how to update custom field in an existing record using PHP toolkit.
<?php
require_once 'PHPToolkit/NetSuiteService.php';
$service = new NetSuiteService();
$ItemId = '72309';
$ItemRecord= new InventoryItem();
//StringCustomFieldRef
$itemField= new StringCustomFieldRef();
$itemField->scriptId = 'custitem_part_lookup';
$itemField->value = 'test';
$customFieldList = new customFieldList();
$customFieldList->customField = array($itemField);
$ItemRecord->internalId = $ItemId ;
$ItemRecord->customFieldList = $customFieldList;
$updateRequest = new UpdateRequest();
$updateRequest->record = $ItemRecord;
$updateResponse = $service->update($updateRequest);
?>
For additional information you can visit Net Suite User Group

Querying a Clarizen Project/Task in PHP

I'm currently trying to get a list of all Clarizen active projects so that I can modify some properties of its tasks. I've read the Clarizen API but it doesn't have many informations on PHP queries. What I managed to do so far is to query all the projects and then test their status one by one. In practice this is not a good approach since I have thousands of projects and not all of them are listed at once. Here's the code:
//LOGIN PROCCESS
$soapUrl = 'https://api.clarizen.com/v1.0/Clarizen.svc?WSDL';
$soapApiUrl = 'http://clarizen.com/api';
$soapConfig = array('exceptions' => 1);
$request = array();
$params = array(
'userName' => $username,
'password' => $password
);
$client = new SoapClient($soapUrl, $soapConfig);
$response = $client->Login($params);
$sessionId = $response->LoginResult->SessionId;
$userId = $response->LoginResult->UserId;
//Create a SOAP header containing the session ID for future requests
$header = new SoapHeader($soapApiUrl, 'Session', array("ID"=>$sessionId));
$client->__setSoapHeaders($header);
//Create a Query object
$userQuery = new stdClass();
//Set the name of the entity type you are querying
$userQuery->TypeName = 'Project';
//Select the fields you want retrieved from that entity
$userQuery->Fields = array('Name', 'State');
/* Doesnt work...*/
/*
$userQuery->Where = new stdClass();
$userQuery->Where->LeftExpression = new stdClass();
$userQuery->Where->LeftExpression->FieldName = 'State';
$userQuery->Where->Operator = 'Equal';
$userQuery->Where->RightExpression = new stdClass();
$userQuery->Where->RightExpression->Value = new stdClass();
$userQuery->Where->RightExpression->Value->TypeName = 'State';
$userQuery->Where->RightExpression->Value->Value = 'Active';
*/
$request[] = new SoapVar($userQuery, SOAP_ENC_OBJECT, 'EntityQuery', 'http://clarizen.com/api/queries');
//Execute the request
$result = $client->Execute(array("request"=>$request));
3 questions follow:
What is the right way to query in PHP with the "WHERE" clause
How to fetch the tasks of this project and then, for example create a Stopwatch for it.
How to continue the query until the hasMore flag is 0, or is there a way to fetch the entire thing all at once?
Thanks in advance.

Netsuite PHP API - Find customer by email

I am trying to search for a customer by email. The Netsuite api documentation does not help much. Appreciate any help i can get. Thank You.
global $myNSclient;
$email = "myemail";
$item = new nsComplexObject('SearchStringField');
$item->setFields(array( 'searchValue' => $email, 'operator' => 'is'));
$search = new nsComplexObject('ContactSearchBasic');
$search->setFields($item);
$myNSclient->setSearchPreferences(false, 10);
$searchResponse = $myNSclient->search($search);
I got quite a few gray hairs trying to figure this out myself.
Below is the code to get a contact (different than a customer in Netsuite, both are native data types though) from their email. It will be very similar for a Customer.
$service = new NetSuiteService();
$service->setSearchPreferences(false, 20);
$recordRef = new RecordRef();
$recordRef->internalId = '-6'; //Internal ID for a customer is -2, contact is -6
$contactSearch = new ContactSearch(); //use CustomerSearch() for a customer
$contactSearchBasic = new ContactSearchBasic();//SearchRecordBasic
$contactSearchBasic->email = new SearchStringField();
$contactSearchBasic->email->searchValue = 'someone#somewhere.com';
$contactSearchBasic->email->operator = SearchStringFieldOperator::is;
$contactSearch->basic = $contactSearchBasic;
$searchRequest = new searchRequest(); //% contains a searchRecord
$searchRequest->searchRecord = $contactSearch;
$searchResponse = $service->search($searchRequest);

Categories