SuiteTalk – Using upsertList to beat missing RecordRef error

Learning this rocked my world. It’s been a frustration when importing or synchronizing records between my legacy system and NetSuite. If I submit a request to create or update a record in NetSuite and all the references to related records are not present, I get a missing RecordRef error and the entire operation fails. In order to beat this, I preceded my insert/update operations with queries to ascertain the state of each reference. From a performance perspective, this was expensive.

So here’s how to improve the performance of your insert/update transactions by skipping all the reference checks.

  1. Create a new List<Entity or Transaction Type>.
          List<CustomRecord> custRecList = new List<CustomRecord>();
  2. Create a new [Entity or Transaction type] object.
          CustomRecord custRec = new CustomRecord(
    recType = new RecordRef() {
    internalid = “9999” // Key to my custom record type
    }
    , externalId = “xxxxxxxx” // An actual record Key
    , customFieldList = CustomFieldRef[] {}
    );
  3. Add fields that you know will NOT blow up on missing RecordRefs.
    • ExternalId
    • Name
    • And create a customFieldList array and populate the “safe” fields.
  4. Add custRec to CustRecList.
          custRecList.Add(custRec)
  5. NOW… HERE IS THE SECRET SAUCE… DON’T MISS IT.
    Recreate custRec with a record key (externalId) that references the record you just pushed onto the list in step 4. Then add one field that you suspect might have a missing RecordRef. And push that new object onto the list.
               CustomRecord custRec = new CustomRecord(
    recType = new RecordRef() {
    internalid = “9999”
    }
    , externalId = “xxxxxxxx”
                         //, body field that might fail because of a missing RecordRef
    , customFieldList = CustomFieldRef[] {
                     // custom field that might fail because of a missing RecordRef
    }
    );
  6. And finally, submit the entire list as your insert/update/upsert.
    netSuiteService.upsertList(custRecList)
  7. As you check the results, you’ll find that the initial insert/update request succeeds. Subsequent requests, where you have a missing RecordRef will fail. But that’s OK. It’s expected!

I hope this is as great a revelation to you as it was to me. Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s