user27490221
user27490221

Reputation:

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
                })

Upvotes: 0

Views: 99

Answers (2)

Yasir Rao
Yasir Rao

Reputation: 64

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
    });
  }
 };

Upvotes: 0

Sundance.101
Sundance.101

Reputation: 424

I suspect that the erroneous value is the id of the project/job record, whereas you are trying to get the text/name of that sublist field? I'll continue on that assumption.

I'd also note that if the field custbody_job_number is a freetext field, you can still use setValue/getValue, either would work as really the getText method is for when the field type is list/record (or, that's my understanding of it's usage, happy to be corrected).

I've encountered this type of error before and therefore generally avoid using getText or getSublistText.

The reason for this error is answered very well here by Will Charbonneau. Netsuite Invalid API usage. You must use getValue to return the value set with setValue

The help for that, which Will also links, can be found here Record.getText(options)

The way I would tackle this would be to lookup the value on the fly. You might need to change the query I used a little, but the principle remains the same.

I've reworked the shape of this a little, and included use of SuiteQL, so you would need the N/query module

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.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'job',
                    line: i
                });
                let jobResult = SuiteQL.runQuery({
                    query: 'SELECT companyname FROM job WHERE id = ?',
                    params:[jobNumber]
                });

                if(jobResult.length > 0)
                {
                    scriptContext.newRecord.setText({
                        fieldId: 'custbody_job_number',
                        value: jobResult[0].companyname
                    });
                    break;
                }
            }
        }
    }
    catch (e)
    {
        log.error({
            title: e.name,
            details: e.message
        });
    }
}

Hope that helps

Upvotes: 0

Related Questions