0
votes

I am new to suitescript and trying to migrate our "units of measure" over to netsuite via suitescript.

I believe the best way to setup an on-demand script execution is 'AfterSubmit' on a user event script, is that accurate? I looked into a scheduled script, but that doesn't really run on demand since it waits for the scheduler.

My 'After Submit' function currently looks like this. I'm trying to create a record under Lists > Accounting > Units of Measure (Unit Type)

const afterSubmit = (scriptContext) => {
var unittype = record.create({ type: 'unitstype', defaultValues: { name: 'Weights' } }); }

When I run the script I get this error:

{"type":"error.SuiteScriptError","name":"INVALID_RCRD_INITIALIZE","message":"You have entered an invalid default value for this record initialize operation."

Is that because I need at least one item in the sublist? Can anyone help point me to the syntax needed to create the sublist item?

1

1 Answers

1
votes

I think the use case determines "the best way to setup an on-demand script". Personally I would make a Scheduled or Map/Reduce script and trigger it to run that on demand by using the "Save & Execute" feature on the script deployment in the UI, or task.create (to trigger it from another script). Scripts can also be executed using the Browser Console.

To resolve the error you received, the defaultValues parameter only works with specific records and fields, can reference Suite Answer Id 45152. Units Type is not a support record. So you need to create the record, set all required values, then save the record.

To create a new Unit of Measure the following should work

  //create record
  var newRec = record.create({
    type: 'unitstype',
    isDynamic: //optional
  }); 
  
  //set all desired main line/area values
  newRec.setValue({
    fieldId: 'name',
    value: 'Weights',
    ignoreFieldChange: true //Optional, if set to true, the field change and the secondary event is ignored. default is false
  });
  
  //set sublist values, if in standard mode
  newRec.setSublistValue({
    sublistId: 'uom', //Units in the UI
    fieldId: '', //will need to set all the required sublist fields
    line: 0,
    value: 
  });
  
  //set sublist values, if in dynamic mode
  newRec.selectLine({
    sublistId: 'item',
    line: 3
  });
  newRec.setCurrentSublistValue({
    sublistId: 'uom',
    fieldId: '',
    value: ,
    ignoreFieldChange: true
  });
  newRec.commitLine({
    sublistId: 'uom'
  });
  
  //save record for either mode
  var newRecId = newRec.save({
    enableSourcing: true, //Optional, if set to true, sources dependent field information for empty fields. default is false
    ignoreMandatoryFields: true //Optional, if set to true, all standard and custom fields that were made mandatory through customization are ignored. default is false
  });

For more information about dynamic and standard record, can reference Suite Answer Id 73792