SuiteScript 2.0

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

Below code loads a sales order, reads it’s sublist values and sets value of a custom field.

/**
 *@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
        };
    });

Author

Akhil

Leave a comment