Passing MultiSelect field values from a Client script to Suitelet

When working with SuiteScripts in NetSuite, passing multi-select field values from a Client Script to a Suitelet requires proper encoding and decoding. Since multi-select fields return an array of values, we need to encode them before sending and decode them in the Suitelet.

Client Script: Encoding Multi-Select Values

Use encodeURIComponent() to safely pass multi-select values as a URL parameter:

define([‘N/currentRecord’], function(currentRecord) {

  function sendToSuitelet() {

    let rec = currentRecord.get();

    let multiSelectValues = rec.getValue({ fieldId: ‘custpage_multiselectfield’ }); // Example Multi-Select Field

    let uri = JSON.stringify(multiSelectValues); // Convert array to string

    let encoded = encodeURIComponent(uri);

    let suiteletUrl = ‘/app/site/hosting/scriptlet.nl?script=123&deploy=1&values=’ + encoded;

    window.open(suiteletUrl, ‘_blank’);

  }

  return { sendToSuitelet };

});

Suitelet: Decoding Multi-Select Values

In the Suitelet, use decodeURIComponent() to retrieve and parse the values:

define([‘N/ui/serverWidget’], function(serverWidget) {

  function onRequest(context) {

    if (context.request.method === ‘GET’) {

      let encodedValues = context.request.parameters.values;

      let decodedValues = JSON.parse(decodeURIComponent(encodedValues)); // Convert back to array

       

      log.debug(‘Multi-Select Values’, decodedValues);

       

      context.response.write(‘Received Values: ‘ + decodedValues.join(‘, ‘));

    }

  }

  return { onRequest };

});

This approach ensures that special characters and multiple values are correctly handled when passing data between scripts.

Key Points:

Encode multi-select values using encodeURIComponent() before passing to the Suitelet.

Decode in the Suitelet using decodeURIComponent() and parse back to an array.

✅ Ensure multi-select values are properly formatted to avoid errors in URL parameters.

Leave a comment

Your email address will not be published. Required fields are marked *