USER EVENT SCRIPT 2.0 TO LOAD A RECORD, READ IT'S VALUE AND SET A FIELD VALUE


Email : info@smilingcoders.com
Below code loads a sales order, reads it's sublist values and sets value of a custom field.
Email : info@smilingcoders.com
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record','N/log'], // modules used
    function(record,log) {
   
        function afterSubmit(context) {
            if (context.type !== context.UserEventType.DELETE)
            {
           
                     var rec_id = context.newRecord.id; // get record id
                   
                     log.debug({
                      title: 'rec_id',
                      details: rec_id
                      });
                   
                     var load_so =  record.load({
                        type: record.Type.SALES_ORDER,
                        id: rec_id,
                        isDynamic: false,
                    });
                   
                     var line_count = load_so.getLineCount({
                        sublistId: 'item'
                     });
                   
                     for(var i=1;i<=line_count;i++)
               {
             
                       var sub_field1 = load_so.getSublistText({
                        sublistId: 'item',
                        fieldId: 'custcol_sublistfield1',
                        line: i
                    });
                     
               
                     
                       log.debug({
                        title: 'sub_field1',
                        details: sub_field1
                        });
                     
                   
                     
                       if(sub_field1=='test1')
                     
                       {
                     
                   
                     
                                   load_so.setValue({
                                    fieldId: 'custbody_tran_field1',
                                    value: '7',
                                 
                                });
                                   break;
                       }
               }// for i
                   
                     load_so.save();
                   
            }//not delete
     
        }//after submit
        return {
         
            afterSubmit: afterSubmit
        };
    });
Email : info@smilingcoders.com

Comments

Popular posts from this blog

Calling User Event Script from another User Event

Understanding SuiteScript 2.0

HOW TO SET SUBLIST SUB RECORD VALUES IN SUITE SCRIPT 1.0 AND 2.0