Mitchell Kimbrough
Mitchell Kimbrough

President & CEO

Posted on May 5, 2016

How to Attach Records to Stuff in NetSuite using the SuiteTalk Webservice Thingy

So your client is using NetSuite as their CRM as opposed to Salesforce or HubSpot or what have you. There is a form on a website, maybe one running Freeform Pro on ExpressionEngine. People can submit the form and that data is landing in NetSuite. Now your client asks you to allow file attachments on these form submissions. Maybe the submitters are submitting grant applications or that sort of thing. You are using PHP like me and now you are stuck because the NetSuite documentation and example code for the PHP SDK is super super paper thin on how to attach stuff to other stuff through the API. I bet in your various attempts you are seeing errors like these:

Missing or Invalid RecordType for AttachTo

or

org.xml.sax.SAXException: {urn:core_2015_1.platform.webservices.netsuite.com}BaseRef is an abstract type and cannot be instantiated

Yeah, sadness, trust me I know.

I am assuming you are using the NetSuite PHP Toolkit. What you are looking for is the class: AttachBasicReference. That class has two attributes: attachedRecord, attachTo. you have already uploaded a file to NetSuite using the File object. The SuiteTalk API returned an id number for that object. You already have the NetSuite id for the contact that you want to attach the file to. So now here's the code you need to create the attachment relationship through the SuiteTalk API:

$contact = new RecordRef();
$contact->type = 'contact';
$contact->internalId = $contact_id;

$file = new RecordRef();
$file->type = 'file';
$file->internalId = $file_id;

$attachment = new AttachBasicReference();
$attachment->attachedRecord = $file;
$attachment->attachTo = $contact;

$request = new AttachRequest();
$request->attachReference = $attachment;

try
{
  $response = $this->service->attach($request);
}
catch (Exception $e)
{
  print_r($e->getMessage()); exit();
}

See, what you want is the AttachBasicReference class. That lets you attach one thing to another thing in NetSuite, assuming an object accepts attachments. Most do.

The 'attachedRecord' is the file and 'attachTo' is the object that gets the file, in this case a contact.

And for what's it's worth, I also had the same trial and error problem trying to attach a contact record to an opportunity record using SuiteTalk. The code for that is a touch different because there is a specialized subclass for contact attachments: AttachContactReference.

$contact = new Contact();
$contact->internalId = $contact_id;

$opportunity = new RecordRef();
$opportunity->type = 'opportunity';
$opportunity->internalId = $opportunity_id;

$attachment = new AttachContactReference();
$attachment->contact = $contact;
$attachment->attachTo = $opportunity;

$request = new AttachRequest();
$request->attachReference = $attachment;

try
{
  $response = $this->service->attach($request);
}
catch (Exception $e)
{
  print_r($e->getMessage()); exit();
}

You're totally welcome!!!

smile

Let's begin our conversation

Contact us now.