OnChange Client Script using GlideAjax

SNOW32
Giga Expert

Hi All,

I have a requirement, If Opened by user is part of an Assignment Group then a responded checkbox should be default checked. I thought to using OnChange() client script using GlideAjax . Can anyone help me how to do it?

 

 

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

As some have suggested, a Business Rule would be the best way to go, and because you're looking at the Opened by user, which is set on insert, an Advanced Before Insert Business Rule will do the trick.  No need for it to run on update:

 

find_real_file.png

 

Here's the "Condition" field contents:

gs.getUser().isMemberOf("MediaFirst SRO Team")

 

And the "Script" field contents (you may have to change the field name):

(function executeRule(current, previous /*null when async*/) {

	//set the Responded field to "true"
	current.u_responded = true;

})(current, previous);

 

The reason your After Business Rule was not working is because you did not actually update the record.  You do not need to, nor should you, include the update() function when using a Before Business Rule.  And because you want to trigger an SLA or not based on the Responded field value, you are better off setting it before the insert of the record.

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi

Suggest you put this in a Business Rule rather than a client script. 

You can then do an after/insert rule to check the logic and amend the record as required. 

Cheers
Mike

Hi Mike,

You're right! I have to use BR.. But its not working as expected.As "responded" check box is connected with Response SLA.So when "Opened by" user belongs to particular group then Responded check box to be checked(Response SLA will work when Responded check box will be checked.

find_real_file.png

find_real_file.png

I wrote After/Insert BR  but Responded check box is not getting active

var usr = gs.getUser().isMemberOf('MediaFirst SRO Team');
    var grp = new GlideRecord('incident');
    grp.addQuery('group.name', 'MediaFirst SRO Team');
    grp.addQuery('opened_by', usr);
    grp.addQuery();
    if(grp.next()){
        current.setValue('u_responded', 'true');  //Do something

    }

Gowrisankar Sat
Tera Guru

For onchange of assignment group:

1) You can use code from User Object Cheat Sheet:

https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

//Check to see if assigned to is a member of selected group
var grpName = 'YOURGROUPNAMEHERE';
var usrID = g_form.getValue("opened_by");
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group.name', grpName);
grp.addQuery('user', usrID);
grp.query(groupMemberCallback);
   
function groupMemberCallback(grp){
//If user is a member of selected group
    if(grp.next()){
        //Do something
        alert('Is a member');
    }
    else{
        alert('Is not a member');
    }
}

2) Write glideajax, check this one:

https://community.servicenow.com/community?id=community_question&sys_id=c3c8eeccdb227b40feb1a851ca96...

- If you want this only onload, use below approach:

Using Scratchpad variables in onDisplay Business rules along with Onload client script.

https://community.servicenow.com/community?id=community_question&sys_id=18e01ba9dbdcdbc01dcaf3231f96...

Hi Gowrisankar,

I have tried option given by you.. "Responded" checkbox is getting checked but the Response SLA is not activated as Response SLA is activated when Responded check box will checked.May be server in script include we need to add it.But i don't know how?

find_real_file.png

find_real_file.png

I wrote Client Script using GlideAjax but Responded SLA didn't activated as Responded check box is active.

Client script-

function onLoad() {
    var usrID = g_form.getValue("opened_by");
    var ga = new GlideAjax('Responded');
    ga.addParam('sysparm_name','checkIsMember');
    ga.addParam('sysparm_user', usrID);
    var ans = ga.getXMLWait();
    g_form.setValue('u_responded',ans);  
}

Script Include-

        checkIsMember: function(){
        var user1 = this.getParameter('sysparm_user');
        var a=gs.getUser().isMemberOf('4b4f1266dbe023c0ca1e2ded0b961989');
        return a;

    },