How to create User Event in Suite Script 2.0 in Netsuite?

  Hi All,


User Event script is server side script, it runs when certain type of action perfom on record example
create, copy, edit, xedit etc.Most records in Netsuite support user event script, user can still check if user event is allowed on record or not by seeing "Deployed to" field on deployment record.

User first have to create file in javascript and save it. You can define file name with keyword related to task it is performing. This will help in understanding for which functionality script is getting used just by seeing file.
Same applies to Script name ex: "NetsuiteGuru Update Tax Code" as a script name suggest that script written by Netsuite Guru is updating Tax code on the record it is applied.
How to createScript:

Step 1: Create javascript file,
Step 2: Go to Customization > Scripting > Scripts > New
how to create script in netsuite
New Script

Step 3: Upload file
Step 4: Enter Script Name and id.
how to add name and id on script record
Enter Name and ID

Step 5: Click Save and Deploy

Step 6: Enter Id , Apply to Record which is needed.
how deployment in netsuite looklike for user event
Enter Event type, Id , Applied To and Execute as role
**** Point to note:
a. User can uncheck Deployed and script deployment will not work. If script have many deployment then other deployment will work as expected.
b. If user want to run user event without issue or not dependent on role then set "Execute as Role" as administrator.
c. Check all role checkbox under audience to allow this for all role. User can also multiselect roles which are needed in dropdown using CTRL + Select.
d. While testing functionality user can select status as testing on deployment record/page.

Step 7: Save Record.

**** User event have 3 type of functions:


Before Load – event occurs when a read operation on a record takes place.
     A Before Load event occurs when a user clicks Edit or View on an existing record,
     or clicks New to create a new record.

Before Submit – event occurs when a record is submitted,
     but before the changes are committed to the database. A Before Submit event occurs
     when a user clicks Save or Submit on a record.

After Submit – event occurs after the changes to the record are committed to the database.
     An After Submit event occurs after a user clicks Save or Submit on a record.
Sequence of Actions .


Scripting Example:

 
 
Step 1: 
 Create Script like below :
 
 
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record'],
    function(record) {
        function beforeLoad(context) {
            //========add your before load logic here ==========

           //Sample allow only create type

           if (context.type !== context.UserEventType.CREATE)
                return;
            var record1 = context.newRecord;
            record1.setValue('custbody1', 'this is for before load'); 

 

  }
        function beforeSubmit(context) {
            //======add your before submit logic here============

          if (context.type !== context.UserEventType.CREATE)
                return;
            var record1 = context.newRecord;
            record1.setValue('custbody2', 'this is before submit'); 

 

  }
        function afterSubmit(context) {
            //===========add you after submit logic here ======

            if (context.type !== context.UserEventType.CREATE)
                return;
            var record1 = context.newRecord;
            
                var customer = record.create({
                    type: record.Type.CUSTOMER,
                    isDynamic: true
                });
                customer.setValue('email','test@test.com');
                customer.setValue('custbody3', 'this is after submit');
                try {
                    var callId = customer.save();
                    log.debug('record created successfully', 'Id: ' + callId);
                } catch (e) {
                    log.error(e.name);
                }
  }
        return {
            beforeLoad: beforeLoad,
            beforeSubmit: beforeSubmit,
            afterSubmit: afterSubmit
        };
    });
 
**** Other Important Details:
To go to script record: 
 

How to go to script record.

For Uploading File ** , it will automatically Detect version based on Script. Based on Script Type mentioned in Script , Netsuite will open the script screen  also auto populate functioned as described beforeLoad, beforeSubmit and afterSubmit






Please let me know if you have any issue.
All Suggestions are welcome.  

Thanks
Netsuite Guru

Comments

  1. Hi, I have created a simple UserEvent script for Purchase order transaction. Its working fine when PO transaction done through UI but its not working when using suiteTalk Web Serivices.

    ReplyDelete
    Replies
    1. Hi Kailash,
      Please check if there is any condition for context in script or on Deployment page.
      Look like some where context is selected as UI or User Interface.

      Delete
    2. I also have similar issue, I created Userevent script and it is not updating the sublist item values for one of the field.

      Delete
    3. Hi @akber
      Issue might be related to subsist field id.

      Delete

Post a Comment

Thanks for you message, please join us on Facebook and Linkedin