Filter Locations based on subsidiary in Subsidiary record

Set a field to choose a location in the subsidiary record. The list should display the locations of that particular subsidiary only.

This can be achieved by creating a virtual field displaying the results of a saved search which provides the location values for that subsidiary only. Then the selected value is stored in the custom field created in the subsidiary record.

User event script is deployed for the functionality.

define(['N/record', 'N/search','N/ui/serverWidget'],
(record, search, serverWidget) => 
{
    const beforeLoad = (scriptContext) => 
    {
        try 
        {
            let form = scriptContext.form;
            let subsidiaryRec = scriptContext.newRecord;
            let recordId = subsidiaryRec.id;
            if(scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT)
            {
                let defaultFromLoc = form.getField({id: 'custrecord_jj_default_to_location'});
                if (defaultFromLoc) 
                {
                    defaultFromLoc.updateDisplayType({displayType: serverWidget.FieldDisplayType.HIDDEN});
                } 
            }
            if(scriptContext.type === scriptContext.UserEventType.EDIT)
            {
                let defaultFromLoc = subsidiaryRec.getValue("custrecord_jj_default_to_location");
                let locationField = form.addField(
                {
                    id: 'custpage_filtered_locations',
                    type: serverWidget.FieldType.SELECT,
                    label: 'DEFAULT FROM LOCATION'
                });
                locationField.isMandatory = false;
                if(defaultFromLoc)
                {
                    locationField.defaultValue = defaultFromLoc;
                }
                locationField.addSelectOption(
                {
                    value: '',
                    text: ' '
                });
                let locationList = [];
                if(!recordId)
                {
                    return locationList;
                }
                let locationSearch = search.create(
                {
                    type: 'location',
                    filters: [['subsidiary', 'is', recordId], 'AND', ['isinactive','is','F']],
                    columns: ['internalid', 'name']
                });
                locationSearch.run().each(function(result) 
                {
                    locationList.push(
                    {
                        id: result.getValue('internalid'),
                        name: result.getValue('name')
                    });
                    return true;
                });
                locationList.forEach(function (location) 
                {
                    locationField.addSelectOption(
                    {
                        value: location.id,
                        text: location.name
                    });
                });
            }
        } 
        catch(e) 
        {
            log.debug('error @ beforeLoad', e.message)
        }
    }
      
    const beforeSubmit = (scriptContext) => 
    {
        try
        {
            let newRecord = scriptContext.newRecord;
            let selectedVirtualFieldValue = newRecord.getValue("custpage_filtered_locations");
            newRecord.setValue(
            {
                fieldId: "custrecord_jj_default_to_location",
                value: selectedVirtualFieldValue?selectedVirtualFieldValue:null
            });
        }
        catch(e)
        {
            log.error("Error @ beforeSubmit", e.message);
        }
     }
     return {beforeLoad, beforeSubmit}
});

Leave a comment

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