How to get suitelet parameter in scheduled script?

I have created scheduled script to create custom sublist. Now I want to post sublist data to scheduled script. I have created task. But now I am not getting sublist data parameter value in scheduled script. Please Help!

 

//Suitelet code

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * … more options …
 */
define([‘N/ui/serverWidget’, ‘N/record’, ‘N/search’, ‘N/runtime’,’N/task’], function (serverWidget, record, search, runtime,task) {
    function onRequest(context) {
      var request = context.request;
      var response = context.response;
      if (request.method === ‘GET’) {
        try {
          var form = serverWidget.createForm({ title: ‘Funds Form’ });
          log.debug(“form”, form);
          var script=runtime.getCurrentScript();
          //log.debug(“script”,script);
          var scriptId= script.id;
          log.debug(“scriptId”,scriptId)
        form.addSubmitButton({ label: ‘Transfer Funds’ });
          //Sublist Code Bigin…….
          var empSublist = form.addSublist({
            id: ‘custpage_payroll’,
            type: serverWidget.SublistType.LIST,
            label: ‘EMPLOYEE PAYROLL INFORMATION’
          });
          empSublist.addField({
            id: ‘custpage_sublistemployee’,
            type: serverWidget.FieldType.TEXT,
            label: ‘Employee’
          });
          empSublist.addField({
            id: ‘custpage_sublistnetpay’,
            type: serverWidget.FieldType.CURRENCY,
            label: ‘Net Pay’
          });
          empSublist.addField({
            id: ‘custpage_sublisttaxdeduction’,
            type: serverWidget.FieldType.CURRENCY,
            label: ‘Tax Deduction’
          });
          empSublist.addField({
            id: ‘custpage_sublisttotal’,
            type: serverWidget.FieldType.CURRENCY,
            label: ‘Total’
          });
          var empData = [  { emp: ‘mona’, netPay: 7000, tax: 50, total: 7050 },  { emp: ‘Riya’,netPay: 5000, tax: 40, total: 5050 }];
          for (var i = 0; i < empData.length; i++) {
            empSublist.setSublistValue({
              id: ‘custpage_sublistemployee’,
              line: i,
              value: empData[i].emp
            });
            empSublist.setSublistValue({
              id: ‘custpage_sublistnetpay’,
              line: i,
              value: empData[i].netPay
            });
            empSublist.setSublistValue({
                id: ‘custpage_sublisttaxdeduction’,
                line: i,
                value: empData[i].tax
              });
              empSublist.setSublistValue({
                id: ‘custpage_sublisttotal’,
                line: i,
                value: empData[i].total
              });
          }
          response.writePage(form);
        } catch (error) {
          log.debug(‘Error’, error);
      }
      } else if (context.request.method === ‘POST’) {
         log.debug(“Suitelet is posting.”)
         var empData = [];
            var lineCount = request.getLineCount({
                group: ‘custpage_payroll’
            });
            for (var i = 0; i < lineCount; i++) {
                empData.push({
                    emp: request.getSublistValue({
                        group: ‘custpage_payroll’,
                        name: ‘custpage_sublistemployee’,
                        line: i
                    }),
                    netPay: request.getSublistValue({
                        group: ‘custpage_payroll’,
                        name: ‘custpage_sublistnetpay’,
                        line: i
                    }),
                    tax: request.getSublistValue({
                      group: ‘custpage_payroll’,
                      name: ‘custpage_sublisttaxdeduction’,
                      line: i
                  })
                });
            }
            log.debug(“empData”,empData);
         var scriptTask = task.create({
          taskType: task.TaskType.SCHEDULED_SCRIPT,
          scriptId: “customscript_scheduler_get_sublist_data”,
          deploymentId: “customdeploy_scheduler_get_sublist_data”,
          params: {
            ‘custscriptcustscript_emp_data’: JSON.stringify(empData)
          }
          });
          var scriptTaskId = scriptTask.submit();
          log.debug(“scriptTaskId”, scriptTaskId);
      }
  }
  return{
    onRequest:onRequest
  }
  })
//Scheduled script code
/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */
 define([‘N/runtime’, ‘N/record’, ‘N/search’, ‘N/config’, ‘N/ui/serverWidget’,’N/redirect’,’N/url’],
 function(runtime, record, search, config, serverWidget,redirect,url) {
    function execute(context) {
        try{
            var getParameter=runtime.getCurrentScript().getParameter({name: ‘custscriptcustscript_emp_data’});
            log.debug(“getParameter”,getParameter);
}catch(error)
{
    log.debug(“error”,error);
}
}
return{
    execute:execute
}
});
Beginner Asked on March 8, 2023 in SuiteScript.
Add Comment
3 Answer(s)

Hi Maira,

I wouldn’t put the param as text but like this (without quotes):

 params: {
            custscriptcustscript_emp_data: JSON.stringify(empData)
          }
Rookie Answered on March 8, 2023.
Add Comment

Hi Maira,

Do your have output at line log.debug(“empData”,empData); ?

When I see your process, you will save empData with object inside.
When we use object you need to declare parameter then continue to value, reference object you can see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Sample in your code should like this

 

for (var i = 0; i < lineCount; i++) {
var emp = request.getSublistValue({

group: ‘custpage_payroll’,

name: ‘custpage_sublistemployee’,

line: i

}),

var netPay = request.getSublistValue({

group: ‘custpage_payroll’,

name: ‘custpage_sublistnetpay’,

line: i

}),

var tax = request.getSublistValue({

group: ‘custpage_payroll’,

name: ‘custpage_sublisttaxdeduction’,

line: i

})
// when use Object, need to declare parameter and value

empData.push({

'emp' : emp,

'netPay' : netPay,

'tax' : tax

});

}

log.debug(“empData”,empData);

 

Rookie Answered on March 8, 2023.
Add Comment

OK Thanks!

Beginner Answered on March 9, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Become a Sponsor   Become a Sponsor