Vernita
Vernita

Reputation: 103

Userevent button to trigger clientscript and open suitelet

I have a button created with a userevent script which appears on a transaction record userevent button This is supposed to trigger a client script on click which in turn is supposed to redirect to a suitelet.

So far, I am not getting an error message though it is not doing anything either. I am following SuiteAnswers id: 93513 for the basic structure as I am trying to pass the parameters of the record the user is on, to the suitelet. i.e. if the user clicks on the 'add note' button while on invoice(internal id-38060), I want that value to be set in the Suitelet form under the field 'Parent transaction'. Where am I going wrong with my script? edit: there are two different variations of this that now work. The first is using only a userevent script and suitelet (getting rid of the client script) and the other is the tweaked client script to include the n/currentRecord module Userevent Script:

   /**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(["N/url", "N/record", "N/runtime", "N/ui/serverWidget"], function (
    url,
    record,
    runtime,
    serverWidget
) {
    var exports = {}

    /**
     * @param {UserEventContext.beforeLoad} context
     */
    function beforeLoad(context) {
        if (
            context.type == context.UserEventType.EDIT ||
            context.type == context.UserEventType.VIEW
        ) {
            var record = context.newRecord
            var recordId = record.id
            var recordType = record.type

            var form = context.form
            //the internal id of the client script file in the filing cabinet
             form.clientScriptFileId = 4910

            var suiteletURL = url.resolveScript({
                scriptId: "customscript_suitelet_notes",
                deploymentId: "customdeploy_suitelet_notes",
                returnExternalUrl: true,
                params: { recordId: recordId, recordType: recordType },
            })

            form.addButton({
                id: "custpage_add_note",
                label: "Add Note",
                functionName:"onclick_callforSuitelet"
              
              
              
//                  "window.open('" +
//                  suiteletURL +
                    //"','PopUpWindow','height=500,width=1000,left=120,top=100,resizable=yes,scrollbars=yes,//toolbar=yes,menubar=no,location=no,directories=no, status=yes');",
            })
        }
    }

    
    return {beforeLoad:beforeLoad}
})

Client script:

   /**
     *@NApiVersion 2.x
     *@NScriptType ClientScript
     */
    define(["N/record", "N/url",'N/currentRecord'], function (record, url,currentRecord) {
      /**
       * @param {ClientScriptContext.pageInit} context
       * @param {ClientScriptContext.onclick_callforSuitelet} context
       */
     function pageInit() {}
      function onclick_callforSuitelet() {
        var record = currentRecord.get();
        var recordId = record.id;
        var recordType = record.type;
        log.debug("recId", recordId);
        log.debug("recType", recordType);
    
        //this is the script id on the script record for the suitelet (not the deployment)
        var suiteletURL = url.resolveScript({
          scriptId: "customscript_suitelet_notes",
          //this is the id from the deployment record for the suitelet
          deploymentId: "customdeploy_suitelet_notes",
          params: { recordId: recordId, recordType: recordType },
        });
        document.location = suiteletURL;
        log.debug("suiteletURL", suiteletURL);
      }
    
      return {
        onclick_callforSuitelet: onclick_callforSuitelet,
      pageInit: pageInit,
      };
    });

Suitelet script

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
define(["N/ui/serverWidget", "N/log", "N/record", "N/url"], function (
    serverWidget,
    log,
    record,
    url
) {
    /**
     * @param {SuiteletContext.onRequest} context
     */
    function onRequest(context) {
        if (context.request.method === "GET") {
            // Section One - Forms
            var invoice_id = parseInt(context.request.parameters.recordId)

            var form = serverWidget.createForm({
                id: "notes",
                title: "Notes",
            })

            var customerGroup = form.addFieldGroup({
                id: "customerDetails",
                label: "Customer Details",
            })
            customerGroup.isSingleColumn = false

            form.addSubmitButton({
                label: "Submit",
            })

            var select = form.addField({
                id: "custpage_source",
                type: serverWidget.FieldType.SELECT,
                label: "Source of Communication",
                container: "customerDetails",
            })
            //the value is the internal id of the option under customisation>lists,records and forms>lists>'source of communication' list
            select.addSelectOption({
                value: 1,
                text: "Phone",
            })

            select.addSelectOption({
                value: 2,
                text: "Email",
            })

            select.addSelectOption({
                value: 3,
                text: "Website Contact Form",
            })

            select.addSelectOption({
                value: 4,
                text: "Other",
            })

            form.addPageLink({
                type: serverWidget.FormPageLinkType.CROSSLINK,
                title: "Invoice",
                url:
                    "https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
                    invoice_id,
            })

            var parentTransaction = form.addField({
                id: "custpage_parent_transaction",
                type: serverWidget.FieldType.SELECT,
                label: "Parent Transaction",
                container: "customerDetails",
            })

            parentTransaction.addSelectOption({ value: invoice_id, text: invoice_id })

            // form.updateDefaultValues({
            //   custpage_recordurl:
            //     "https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
            //     recId,
            // });
            context.response.writePage(form)

            function getBaseUrl() {
                return url.resolveRecord({
                    recordType: record.Type.INVOICE,
                })
            }
            // Section Two - Tabs - See "Steps for Adding a Tab to a Form"
            // Section Three - Sublist - See "Steps for Adding a Sublist to a Form"
        } else {
            // Section Four - Output - Used in all sections

            var delimiter = /\u0001/
            var sourceField = context.request.parameters.custpage_source
            var parentField = context.request.parameters.custpage_parent_transaction

            var invoiceRecord = record.load({
                type: record.Type.INVOICE,
                id: parentField,
            })
            log.debug("parent record", invoiceRecord)

            //   context.response.write(
            //     "You have entered:" + "<br/>  Name: " + sourceField
            //   );
            var recObj = record.create({
                type: "customrecord_user_notes",
            })
            recObj.setValue({ fieldId: "custrecord_source", value: sourceField })
            recObj.setValue({
                fieldId: "custrecord_parent_transaction",
                value: parentField,
            })
            var userNote = recObj.save({})
            var notesFieldUpdate = record.submitFields({
                type: record.Type.INVOICE,
                id: parentField,
                values: { custbody_notes_check: "CHECK NOTES" },
            })
            log.debug("notesfield", notesFieldUpdate)

            context.response.write("Note Created")
        }
    }
    return {
        onRequest: onRequest,
    }
})

At the moment, I haven't completed the suitelet form though it is working correctly in that it is creating a instance of a custom record using the field data entered by the user: suitelet

I hadn't defined the record context which is why it was doing nothing. I could only see it after looking at the console log and seeing an error message in that

Thanks Sitaram and ZSwitaj for their suggestions

Upvotes: 1

Views: 4696

Answers (2)

ZSwitaj
ZSwitaj

Reputation: 13

As far as I know, you don't have access to the context within your client script in a user-defined function like yours. log.debug() also tends to not work well with client scripts, using console.log() and opening the browser console tends to lead to better debugging.

If you change your log.debug() functions to console.log() and run the script (aka press your button), do recordId and recordType logs give you expected results?

According to SuiteAnswer 93513 you should be able to use currentRecord.get() to get your current record. May not solve all your problems, but hopefully can get you rolling again!

Upvotes: 1

Sitaram
Sitaram

Reputation: 16

form.addButton({
            id: "custpage_add_note",
            label: "Add Note",
            functionName: "onclick_callforSuitelet()",
          });

Use onclick_callforSuitelet instead of onclick_callforSuitelet() in the function name.

Upvotes: 0

Related Questions