How to use SuiteScript to create a button that ope...
# ask-stanley-ai
s
How to use SuiteScript to create a button that opens a copy of the current case record in edit mode, with a custom field auto-populated with the original case?
c
AI generated answer (using internal Slack threads):
To create a button that opens a copy of the current case record in edit mode, with a custom field auto-populated with the original case, you can use a combination of User Event (UE) and Client Script (CS) in SuiteScript. Here's a step-by-step guide: 1. In your User Event script, use the
beforeLoad
function to add a button to the form. You can use the
addButton
method for this. The
functionName
parameter should be the name of the function that will be called in your Client Script when the button is clicked.
Copy code
javascript
function beforeLoad(context) {
    context.form.addButton({
        id: "custpage_copy_case_button",
        label: "Copy Case",
        functionName: "COPY_CASE_FUNCTION"
    });
    context.form.clientScriptModulePath = "SuiteScripts/copy_case_button_cs.js";
}
2. In your Client Script, define the function that will be called when the button is clicked. This function should create a new case record, copy the necessary fields from the current record, and then redirect the user to the new record in edit mode.
Copy code
javascript
function COPY_CASE_FUNCTION() {
    var currentRecord = currentRecordMod.get();
    var newRecord = record.copy({
        type: currentRecord.type,
        id: currentRecord.id,
        isDynamic: true
    });

    // Set the custom field with the original case
    newRecord.setValue({
        fieldId: 'custfield_original_case',
        value: currentRecord.id
    });

    var newRecordId = newRecord.save();

    var newRecordUrl = url.resolveRecord({
        recordType: 'supportcase',
        recordId: newRecordId,
        isEditMode: true
    });

    window.open(newRecordUrl);
}

return {
    COPY_CASE_FUNCTION: COPY_CASE_FUNCTION
};
Please note that you need to replace
'custfield_original_case'
with the actual ID of your custom field. Also, make sure that the Client Script is deployed on the case record and that the User Event script is deployed on the specific case form. ****** 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.85736084
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.848022461
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.845397949
Please provide your feedback on this answer.