Client script going in infinity loop

Mr_Blue
Tera Expert

Hello All,
This is a onchange client script written on " Assigned To " name change, Here if the user is not the member of the group then it must say you dont have permission. As of now its going into infinity loop, Can some people please suggest me what to do here so that i gets a correct msg.

Code :: 

var usersysid = 0;
var cntr = 0;
var userx = newValue;

var grpx = g_form.getValue('assignment_group');
var username = g_user.userName;

var target = new GlideRecord('sys_user');
target.addQuery('user_name', username);
target.query();
while (target.next()) {
usersysid = target.sys_id;

}

var testx = new GlideRecord('sys_user_grmember');
testx.addQuery('group', grpx);
testx.query();
while (testx.next()) {
if (testx.user == usersysid) {
cntr = cntr + 1;

}
}

var testy = new GlideRecord('sys_user_grmember');
testy.addQuery('group', 'a3deaeb56f4d710027be33d9ea3ee4f1');
testy.query();
while (testy.next()) {
if (testy.user == usersysid) {
cntr = cntr + 1;
}
}

if (cntr > 0.5) { }
if (cntr < 0.5) {
g_form.setValue('assigned_to', oldValue);
g_form.showErrorBox('assigned_to', 'you do not have permission to changed the assigned to field on this change request');
}
}
thank You 

1 ACCEPTED SOLUTION

You only need to write all the glideRecords whichever you have written in onchange client script just copy and paste into script include and write glideAjax call into onChange client script.

for your reference, I am giving you some links for how to write glideAjax

why it is not good to use GlideRecord in clientscript? |

Examples of asynchronous GlideAjax | 

Servicenow GlideAjax Script Include Example | Asynchronous .(Must watch this video for better unders...|

 

View solution in original post

4 REPLIES 4

Azim Kazi
Giga Guru

Hello Blue,

First of all, this is not the best practice to write GlideRecord in any client script

Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The top ways to get information from the server are g_scratchpad, and asynchronous GlideAjax .

I suggest you to, write a Script include and by using glideAjax call serverside values on clientscript then set as per your requirements.

 

if my response helps you then kindly mark my answer helpful and correct otherwise if any query feels free to ask further.

Regards,

Ajim.

You only need to write all the glideRecords whichever you have written in onchange client script just copy and paste into script include and write glideAjax call into onChange client script.

for your reference, I am giving you some links for how to write glideAjax

why it is not good to use GlideRecord in clientscript? |

Examples of asynchronous GlideAjax | 

Servicenow GlideAjax Script Include Example | Asynchronous .(Must watch this video for better unders...|

 

Indrajit
Mega Guru

Hey Mr.Blue,

Do not use GlideRecord in client script that is the one reason your script is going in loop.Client scripting(which runs on browser) uses either data available on the form or data retrieved from the database.

The best ways to get data into form/browser from the database are g_scratchpad, and asynchronous GlideAjax lookup.

refer this link for using GlideAjax in client script

https://community.servicenow.com/community?id=community_blog&sys_id=ba9d6e69dbd0dbc01dcaf3231f9619d2

mark correct and helpful if it useful content.

Regards,

Indrajit.

Arvind Singh Ra
Kilo Guru

Hi Mr. Blue,

 

I would suggest you create a display Business rule with script as. 

find_real_file.png

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

var gp = current.assignment_group;
g_scratchpad.grp = gs.getUser.gs.getUserID.isMemberOf('assignment_group');

})(current, previous);

 

then Create a Onchange Client script:

find_real_file.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var scratch = g_scratchpad.grp;
if(scratch!=true){
g_form.addErrorMessage("YOUR ARE THE MEMBER");
return false;
}

//Type appropriate comment here, and begin script below

}

 

after testing this code:

 

find_real_file.png

 

Mark Correct and 👍 Helpful based on the impact.

Warm Regards,

Arvind