user2243146
user2243146

Reputation: 11

Netsuite - get custom record with php toolkit

I've been handed a project to complete and the clients have asked for a field from a customrecord attached to each customer to appear on their website. We're integrating with Netsuite on login, and saving their data in our database so we don't have to keep accessing Netsuite (very slow).

On login, we access Netsuite to do a SearchMultiSelectCustomField and find the customer's company, and then do a CustomRecordSearchBasic and use their company ID to get a list of items they have access to.

We loop over each of those items, and then loop over their custom fields. One of the fields has a typeId of -10, which means we do an ItemSearchBasic to get this item's record and the item's custom fields, saving the internalId of this item.

At the end of this loop, we have an array of item IDs that a company is linked to. We also have the company ID (custrecord_nn_item_customer) and the Item ID (custrecord_nn_item_customer_list).

I need to perform a get request on a custom record to check if that customer has been approved for that item.

The customrecord's ID is 'customrecord_custitem', and internal Id is '1'. The record has 3 fields (although only 2 show up for the customer's Netsuite Record page):

custrecord_lookup_item - this is the Item record code (custrecord_nn_item_customer_list from above) custrecord_custitem_code is the code I need

My question (after all that) is does anyone have any examples or can point me in the right direction on how I can access a customrecord attached to a customer? I think all of the necessary information is provided, but I've never used Netsuite before or the PHP toolkit.

$this->depends('netsuite');
$this->netsuite->start();

// get the "customer" (aka company) that the user's contact record belongs to
$companySearch = $this->netsuite->complexObject('SearchMultiSelectCustomField')
    ->setFields(array(
        'searchValue' => new nsListOrRecordRef(array('internalId' => $companyId)),
        'internalId' => 'custrecord_nn_item_customer',
        'operator' => 'anyOf'
    ));

// Fetch items that the user's company has access to
$search = $this->netsuite->complexObject('CustomRecordSearchBasic')
    ->setFields(array(
        'recType' => new nsCustomRecordRef(array(
            'internalId' => 260,
            'type' => 'customRecord')
        ),
        'customFieldList' => array($companySearch)
    ));
    $response = $this->netsuite->client->search($search);



    // loop over the items
    foreach($this->netsuite->complexToSimple($response->recordList) as $record){
        //var_dump($record);
        $processor = null;
        $this_item = '';
        $this_person = '';
        // foreach custom field (all the fields we're interested in, common name etc. are custom)
        foreach($record['customFieldList']['customField'] as $customField){
            $processor = $customField['value'];
            $id = $processor['internalId'];
            $typeId = $processor['typeId'];

            if($customField['internalId']=='custrecord_nn_item_customer'){
                $this_person = $id;
            }elseif($customField['internalId']=='custrecord_nn_item_customer_list'){
                $this_item = $id;
            }

            // a typeId of -10 = an Inventory Item
            if($typeId == -10){


                // do an ItemSearchBasic to fetch the item with it's custom fields
                $itemSearch = $this->netsuite->complexObject('ItemSearchBasic')
                    ->setFields(array(
                        'internalId' => array(
                            'operator' => 'anyOf',
                            'searchValue' => array('type' => 'inventoryItem', 'internalId' => $id)
                        )
                    ));

                $itemSearch = $this->netsuite->client->search($itemSearch);

                // foreach custom item field
                if($v=@$this->netsuite->complexToSimple($itemSearch->recordList)){
                    foreach($v as $itemRecord){
                        //var_dump($itemRecord);
                        $item = array('id' => $itemRecord['internalId']);

                        $items[] = $item;
                    }
                }
            }
        }
    }

It is inside the foreach loop that I need to get the customrecord field for the company ID and the current iteration of the item ID.

Upvotes: 1

Views: 2993

Answers (1)

Suite Resources
Suite Resources

Reputation: 1164

Set up a saved search with the results columns you need. Don't worry about filtering by customer.

Call the search from your code, and dynamically filter for the current customer.

Your code should be about 5-10 lines long to get that done, and should be super quick.

Upvotes: 1

Related Questions