Remove options from a record producer variable in native view

ameybhaisar
Tera Contributor

Hi All,

 

I have an incident record producer. There is a variable of select box type named as rp_subcategory. The choices in this fields gets populated based on 2 other fields - u_entity and u_department . While submitting it on the portal it works once.

 

Once the incident is raised, in the backed incident form -- variables section -- all the choices are being shown irrespective of the entity and department since its coming from sys_choice table.

 

I have created below script include function and on load client script to remove the choices from this field but its not removing the chocies.

 

 

Script include 

getIssueTypesByEntityandDepartment: function() {
        var entity = this.getParameter('sysparm_entity');
		var department = this.getParameter('sysparm_department');

        var arr = [];
        var res = [];

        var gr = new GlideRecord('table##');
        gr.addEncodedQuery('##');
        gr.orderBy('u_incident_sub_category');
        gr.query();
        while (gr.next()) {
            arr.push(gr.u_incident_sub_category.toString());
        }

        var ga = new GlideRecord('sys_choice');
        ga.addEncodedQuery('name=u_data_administration^element=u_incident_sub_category^inactive=false');
        ga.query();
        while (ga.next()) {
            if (arr.indexOf(ga.value.toString()) === -1) {
                if (res.indexOf(ga.value.toString()) === -1) {
                    res.push(ga.value.toString());
                }
            }
        }

        return res.toString();
    },

 

On Load Client Script 

function onLoad() {
    var ga = new GlideAjax('IncidentRecordProducerUtils');
    ga.addParam('sysparm_name', 'getIssueTypesByEntityandDepartment');
    ga.addParam('sysparm_entity', g_form.getValue('u_entity'));
	ga.addParam('sysparm_department', g_form.getValue('u_inc_department'));
    ga.getXMLAnswer(function getIssueTypes(answer) {
        var arr = answer.split(',');
        for (var i = 0; i < arr.length; i++) {
            g_form.removeOption('variables.rp_subcategory', arr[i]);
        }
    });
	g_form.removeOption('variables.ent_eng', 'Single');
}

 

I added logs and its correctly returning the choices which needs to be removed on the form. I tried adding timeout, doesn't help.

 

However, the same logic is working in Service Operations Workspace (SOW).  

Is it because the variable is displayed under a container in the form (Incident form variables - formatter)

 

Let me know if anyone has any solution.

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@ameybhaisar 

your onLoad catalog client script should be marked as "Applies on Target Record" as True

it should work fine for variables as OOTB the variables are shown always in Variable Formatter

share screenshots

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Yes, it's already marked as 'Applies on Target Record'

 

Below is the screenshot from the default view -- 

ameybhaisar_0-1758089878229.png

The variable label is 'Issue Type'  and backend name is rp_subcategory.

 

 

@ameybhaisar 

You said you created onLoad client script.

You should use catalog client script. Since it's catalog client script the variable syntax is this and not variables.variableName

I updated as this

-> your onLoad catalog client script should be able to access form field values

-> ensure "Applies on Target Record" is marked as TRUE

function onLoad() {
    var ga = new GlideAjax('IncidentRecordProducerUtils');
    ga.addParam('sysparm_name', 'getIssueTypesByEntityandDepartment');
    ga.addParam('sysparm_entity', g_form.getValue('u_entity'));
    ga.addParam('sysparm_department', g_form.getValue('u_inc_department'));
    ga.getXMLAnswer(function getIssueTypes(answer) {
        var arr = answer.split(',');
        for (var i = 0; i < arr.length; i++) {
            g_form.removeOption('rp_subcategory', arr[i]);
        }
    });
    g_form.removeOption('ent_eng', 'Single');
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader