scripting

Gampa Ashrith
Tera Contributor

Hi,

I want to populate the problem number(i want to get the sys_id) which in the incident form field,

 

which i have created it from the incident page and i want to do it with UI Actions button that to by using  GlideModalForm-API.


my main question here is how do we get the current sys_id of the problem record which we have update it from the incident table through GlideAjax-api.

 

i want the logic and script in the Script include.

 

 

 

 

1 REPLY 1

HrishabhKumar
Kilo Sage

Hi @Gampa Ashrith ,

To achieve this, you can follow these steps:

 

Create a UI Action on the Incident form.

Create a Script Include to handle the server-side logic.

Use GlideAjax to call the Script Include from the client-side script in the UI Action.

Display the result using GlideModalForm.

Here's how you can set this up:

 

Step 1: Create the Script Include

Create a Script Include that retrieves the Problem record's sys_id associated with a given Incident sys_id.

 

 

var IncidentProblemUtil = Class.create();
IncidentProblemUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getProblemSysId: function() {
        var incidentSysId = this.getParameter('sys_id');
        var gr = new GlideRecord('problem');
        gr.addQuery('incident.sys_id', incidentSysId);
        gr.query();
if (gr.next()) {
            return gr.getValue('sys_id');
        } else {
            return '';
        }
    }
});

 

 

Step 2: Create the UI Action

Create a UI Action on the Incident form that will call the Script Include via GlideAjax and display the result in a modal form.

 

Client-side script for the UI Action:

 

 

function onClick() {
    var ga = new GlideAjax('IncidentProblemUtil');
    ga.addParam('sys_id', g_form.getUniqueValue());
    ga.addParam('sysparm_name', 'getProblemSysId');
    ga.getXMLAnswer(function(response) {
        var problemSysId = response.responseXML.documentElement.getAttribute('answer');
        if (problemSysId) {
            // Use GlideModalForm to display the Problem record
            var gm = new GlideModalForm('Problem', problemSysId);
            gm.setTitle('Problem Details');
            gm.render();
        } else {
            alert('No related Problem record found.');
        }
    });
}
 
g_form.addActionButton('show_problem', 'Show Problem', onClick);

 

 

 

Explanation:

Script Include: The IncidentProblemUtil Script Include retrieves the sys_id of the Problem record associated with the given Incident sys_id. This is done by querying the problem table where the incident.sys_id matches the given Incident sys_id.

 

UI Action:

 

Client-side script: This script is executed when the UI Action button is clicked. It uses GlideAjax to call the getProblemSysId method in the IncidentProblemUtil Script Include.

The ga.getXMLAnswer method handles the server response. If a Problem record is found, it uses GlideModalForm to display the Problem record in a modal form. If no related Problem record is found, it shows an alert.

GlideModalForm: This is used to display the Problem record in a modal form. The render method displays the modal.

 

Ensure the Script Include is set to be accessible via GlideAjax by checking the "Client Callable" checkbox. Also, make sure you have appropriate permissions and roles to query the Problem and Incident tables.

 

Thanks,

Hope this helps.

If my response turns useful, please mark it helpful and accept solution.