I am trying to create a script that populates a cu...
# suitescript
t
I am trying to create a script that populates a custom body field with the current date when the Vendor Return Authorization is approved. Would anyone have any insight on how to do this? ChatGPT came up with this but I'm thinking there may be a obvious issue someone with some experience could easily point out.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/runtime'], function(record, runtime) {
function afterSubmit(context) {
var newRecord = context.newRecord;
var approvalStatus = newRecord.getValue({fieldId: 'approvalstatus'});
if (approvalStatus === '2') {
var currentDate = new Date();
// Update the custom field with the current date
record.submitFields({
type: newRecord.type,
id: newRecord.id,
values: {
custbody34: currentDate
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
}
}
return {
afterSubmit: afterSubmit
};
});
i
this looks like it would run everytime you save it so it would be updating the custom field to the last saved date. you will want to add logic to only trigger if the custom field is empty. Also not sure if approvalstatus would be a valid field, you might need to use orderstatus or status.
t
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/runtime'], function(record, runtime) {
function afterSubmit(context) {
var newRecord = context.newRecord;
// Get the current value of 'custbody34'
var custbody34 = newRecord.getValue({fieldId: 'custbody34'});
// Check if 'custbody34' is null or empty
if (!custbody34) {
var status = newRecord.getValue({fieldId: 'status'});
if (status === '2') {
var currentDate = new Date();
// Update the custom field with the current date
record.submitFields({
type: newRecord.type,
id: newRecord.id,
values: {
custbody34: currentDate
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
}
}
}
return {
afterSubmit: afterSubmit
};
});
Does that look better?
Would this work if the approve button was clicked?