3

I am new to Suitelet. My object is to redirect from Sales Order to Form Suitelet. I create a file.js to redirect from a userevent script to a suitelet.

function afterSubmit(scriptContext) {

  if (scriptContext.type != 'delete') {

   var salesOrder = scriptContext.newRecord;

   var orderId = salesOrder.getValue('tranid');
   var customer = salesOrder.getValue('entity');
   var date = salesOrder.getValue('trandate');
   var total = salesOrder.getValue('total');
   var financePrice = salesOrder
     .getValue('custbody_rmb_financing_price');
   var internalid = salesOrder.id;

   var arrParams = {
    custparam_orderid : orderId,
    custparam_customer : customer,
    custparam_date : date,
    custparam_total : total,
    custparam_financeprice : financePrice,
    custparam_id : internalid
   };
   salesOrder.save({
    ignoreMandatoryFields : true
   });

   redirect.toSuitelet({
    scriptId : 'customscript_rmb_salesorderfinance_suite',
    deploymentId : 'customdeploy_rmb_salesorderfinance_suite',
    params : arrParams
   });

  }

 }
I create another file to redirect and display data of sales order.

function(serverWidget, http, runtime, record) {

   /**
    * Definition of the Suitelet script trigger point.
    * 
    * @param {Object}
    *            context
    * @param {ServerRequest}
    *            context.request - Encapsulation of the incoming
    *            request
    * @param {ServerResponse}
    *            context.response - Encapsulation of the Suitelet
    *            response
    * @Since 2015.2
    */
   function processSalesOrderFinancing(context) {

    if (context.request.method == 'GET') {

     var form = serverWidget.createForm('Sales Order Financing');

     form
       .addField({
        id : 'custpage__sdr_financing_help',
        type : 'help',
        label : 'Please assign a price to the financing of this sales order, then click Submit Financing'
       });
     var scriptObj = runtime.getCurrentScript();

      var stOrder =
      scriptObj.getParameter("custparam_orderid");
     var stCustomer =  scriptObj.getParameter("custparam_customer");

     var stDate = scriptObj.getParameter("custparam_date");
     var stTotal = scriptObj.getParameter("custparam_total");

     var orderid = form.addField({
      id : 'custpage_rmb_order',
      type : 'text',
      label : 'order#'
     });

     form.updateDefaultValues({
      custpage_rmb_order : stOrder
     });

     orderid.updateDisplayType({
      displayType : serverWidget.FieldDisplayType.INLINE
     });
     var customer = form.addField({
      id : 'custpage_rmb_customer',
      type : 'text',
      label : 'Customer:Project'
     });

     form.updateDefaultValues({
      custpage_rmb_customer : stCustomer
     });

     customer.updateDisplayType({
      displayType : serverWidget.FieldDisplayType.INLINE
     });
     var date = form.addField({
      id : 'custpage_rmb_date',
      type : 'date',
      label : 'Date'
     });

     date.updateDisplayType({
      displayType : serverWidget.FieldDisplayType.INLINE
     });

     form.updateDefaultValues({
      custpage_rmb_date : stDate
     });

     var total = form.addField({
      id : 'custpage_rmb_total',
      type : 'currency',
      label : 'Total'
     });

     form.updateDefaultValues({
      custpage_rmb_total : stTotal
     });

     total.updateDisplayType({
      displayType : serverWidget.FieldDisplayType.INLINE
     });

     var financeprice = form.addField({
      id : 'custpage_rmb_fprice',
      type : 'currency',
      label : 'Finance Price'
     });

     var salesID = form.addField({
      id : 'custpage_rmb_orderid',
      type : 'text',
      label : 'LABEL'
     });

     salesID.updateDisplayType({
      displayType : serverWidget.FieldDisplayType.HIDDEN
     });

     form.addSubmitButton('Submit Financing RMB');

     context.response.writePage({
      pageObject : form
     });
    } }

After that code, I don't get the data of sales order on the the form. I don't get the result that I want. getParameter returns null.

enter image description here

If you have any ideas to help me, I will be thankful.

Wyck
  • 10,311
  • 6
  • 39
  • 60
spring_flower
  • 71
  • 1
  • 1
  • 9

2 Answers2

5

When you use the runtime.getCurrentScript().getParameter() function, you're trying to retrieve the parameters that are defined on the script deployment record.

When you use the redirect.toSuitelet() function and pass in the params property, it will append those parameters to the end of the URL. To access the parameters from there in your Suitlet, you should use

var stCustomer = context.request.parameters['custparam_customer'];

EDIT to add full example:

User Event Script

define(['N/log', 'N/redirect'], function (log, redirect) {
    function afterSubmit() {
        var arrParams = {
            custparam_customer: 1234
        };

        redirect.toSuitelet({
            scriptId: 'customscript_suitelet_test',
            deploymentId: 'customdeploy_suitelet_test',
            parameters: arrParams
        });
    }

    return {
        afterSubmit: afterSubmit
    };
});

Suitelet

define(['N/log', 'N/ui/serverWidget'], function (log, ui) {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            var form = buildForm();


            context.response.writePage(form);
        }

        function buildForm() {
            var form = ui.createForm({title: 'Test Params'});
            var customerField = form.addField({
                id: 'custpage_customer',
                type: 'text',
                label: 'Customer Param'
            }).updateDisplayType({ displayType: ui.FieldDisplayType.INLINE });

            customerField.defaultValue = context.request.parameters['custparam_customer'];

            return form;
        }
    }

    return {
        onRequest: onRequest
    };
});

