Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to populate the Caller active status based on the Caller ID field in the Incident form

Happy S
Tera Expert

Hi,

 

As per subject I am trying to populate the Caller active status based on the Caller ID field in the Incident form.

 

I have created a field Caller Status on the Incident form and need the logic to have it auto populate its state based on the Caller ID field.

 

Please help.

1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @Happy S 

 

You can use an On-Change Client Script. Refer below screenshots and the code:

AmitVerma_0-1736917759992.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var userRef = g_form.getReference('caller_id', userLookup);
    function userLookup(userRef) {
        alert(userRef.active.toString());
//You can remove this alert and make use of g_form API to set value of your custom field
g_form.setValue('your_field_name',userRef.active.toString());
    }
}

 

Output:

AmitVerma_1-1736917897511.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

Ok it works..

 

I added the below code to have it work on both OnLoad and OnChange in the same script.

 

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

 

Community Alums
Not applicable

Hi, @Happy S 

As per best practice and better performance will used glideAjax api,

we can achive that with help of following scap.

Client Script:

var c = g_form.getValue('caller_id');
    var ga = new GlideAjax('VerifyUser');
    ga.addParam('sysparm_name', 'getUserStatus');
    ga.addParam('sysparm_caller', c);
    ga.getXML(getStatus); // Wait synchronously for the server response

    function getStatus(response) {
        var ans = response.responseXML.documentElement.getAttribute("answer"); // Extract the response
        alert(ans);
    }
 
Script include
getUserStatus: function() {
        var c = this.getParameter('sysparm_caller');
        var gr = new GlideRecord("sys_user");
        if(gr.get(c)){
            return gr.active;
        }
    },