Clean Up Content for Text Area Fields After Saving a Record
The following sample uses the large language model (LLM) to correct the text for the purchase description and the sales description fields of an inventory item record after the user saves the record. This sample also shows how to use the llm.generateText.promise
method.
To test this script after script deployment:
-
Go to Lists > Accounting > Items > New.
-
Select Inventory Item.
-
Enter an Item Name and optionally fill out any other fields.
-
Select a value for Tax Schedule in the Accounting subtab.
-
Enter text into the Purchase Description and Sales Description fields.
-
Click Save.
When you save, the script will trigger. The content in the Purchase Description and Sales Description fields will be corrected, and the record will be submitted.
This script sample uses the define
function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require
function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/llm'], (llm) => {
/**
* @param {Object} scriptContext The updated inventory item
* record to clean up typo errors for purchase description and
* sales description fields. The values are set before the record
* is submitted to be saved.
*/
function fixTypos(scriptContext) {
const purchaseDescription = scriptContext.newRecord.getValue({
fieldId: 'purchasedescription'
})
const salesDescription = scriptContext.newRecord.getValue({
fieldId: 'salesdescription'
})
const p1 = llm.generateText.promise({
prompt: `Please clean up typos in the following text:
${purchaseDescription} and return just the corrected text.
Return the text as is if there's no typo
or you don't understand the text.`
})
const p2 = llm.generateText.promise({
prompt: `Please clean up typos in the following text:
${salesDescription} and return just the corrected text.
Return the text as is if there's no typo
or you don't understand the text.`
})
// When both promises are resolved, set the updated values for the
// record
Promise.all([p1, p2]).then((results) => {
scriptContext.newRecord.setValue({
fieldId: 'purchasedescription',
value: results[0].value.text
})
scriptContext.newRecord.setValue({
fieldId: 'salesdescription',
value: results[1].value.text
})
})
}
return { beforeSubmit: fixTypos }
})