I am trying to create a script that when a custom ...
# suitescript
s
I am trying to create a script that when a custom button is selected on specific Cases, it looks through a sublist of custom records (which holds item, quantity, price, and other info) and creates a Quote using those custom records to populate the line items. I have been trying to perform this so when the button is clicked a function is performed from a client script in the file cabinet, but anything more than 5 items can take more than 30 seconds to load, and causes the browser to show NetSuite as unresponsive, which is not what my requester wants. Am I going about this in the right way? Is there a way to avoid the long load time or have it work in the backend so the user can still work? I am newer to SuiteScript so any assistance is appreciated! (Code in comments)
g
Depends on the script and what it is going. Sharing your code would likely help with responses.
s
For sure, here is the code for the button:
Copy code
const beforeLoad = (context) => {
            return logErrorIfNeeded("beforeLoad", function() {
                var currentRecord = context.newRecord;
    
                context.form.addButton({
                    id: 'custpage_quote_button',
                    label: 'Create Quote',
                    functionName: 'createQuoteForm'
                });
                context.form.clientScriptModulePath = "SuiteScripts/LX_CL_Quote_Line_Items.js";
            });
        };
        
        return {beforeLoad}
And the code for file cabinet client script:
Copy code
function createQuoteForm() {
            return logErrorIfNeeded("pageInit", function () {
                var currentRecObj = currentRecordMod.get();
                var caseId = currentRecObj.id;
                var caseRecord = record.load({
                    type: record.Type.SUPPORT_CASE,
                    id: caseId
                });
                
                var quoteRecord = record.create({
                    type: record.Type.ESTIMATE,
                    isDynamic: true
                });

                //set fields on quote
                quoteRecord.setValue({
                    fieldId: 'entity',
                    value: caseRecord.getValue({fieldId: 'company'})
                });
                ...
                ...
                ...

                var sublistName = 'recmachcustrecord_na_case';

                var itemLineCount = caseRecord.getLineCount({
                    sublistId: sublistName
                });

                log.debug("itemLineCount", itemLineCount);

                for (var i = 0; i < itemLineCount; i++) {

                    //get item fields from case
                    var itemSKU = caseRecord.getSublistValue({
                        sublistId: sublistName,
                        fieldId: 'custrecord_na_item',
                        line: i
                    });
                    ...
                    ...
                    ...

                    //add line
                    quoteRecord.selectNewLine({sublistId: 'item'});

                    //set line item fields
                    quoteRecord.setCurrentSublistValue({sublistId: "item", fieldId: "item", value: itemSKU, forceSyncSourcing: true});
                    ...
                    ...
                    ...

                    //commit line
                    quoteRecord.commitLine({sublistId: 'item'});
                }

                var quoteId = quoteRecord.save();

                var quoteUrl = url.resolveRecord({
                    recordType: 'estimate',
                    recordId: quoteId,
                    isEditMode: true
                });

                window.open(quoteUrl);
            })
                
        }

        return {
            pageInit: pageInit,
            createQuoteForm: createQuoteForm
        };
g
Hmm, ok so I am by no means an expert but I think you'll be somewhat limited with a client script. If you don't need it real-time I would say N/task but I don't think that is exposed to client scripts. So my next thought would be transfer all the record create logic to a suitelet and just pass the caseId to the suitelet as a param with redirect.toSuitelet or pass it in the URL with &caseid=1234 and grab it in the suitelet with context.request.parameters.caseid, do all the stuff then redirect to the new record when done. Or you could trigger N/task here if a background task is sufficient.
❤️ 1
s
Thank you! I found a thread that mentioned going that route but wanted to be sure - I'll try it out and see what's up. Appreciate the response!
👍 1