suitescriptsuitescript2.0

SuiteScipt 2.x User Event Script INVALID_API_USAGE


I'm trying to write a script on the server side that copies values from a line item field to a header field. What's weird is the script works when I'm editing the record but when I create a new record and test the script on that I get the INVALID_API_USAGE error with this message "Invalid API usage. You must use getSublistValue to return the value set with setSublistValue". Additionally I tried switching from the getSublistText() function to the getSublistValue() function which copied an erroneous value through but I did not get the error. I had originally tried to encapsulate the the record with the record module using record.load but I ran into issues getting the records Id I'm assumed that's because I'm using the beforsubmit function so the id hasn't been generated yet but the aftersubmit function was having similar issues. Can anyone help?

const beforeSubmit = (scriptContext) => {
        try{
            var count = scriptContext.newRecord.getLineCount({
                sublistId: 'item'
            })
            for(var i = 0; i < count; i++){ 
                if(scriptContext.newRecord.getSublistText({sublistId: 'item', fieldId: 'job', line: i}) != ''){
                    var jobNumber = scriptContext.newRecord.getSublistText({
                        sublistId:'item',
                        fieldId: 'job',
                        line: i
                    });
                    i = count
                }
            }
            scriptContext.newRecord.setText({
                fieldId: 'custbody_job_number',
                value: jobNumber
                
            });
        }
        catch(e){
            log.error ({ 
                title: e.name,
                details: e.message
            }); 
        }
        }

This is the record load function Ive been using.

var contextId = scriptContext.newRecord.getValue({fieldId: 'salesorder'})
            var objRecord = record.load({
                type: record.Type.SALES_ORDER,
                id: contextId,
                isDynamic: true
                })

Solution

  • Recently NetSuite have changed something which is causing errors when using the getText or getSublistText without using setSublistText or setText. Therefore, the solution here will be to use AfterSubmit and load the record first. Once loaded then you can use getText or getSublistText easily. Afterwards, save the record in afterSubmit. Please review below code for reference. Its working.

    const afterSubmit = (scriptContext) => {
        try {
    
    
    // Load the record
        const rec = record.load({
          type: scriptContext.newRecord.type,
          id: scriptContext.newRecord.id,
          isDynamic: true
        });
    
        // Get the number of line items
        const count = rec.getLineCount({ sublistId: 'item' });
    
        let jobNumber = '';
    
        // Loop through each line item and get the job number
        for (let i = 0; i < count; i++) {
          const jobText = rec.getSublistText({ sublistId: 'item', fieldId: 'job', line: i });
          if (jobText !== '') {
            jobNumber = jobText;
            break; // Exit the loop once the job number is found
          }
        }
    
        // Set the job number on the body field
        if (jobNumber) {
          rec.setValue({
            fieldId: 'custbody_job_number',
            value: jobNumber
          });
    
          // Save the record
          rec.save();
        }
      } catch (e) {
        log.error({
          title: e.name,
          details: e.message
        });
      }
     };