Service Operation Workspace - g_modal pop up for showing CI's related to Caller field

dharmesh rathod
Tera Contributor
Hello All,
 
I have to open a popup modal in Service operation workspace where on change of Caller a g_modal will open up and it will show CI assigned to that user and in choice and after selection of the CI from the  list it should get mapped in the cmdb_ci field of the incident form.
 
I have achieved 98% of the solution and just 2% is not working which is on selection if the CI it is not getting mapped in the cmdb_ci field.
 
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var callerId = g_form.getValue('caller_id');
alert("1111");
    // Make a GlideAjax call to fetch CI choices based on the caller ID
    var ga = new GlideAjax('GetCIsForUser');
    ga.addParam('sysparm_name', 'getCIs');
    ga.addParam('caller_id', callerId);
    ga.getXMLAnswer(function(response) {
                
                   try {
            alert('Response received:', response);
 
            var ciList = JSON.parse(response);
            alert('Parsed CI list:', ciList);
 
            // Clear existing options in CI field
            g_form.clearOptions('cmdb_ci');
 
            // Populate CI field with fetched CIs
            if (ciList.length > 1) {
                // Open a modal dialog with type 'choice' and show options
                var fields = [{
                    type: 'choice',
                    name: 'cmdb_ci',
                    label: 'Select Configuration Item',
                    choices: ciList.map(function(ci) {
                        return {
                            value: ci.sys_id,
                            displayValue: ci.name
                        };
                    }),
                    mandatory: true
                }];
 
                alert('Opening modal dialog.');
 
                g_modal.showFields({
                    title: 'Select Configuration Item',
                    fields: fields,
                    buttons: [{
                        name: 'ok',
                        label: 'OK',
                        primary: true,
                        onClick: function() {
alert("Insice on click");
                            var selectedCI = g_modal.getValue('cmdb_ci');
                            if (selectedCI) {
                                g_form.setValue('cmdb_ci', selectedCI);
                            }
                            g_modal.hide();
                        }
                    }, {
                        name: 'cancel',
                        label: 'Cancel',
                        onClick: function() {
                            g_modal.hide();
                        }
                    }]
                });
            } else {
                alert('No Configuration Items found for the selected user.');
                alert('No Configuration Items found for the selected user.');
            }
        } catch (e) {
            alert('Error:', e);
            alert('An error occurred. Please check browser console for details.');
        }
    });
 
   //Type appropriate comment here, and begin script below
   
}
 
 
Screenshot :
dharmeshrathod_0-1708625950023.png

 

2 REPLIES 2

dharmesh rathod
Tera Contributor

I have found the solution for the above

 

Please find my solution below and mark it as helpful.

 

Script include: 

 

var GetCIsForUser = Class.create();
GetCIsForUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCIs: function() {
        var callerId = this.getParameter('caller_id');
        var ciList = [];

        // Query CIs assigned to the caller
        var grCI = new GlideRecord('cmdb_ci');
        grCI.addQuery('assigned_to', callerId);
        grCI.query();

        // Build array of CI sys_ids and names
        while (grCI.next()) {
            ciList.push({
                sys_id: grCI.getValue('sys_id'),
                name: grCI.getValue('name')
            });
        }

        return JSON.stringify(ciList);
    },


    type: 'GetCIsForUser'
});
 
 
Client script:
 
 
if (isLoading || newValue === '') {
        return;
    }
    var callerId = g_form.getValue('caller_id');
    // Make a GlideAjax call to fetch CI choices based on the caller ID
    var ga = new GlideAjax('GetCIsForUser');
    ga.addParam('sysparm_name', 'getCIs');
    ga.addParam('caller_id', callerId);
    ga.getXMLAnswer(function(response) {

        var ciList = JSON.parse(response);

        if (g_form.isNewRecord()) {
            // Clear existing options in CI field
            g_form.clearOptions('cmdb_ci');

            // Populate CI field with fetched CIs
            if (ciList.length > 1) {
                // Open a modal dialog with type 'choice' and show options
                var fields = [{
                    type: 'choice',
                    name: 'cmdb_ci',
                    label: 'Select Configuration Item',
                    choices: ciList.map(function(ci) {
                        return {
                            value: ci.sys_id,
                            displayValue: ci.name
                        };
                    }),
                    mandatory: true
                }];

                g_modal.showFields({
                    title: 'Select Configuration Item',
                    fields: fields,
                }).then(function(fieldValues) {

                    g_form.setValue('cmdb_ci', fieldValues.updatedFields[0].value);

                });
            }

        }


    });

Sai Avinash Ket
Tera Contributor

Thanks for explaining in detail on g_modal pop-up. I was able to replicate the same in SOW.