How do I create user event suitescript that create...
# ask-stanley-ai
s
How do I create user event suitescript that creates a button and when that button is clicked, a SuiteLet runs in the background and creates a quote record?
c
AI generated answer (using internal Slack threads):
To create a user event SuiteScript that creates a button and when that button is clicked, a SuiteLet runs in the background and creates a quote record, you can follow these steps: 1. Create a User Event Script: This script will add a button to your record form. You can use the
beforeLoad
function in a User Event Script to add a button to your form. Here is a sample code snippet:
Copy code
javascript
function beforeLoad(context) {
    if (context.type !== context.UserEventType.CREATE)
        return;

    var form = context.form;
    var field = form.addField({
        id: 'custpage_button',
        type: serverWidget.FieldType.INLINEHTML,
        label: ' '
    });

    var script = "<input type='button' value='Create Quote' onclick='redirectToSuitelet()'>";
    field.defaultValue = script;
}
2. Create a Client Script: This script will be triggered when the button is clicked. It will redirect to the Suitelet. Here is a sample code snippet:
Copy code
javascript
function redirectToSuitelet() {
    var url = url.resolveScript({
        scriptId: 'customscript_your_suitelet_script_id',
        deploymentId: 'customdeploy_your_suitelet_deployment_id',
        returnExternalUrl: true
    });

    window.location = url;
}
3. Create a Suitelet: This Suitelet will be responsible for creating the quote record. Here is a sample code snippet:
Copy code
javascript
function onRequest(context) {
    if (context.request.method === 'GET') {
        var quoteRecord = record.create({
            type: record.Type.ESTIMATE, // 'Estimate' is the record type for Quotes
            isDynamic: true
        });

        // Set fields on the quote record as needed
        // quoteRecord.setValue('entity', customerId);
        // quoteRecord.setValue('item', itemId);
        // ...

        var quoteId = quoteRecord.save();

        context.response.write('Quote created with ID: ' + quoteId);
    }
}
Remember to replace
'customscript_your_suitelet_script_id'
and
'customdeploy_your_suitelet_deployment_id'
with your actual Suitelet script ID and deployment ID. ****** 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.851226687
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.846491277
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.842876196
Please provide your feedback on this answer.