How to Auto populate a field which depends on other field

spimple
Tera Contributor

Hello Devs

I am working on a requirement in which there is a section in User form Named Incident Insights which has a dropdown named as Sentiments:

 

spimple_0-1690870492358.png

 

When we choose Unhappy as sentiment Notes field appears:

spimple_1-1690870649680.png

 

The requirement is When we select Unhappy as sentiment .. whatever the User record we r on it should search for the CLOSED incidents for that particular user and if that user has Closed incidents then the NOTES field should auto populate with : "This user has closed incidents" as soon as we select unhappy. If that user does not have any closed incidents then it should auto populate with : "No closed incidents"

So we are dealing with Users and Incident table.

Please suggest how do i solve this one

1 ACCEPTED SOLUTION

When you use GlideAjax from a Client Script, you need to declare the parameter that is passed in from the client, so you can get rid of userSysId as a function argument and add a line as the first line in the function:

var userSysId = this.getParameter('sysparm_userName');

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

If you already have an onChange Client Script to make the Notes field visible and mandatory, you can add to this, or if it's a UI Policy doing that, then create an onChange Client Script when Sentiments changes.  Your Client Script will use GlideAjax to call a Script Include, passing in the User value from this record.  The SI will do a GlideRecord query on the incident table where caller = the user value passed in from the Client Script, returning something like 'true' or 'false'.  The Client Script takes the returned value and populates the field with the message based on the value.

 

Here's an excellent guide on using GlideAjax.  It includes the syntax for returning more than one value from the SI, which is more than you need in this case, but it still works returning only one value (a static true or false or whatever based on the query results).  Give this a shot, and let me know if you get stuck with your actual use case.

 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Hey Brad, I am still stuck actually what's happening is I have created a Client Script in which I am calling a Script Include and after debugging I found that the control is not at all going inside the script include.
Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var userid = g_form.getUniqueValue()
    var ga = new GlideAjax('GetClosedStatus');
    ga.addParam('sysparm_name', 'checkClosedIncidents');
    ga.addParam('sysparm_userName', userid);

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var count = parseInt(answer);

		alert('count is :'+count);
    });


}

 

Script Include:

var GetClosedStatus = Class.create();
GetClosedStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkClosedIncidents: function(userSysId) {
        var closedIncidents = new GlideRecord('incident');
        closedIncidents.addQuery('caller_id', userSysId);
        closedIncidents.addQuery('state', '7');
        closedIncidents.query();

        var count = 0;
        while (closedIncidents.next()) {
            count++;
        }



        return count;
    },
    type: 'GetClosedStatus'
});

 

Can you please let me know the possible reason of control not going inside the Script include. bcz i have tested the script include code in Background scripts its working

When you use GlideAjax from a Client Script, you need to declare the parameter that is passed in from the client, so you can get rid of userSysId as a function argument and add a line as the first line in the function:

var userSysId = this.getParameter('sysparm_userName');