write script to save opportunity deleted lines in ...
# ask-stanley-ai
m
write script to save opportunity deleted lines in another custom record before being deleted
c
AI generated answer (using internal Slack threads):
To save opportunity deleted lines in another custom record before being deleted, you can use a User Event script. The script will be triggered before the record is deleted (beforeSubmit function with DELETE context). In this function, you can create a new custom record and populate it with the data from the opportunity line that is about to be deleted. Here is a sample script:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/search'],

function(record, search) {

    function beforeSubmit(scriptContext) {
        if (scriptContext.type === scriptContext.UserEventType.DELETE) {
            var opportunityRecord = scriptContext.newRecord;
            var lineCount = opportunityRecord.getLineCount({sublistId: 'item'});
            
            for (var i = 0; i < lineCount; i++) {
                var item = opportunityRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    line: i
                });
                var quantity = opportunityRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    line: i
                });
                var rate = opportunityRecord.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'rate',
                    line: i
                });
                
                var customRecord = record.create({
                    type: 'customrecord_your_custom_record', // replace with your custom record type
                });
                customRecord.setValue({
                    fieldId: 'custrecord_item', // replace with your custom field id
                    value: item
                });
                customRecord.setValue({
                    fieldId: 'custrecord_quantity', // replace with your custom field id
                    value: quantity
                });
                customRecord.setValue({
                    fieldId: 'custrecord_rate', // replace with your custom field id
                    value: rate
                });
                customRecord.save();
            }
        }
    }

    return {
        beforeSubmit: beforeSubmit
    };
});
This script will create a new custom record for each line in the opportunity that is about to be deleted. The custom record will contain the item, quantity, and rate from the opportunity line. Please replace 'customrecord_your_custom_record', 'custrecord_item', 'custrecord_quantity', and 'custrecord_rate' with your actual custom record type and custom field ids. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.818016529
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.81695956
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.807809949
Please provide your feedback on this answer.