How to call client script function in Suitelet ```...
# suitescript
d
How to call client script function in Suitelet
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */

// this creates a Suitelet form that lets you write and send an email

define(['N/ui/serverWidget', 'N/email', 'N/runtime'],
    function(ui, email, runtime) {
        function onRequest(context) {
            if (context.request.method === 'GET') {
                var form = ui.createForm({
                    title: 'Demo Suitelet Form'
                });
                form.clientScriptModulePath = "./helloWorld.js";
                var subject = form.addField({
                    id: 'subject',
                    type: ui.FieldType.TEXT,
                    label: 'Subject'
                });
                subject.layoutType = ui.FieldLayoutType.NORMAL;
                subject.breakType = ui.FieldBreakType.STARTCOL;
                subject.isMandatory = true;
                var recipient = form.addField({
                    id: 'recipient',
                    type: ui.FieldType.EMAIL,
                    label: 'Recipient email'
                });
                recipient.isMandatory = true;
                var message = form.addField({
                    id: 'message',
                    type: ui.FieldType.TEXTAREA,
                    label: 'Message'
                });
                message.displaySize = {
                    width: 60,
                    height: 10
                };
                form.addSubmitButton({
                    label: 'Send Email'
                });
              
              	form.addButton({
                    id: 'custpage_sample_button',
                    label: 'Sample Button',
                    functionName: 'customSubmit'
                });
                context.response.writePage(form);
            } else {
                var request = context.request;
                email.send({
                    author: runtime.getCurrentUser().id,
                    recipients: request.parameters.recipient,
                    subject: request.parameters.subject,
                    body: request.parameters.message
                });
            }
        }
        return {
            onRequest: onRequest
        };
    });
e
You don't call Client Script functions from a Suitelet. The Suitelet runs server-side while the Client Script runs in the user's browser. They share no context.
d
Thanks
e
Looks like you're trying to wire up a button that will POST back to the Suitelet?
d
Yes @erictgrubaugh
I want to create suitelet which will use common utility file so was trying with client script.
@erictgrubaugh Can you help me here I am a newbie.
e
I see
Check out the Help section
SuiteScript 2.0 Custom Modules
✔️ 1
Make a custom module that exports the common properties and functions you need, then import the custom module into both your Suitelet and Client scripts (or whatever else needs it)
d
Thanks for the help I will look into it.
n
Hello there, sorry to jump into this discussion but I have an issue with functionName... It works if my variable is a string, but since mine is an object it does not seem to work... In my client script the dialog shows [object Object],[object Object],[object Object]... but my log in my suitelet is good... Any idea please? 🙏
Morning' @erictgrubaugh, you're the best dev I see around, would you have any idea for me please?
e
Share your code
But in general, the
functionName
property is not used to pass data into the click handler function. The Suitelet and the Client Script don't share any context, so my typical approach is to store the relevant data on the record, in the cache, or in the session (in order of preference).
n
I don't have a specific record since I'm working on a suitelet. I can't find a way to pass my JSON to the Client Script.
e
Is this a UI Suitelet or a backend Suitelet?
n
Just to have everyone knowing, thanks to @erictgrubaugh. I created a field, set a default value and then read the value client side.
325 Views