Tuesday, November 27, 2018

Role-based Value Filtering for Custom List/Record Fields

Custom fields of type List/Record can be populated by custom lists in NetSuite.  However, certain business scenarios may require to filter list values based on the current user's role.  For instance, it may be necessary to show only a certain set of list options to a Sales Staff role and show a different set of list options to the Sales Manager role.  Custom lists do not yet support this feature, but the behavior can be emulated using a custom record type and some SuiteScript.

The purpose of the custom record type is to replace the custom list by representing each list option with a custom record.  Each custom record holds a list entry value, text, and also the roles to which the entry is visible.  To create this custom record, follow these steps

1. Navigate to Customization > Lists, Records, & Fields > Record Types > New.

2. Create the following custom record fields:

-    Name: Role, Type: Multiple Select, List/Record: Role
-    Name: Field Text, Type: Free-Form Text
-    Name: Field Value, Type: Free-Form Text

  • Once the custom record is created, create several instances.
  • After creating the custom records, create and deploy a Suitelet with the following code:

*****
function suiteletForm(request, response)
{
    if(request.getMethod() == "GET")
    {
        var role = nlapiGetContext().getRole();
        var form = nlapiCreateForm('Suitelet Form');
       
        var fld = form.addField('custpage_formtype', 'select', 'Custom Form Type: ');
        fld.addSelectOption('', '');

        var filters = new Array();
        filters.push(new nlobjSearchFilter('custrecord_field_role', null, 'anyof', role));
       
        var columns = new Array();
        columns.push(new nlobjSearchColumn('custrecord_field_text'));
        columns.push(new nlobjSearchColumn('custrecord_field_value'));
       
        var results = nlapiSearchRecord('customrecord_custom_list_value', null, filters, columns);
       
        for(var i=0; i<results.length && results != null; i++)
        {
            var res = results[i];
            fld.addSelectOption(res.getValue('custrecord_field_value'), res.getValue('custrecord_field_text'));
        }
       
        form.addSubmitButton('Create Custom Form');
       
        response.writePage(form);
    }
}
*****

The resulting Suitelet form will contain a select field whose options are filtered based on the role the user logged in with.  The same code can be modified slightly and used in a User Event.

No comments:

Post a Comment