Why is my suitescript not executing?

On a sales order I want to see if the items ordered were in stock when the order is placed. I have added a checkbox to the body that is checked when the script runs, and a checkbox on the line to be checked if quantity is less than or equal to available. But it doesn’t seem to run. I have it deployed to the sales order, it is active, and released. Is there something else I am missing?

/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/

define([‘N/record’], function (record) {
/**
* This function is triggered before a Sales Order record is submitted
* @param {Object} context
* @param {Record} context.newRecord – New record being submitted
* @param {Record} context.oldRecord – Old record before edit (only for edit events)
* @param {string} context.type – Trigger type
* @param {Form} context.form – Current form (only available in beforeLoad event)
*/
function beforeSubmit(context) {
try {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var newRecord = context.newRecord;
var scriptExecuted = newRecord.getValue({
fieldId: ‘custbodyscript_executed’
});

// Check if the script has already been executed
if (!scriptExecuted) {
var lineItemCount = newRecord.getLineCount({
sublistId: ‘item’
});

for (var i = 0; i < lineItemCount; i++) {
var quantity = newRecord.getSublistValue({
sublistId: ‘item’,
fieldId: ‘quantity’,
line: i
});

var availableQuantity = newRecord.getSublistValue({
sublistId: ‘item’,
fieldId: ‘quantityavailable’,
line: i
});

if (quantity <= availableQuantity) {
newRecord.setSublistValue({
sublistId: ‘item’,
fieldId: ‘custcol2’,
line: i,
value: true
});
}
}

// Set the custom field to indicate that the script has been executed
newRecord.setValue({
fieldId: ‘custbodyscript_executed’,
value: true
});
}
}
} catch (error) {
log.error({
title: ‘Error’,
details: error.message
});
}
}

return {
beforeSubmit: beforeSubmit
};
});

 

Rookie Asked on May 24, 2023 in SuiteScript.
Add Comment
2 Answer(s)

For anyone who finds this go ahead and double check your field names.

Rookie Answered on May 24, 2023.
Add Comment

can you share any log error

Rookie Answered on May 26, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor