Tennis
Tennis

Reputation: 137

NetSuite/SuiteScript 2.0 - How to Assign New Customer to Support Case

I have an AfterSubmit UserEventScript (support case) that correctly does the following:

  1. Loads an associated custom record type
  2. Creates a new Customer with Address using that custom record
  3. Saves the newCustomer to the database

            newCustomer.commitLine({
                sublistId: 'addressbook'
            });
    
            newCustomer.save({
                enableSourcing: true,
                ignoreMandatoryFields: false
            });
    

To my question, what are the next steps for replacing the old Customer with this newly created Customer on the support case? I've tried the following code, but it logs an "undefined" value.

          recordSubmitted.setValue({
            fieldId: "companyid",
            value: newCustomer.companyId            
          }); 

Upvotes: 0

Views: 817

Answers (1)

bknights
bknights

Reputation: 15367

The internal id of the new customer record is returned from the save function. The field you want to assign to has script id 'company'. So:

    var custId = newCustomer.save({
        enableSourcing: true,
        ignoreMandatoryFields: false
    });

   recordSubmitted.setValue({
        fieldId: 'company',
        value: custId            
   }); 

Generally if you are choosing a different customer to assign the case to you should consider whether the different customer already exists so you might have loaded the other customer or found it via a search.

from a load and verify:

   recordSubmitted.setValue({
        fieldId: 'company',
        value: differentCustomer.getValue({fieldId:'internalid'})            
   }); 

from a search you might get the id directly:

var custId = null;
mySearch.run().each(function(res){
    if(test(res)){
       custId = res.id;
       return false;
    }
    return true;
});

if(custId) recordSubmitted.setValue({
    fieldId: 'company',
    value: custId            
}); 

Upvotes: 2

Related Questions