g_modal in UI Action workspace client script query

yashu1995
Kilo Guru

Hi All,

 

Have a query on g_modal workspace client script in UI action.

on Agent workspace once click on - "Close complete" button the below Pop up window shows up

Query: Is it possible to show fields based on another field selection. Example below: "Select Correct HR Service" Question should show up only when - "Is the ticket submitted to correct HR Service" - is selected - "No"

 

Kindly help in providing suggestions if condition based field visibility can be achieved in Workspace Client Script in UI action while using g_modal

yyasaswini_0-1693294142100.png

 

UI Action Workspace Client Script:

UI Action Workspace Client Script :    
 var gA = new GlideAjax("sn_hr_core.hr_CaseAgentWorkspace");
    gA.addParam('sysparm_element1', 'u_reason_the_case_is_created_assigned');
    gA.addParam('sysparm_element2', 'u_is_the_ticket_submitted_with_correct_hr_service');
    gA.addParam('sysparm_name', 'getChoices');
    gA.getXML(getChoicesRes);

    function getChoicesRes(response) {
        choicesObj = {};
        var answer = response.responseXML.documentElement.getAttribute("answer");
        choicesObj = JSON.parse(answer);

        closeCodeChoices = [];
        isSubmittedCorrectlyChoices = [];
        for (var key in choicesObj.u_reason_the_case_is_created_assigned) {

            if (choicesObj.u_reason_the_case_is_created_assigned.hasOwnProperty(key)) {

                var closeCodeSet = {
                    displayValue: choicesObj.u_reason_the_case_is_created_assigned[key].displayValue,
                    value: choicesObj.u_reason_the_case_is_created_assigned[key].value
                };
                closeCodeChoices.push(closeCodeSet);
                //alert(closeCodeChoices);

            }

        }

        for (var key in choicesObj.u_is_the_ticket_submitted_with_correct_hr_service) {
            if (choicesObj.u_is_the_ticket_submitted_with_correct_hr_service.hasOwnProperty(key)) {
                var isSubmittedCorrectlySet = {
                    displayValue: choicesObj.u_is_the_ticket_submitted_with_correct_hr_service[key].displayValue,
                    value: choicesObj.u_is_the_ticket_submitted_with_correct_hr_service[key].value
                };
                isSubmittedCorrectlyChoices.push(isSubmittedCorrectlySet);
            }
        }


        var messages = [
            'Work notes', 'Close Complete',
            'Provide a reason for closing the case as Complete.'
        ];

        getMessages(messages, function() {
            var fields = [{
                    type: 'textarea',
                    name: 'work_notes',
                    label: getMessage('Additional Comments (Customer Visible)')
                },
                {
                    type: 'choice',
                    name: 'u_reason_the_case_is_created_assigned',
                    label: getMessage('Reason the case is created / assigned?'),
                    // value: getMessage(' -- Select -- '),
                    value: g_form.getValue('u_reason_the_case_is_created_assigned'),
                    displayValue: g_form.getDisplayValue('u_reason_the_case_is_created_assigned'),
                    choices: closeCodeChoices,
                    mandatory: true
                },
                {
                    type: 'choice',
                    name: 'u_is_the_ticket_submitted_with_correct_hr_service',
                    label: getMessage('Is the ticket submitted with correct HR Service?'),
                    // value: getMessage(' -- Select -- '),
                    value: g_form.getValue('u_is_the_ticket_submitted_with_correct_hr_service'),
                    displayValue: g_form.getDisplayValue('u_is_the_ticket_submitted_with_correct_hr_service'),
                    choices: isSubmittedCorrectlyChoices,
                    mandatory: true
                },
                {

                    type: 'reference',
                    name: 'u_select_correct_hr_service',
                    label: getMessage('Select Correct HR Service'),
                    mandatory: true,
                    reference: 'sn_hr_core_service',
                    referringTable: 'sn_hr_core_case',
                    referringRecordId: g_form.getUniqueValue(),
                    value: g_form.getValue('u_select_correct_hr_service'),
                    displayValue: g_form.getDisplayValue('u_select_correct_hr_service')

                },
            ];

            var sysId = g_form.getUniqueValue();
            var tblName = g_form.getTableName();

            g_modal.showFields({
                title: getMessage('Close Complete'),
                fields: fields,
                instruction: getMessage('Provide a reason for closing the case as Complete.'),
                size: 'xl'
            }).then(function(fieldValues) {
                //get the work note entered
                var newWorkNote = fieldValues.updatedFields["0"].stagedValue;
                g_form.setValue('u_reason_the_case_is_created_assigned', fieldValues.updatedFields[1].value);
                g_form.setValue('u_is_the_ticket_submitted_with_correct_hr_service', fieldValues.updatedFields[2].value);

 

Script Include:
getChoices: function() {
        // var elementsparm = String(this.getParameter('sysparm_elements'));
        //var elements = elementsparm.split(",");
		var choicesObj = {};		
        var element1 = this.getParameter('sysparm_element1');
        var element2 = this.getParameter('sysparm_element2');
		
		if(element2 == 'u_is_the_ticket_submitted_with_correct_hr_service'){
			var name = 'u_is_the_ticket_submitted_with_correct_hr_service';
			choicesObj[name] = {};
			var count = 0;
			var gr = new GlideRecord('sys_choice');
			gr.addEncodedQuery('name=sn_hr_core_case^inactive=false'+'^element='+name);
			gr.query();
			while(gr.next()){
				//gs.info("inside while gr");
				choicesObj[name][count] = {

                    displayValue: gr.label.toString(),
                    value: gr.value.toString()

                }
                count++;
			}
		}

        if (element1 == 'u_reason_the_case_is_created_assigned') {		

			var name = 'u_reason_the_case_is_created_assigned';
			choicesObj[name] = {};
			var count = 0;
			var flag = false;
			var userId = gs.getUserID();
			var grGrpArray = [];
            var gruser = new GlideRecord('sys_user');
            gruser.addQuery('sys_id', userId);
            gruser.query();
            if (gruser.next()) {
                var empCountry = gruser.u_employment_country;
            }
			
			// Get User Groups Array
        var grMem = new GlideRecord('sys_user_grmember');
        grMem.addEncodedQuery("group.typeLIKEc0b59b0e470915d09092e7e8036d43f3^ORgroup.typeLIKEb655530e470915d09092e7e8036d43c2^ORgroup.typeLIKE7cf902f987f0fd50fdb7400e8bbb351a^group.active=true");
        grMem.addQuery('user', userId);
        grMem.query();
        while (grMem.next()) {
            grGrpArray.push(grMem.group);
        }

        if (grGrpArray.length > 0)
            flag = true;
			
			if (flag) {
            var grCustom = new GlideRecord('sn_hr_core_hr_custom_values');
            grCustom.addQuery('u_type', 'group');
            grCustom.addEncodedQuery('u_groupsLIKE' + grGrpArray.toString());
            grCustom.query();
            while (grCustom.next()) {
				choicesObj[name][count] = {

                    displayValue: grCustom.u_se_hr_custom_values_value.toString(),
                    value: grCustom.u_se_hr_custom_values_value.toString()

                }
                count++;
			}
			}else{
				var grc = new GlideRecord('sn_hr_core_hr_custom_values');
            grc.addQuery('u_se_hr_custom_values_type', 'HR Case Closure Choices');
            grc.addEncodedQuery('u_countryLIKE' + empCountry);
			//grc.orderByDesc('u_reason_the_case_is_created_assigned');
			grc.orderBy('u_se_hr_custom_values_value');
            grc.query();
            while (grc.next()) {
                choicesObj[name][count] = {

                    displayValue: grc.u_se_hr_custom_values_value.toString(),
                    value: grc.u_se_hr_custom_values_value.toString()

                }
                count++;
            }
			}
        }

    var result = JSON.stringify(choicesObj);
		gs.info("result : "+result);
    return result;

},

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@yashu1995 

I don't think you can show/hide based on some condition

But here is a workaround to use 2 modals

Hide/Display fields in the modal(g_modal.showFields) for Suspend ( HR case) and Suspend case( HR tas... 

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@yashu1995 

I don't think you can show/hide based on some condition

But here is a workaround to use 2 modals

Hide/Display fields in the modal(g_modal.showFields) for Suspend ( HR case) and Suspend case( HR tas... 

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

Thanks a lot for the solution Ankur. Helped for my query.

Is it possible to just use 1 modal?