Results in: enter image description here

Mike Robbins
  • 3,184
  • 15
  • 20
  • Many thanks @Mike for your reply , but it doesn't work as well – spring_flower Dec 14 '17 at 10:00
  • the return of the log for the value stCustomer is undefined `log .debug({ title : 'debug storder retrieve param from sales order', details : 'The Value of stCustomer is :' + stCustomer });` – spring_flower Dec 14 '17 at 14:52
  • I've updated my answer to provide a full working example. – Mike Robbins Dec 14 '17 at 17:50
  • Looks like the problem is in your call to `redirect.toSuitelet()` your specifying `params` but it should be `parameters`. I made the same mistake building the full example. :) – Mike Robbins Dec 14 '17 at 17:55
  • I owe you this detailed example but it doesn't work again,the value is undefined again – spring_flower Dec 15 '17 at 10:43
  • If the request method = POST, then accessing like: context.request.parameters.custparam_xxx works for me – Skromak Jan 14 '20 at 12:05
  • what about on get? I am doing this using a "get" method in suitelet – tyne Jul 07 '20 at 14:13
  • @tyne You can still access GET query string parameters the same way. `context.request.parameters['query_string_param'];` – Mike Robbins Jul 07 '20 at 18:48
  • hi @MikeRobbins , thank you for answering. But I did that already but same result, also i tried context.request.parameters.query_samting but it didn't work. – tyne Jul 07 '20 at 23:12
-1

Hi please find the below code:

User Event:

define(['N/record','N/redirect'],

function(record,redirect) {

function afterSubmit(scriptContext) {

   if (scriptContext.type != 'delete') {

            var salesOrder = scriptContext.newRecord;

            var orderId = salesOrder.getValue('tranid');
            var customer = salesOrder.getValue('entity');
            var date = salesOrder.getValue('trandate');
            var total = salesOrder.getValue('total');
            var financePrice = salesOrder.getValue('custbody_rmb_financing_price');
            var internalid = salesOrder.id;

            var arrParams = {
                custparam_orderid : orderId,
                custparam_customer : customer,
                custparam_date : date,
                custparam_total : total,
                custparam_financeprice : financePrice,
                custparam_id : internalid
            };
        /*  salesOrder.save({
                ignoreMandatoryFields : true
            }); */

            redirect.toSuitelet({
                scriptId : 'customscript_sut_so_test',
                deploymentId : 'customdeploy_sut_so_test',
                parameters :  {
                'custparam_orderid' : orderId,
                'custparam_customer' : customer,
                'custparam_date' : date,
                'custparam_total' : total,
                custparam_financeprice : financePrice,
                'custparam_id' : internalid
            }
            });


        }
    }


    return {
       // beforeLoad: beforeLoad,
      //  beforeSubmit: beforeSubmit,
        afterSubmit: afterSubmit
    };

});

Suitelet:

define(['N/record', 'N/runtime', 'N/search', 'N/ui/serverWidget'],

function(record, runtime, search, serverWidget) {

    function onRequest(context) {

                if (context.request.method == 'GET') {

                    var form = serverWidget.createForm('Sales Order Financing');

                    form.addField({
                                id : 'custpage__sdr_financing_help',
                                type : 'help',
                                label : 'Please assign a price to the financing of this sales order, then click Submit Financing'
                            });
                    var scriptObj = context.request.parameters;

                    var stOrder = scriptObj.custparam_orderid;
                    var stCustomer =  scriptObj.custparam_customer;
                    var stDate = scriptObj.custparam_date;
                    var stTotal = scriptObj.custparam_total;

                    var orderid = form.addField({
                        id : 'custpage_rmb_order',
                        type : 'text',
                        label : 'order#'
                    });

                    form.updateDefaultValues({
                        custpage_rmb_order : stOrder
                    });

                    orderid.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.INLINE
                    });
                    var customer = form.addField({
                        id : 'custpage_rmb_customer',
                        type : 'text',
                        label : 'Customer:Project'
                    });

                    form.updateDefaultValues({
                        custpage_rmb_customer : stCustomer
                    });

                    customer.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.INLINE
                    });
                    var date = form.addField({
                        id : 'custpage_rmb_date',
                        type : 'text',
                        label : 'Date'
                    });

                    date.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.INLINE
                    });

                    form.updateDefaultValues({
                        custpage_rmb_date : stDate
                    });

                    var total = form.addField({
                        id : 'custpage_rmb_total',
                        type : 'currency',
                        label : 'Total'
                    });

                    form.updateDefaultValues({
                        custpage_rmb_total : stTotal
                    });

                    total.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.INLINE
                    });

                    var financeprice = form.addField({
                        id : 'custpage_rmb_fprice',
                        type : 'currency',
                        label : 'Finance Price'
                    });

                    var salesID = form.addField({
                        id : 'custpage_rmb_orderid',
                        type : 'text',
                        label : 'LABEL'
                    });

                    salesID.updateDisplayType({
                        displayType : serverWidget.FieldDisplayType.HIDDEN
                    });

                }
                context.response.writePage(form);

            }

            return {
                onRequest: onRequest
            };

        });
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32