We can use the ‘nlapiCreateRecord’ Netsuite API to create any new Netsuite record via SuiteScript. This API seems very easy to use and understand for a Netsuite developer. But for some records we need to figure out why this API is not working or why is it throwing some error like ‘USER_ERROR’. While creating a Customer record for Netsuite, I came across a problem/error which said “USER_ERROR Details: Illegal ID”. This error message ideally means that the user has given a wrong ‘Netsuite Field Internal Id’ in the code. But the reason of this error turned out to be some thing else.
You might have noticed that when we manually create a Customer record in Netsuite the ‘Customer ID’/’entityid’ field gets copied from the ‘Company Name’/’companyname’ field, provided the ‘Type’ field is set to ‘Company’ (its the default value). Or it gets copied from the ‘Name’ field if the ‘Type’ is set to ‘Individual’. This should also reflect in your code.
Code Snippet:-
If 'Type' is 'Individual': function CreateCustomerRecord() { var customerRec = nlapiCreateRecord('customer'); customerRec.setFieldValue('isperson', 'T'); //---Mandatory customerRec.setFieldValue('firstname', 'Richi'); //---Mandatory customerRec.setFieldValue('lastname', 'Padhi'); //---Mandatory //--- Set the line items. customerRec.setLineItemValue('addressbook', 'addr1', 1, 'Mindfire Solutions'); customerRec.setLineItemValue('addressbook', 'addr2', 1, 'IDCO Tower 2000'); customerRec.setLineItemValue('addressbook', 'city', 1, 'Bhubaneswar'); customerRec.setLineItemValue('addressbook', 'zip', 1, '751010); var customerId = nlapiSubmitRecord(customerRec, true); } If 'Type' is 'Company':- function CreateCustomerRecord() { var customerRec = nlapiCreateRecord('customer'); //---Mandatory customerRec.setFieldValue('companyname', 'Mindfire Solutions'); //--- Set the line items. customerRec.setLineItemValue('addressbook', 'addr1', 1, 'Mindfire Solutions'); customerRec.setLineItemValue('addressbook', 'addr2', 1, 'IDCO Tower 2000'); customerRec.setLineItemValue('addressbook', 'city', 1, 'Bhubaneswar'); customerRec.setLineItemValue('addressbook', 'zip', 1, '751010); var customerId = nlapiSubmitRecord(customerRec, true); }
Always remember to use ‘nlapiSubmitRecord’ API to submit/save the record. And if you are assigning the 3rd parameter ‘ignoreMandatoryFields’ of ‘nlapiSubmitRecord’ API to ‘true’ then also you need to set those fields, which I have commented as “Mandatory” in the above code snippet. If in your Netsuite account there are some mandatory fields for the ‘Customer’ records, then don’t forget to initialize those fields in your code if you are not setting the ‘ignoreMandatoryFields’ parameter to ‘true’.
Hope this help for all Netsuite developers.