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.
-
In the Item Name/Number field, enter an item name. Optionally, fill out any other fields.
-
On the Purchasing/Inventory subtab, enter text in the Purchase Description field.
On the Sales/Pricing subtab, enter text in the Sales Description field.
-
Click Save.
When you save, the script runs. The content in the Purchase Description and Sales Description fields is corrected, and the record is 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.1 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 only 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 only 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].text
})
scriptContext.newRecord.setValue({
fieldId: 'salesdescription',
value: results[1].text
})
})
}
return { beforeSubmit: fixTypos }
})