- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 03:17 AM
Good day all,
I'm currently struggeling with a while cycle, which we need to run trough whole table.
Basically I'm creating a business rule on table 1, and I need it to update table 2.
The tables are connected trough account name.
We managed to fixed almost every other element of the script, but I'm stuck with empty array, because we have it in
while(current._next()){
var test = current.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(current.getDisplayValue('account').toString());
}
It worked in background script but that was because it wasn't set as "current" but I had a glide record created.
I was told that I should not create a glide record for a table I'm already working in.
So my question would be, is there some workaround for this?
The scenario is that there are 19 records assigned to an account name and 2 of them get filtered by this:
var userAssigned = current.getDisplayValue('account').toString();
current.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');
We need to go trough the filtered list of records in current table.
Thank You in advance for Your responds.
Michal Keluc
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 04:19 AM
Tried to rectify errors that I saw in your script, try using this :
(function executeRule(current, previous /*null when async*/) {
var userAssigned = current.getDisplayValue('account').toString();
var cnt= new GlideRecord('ast_contract');
cnt.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');
cnt.query();
gs.info('MK_'+ userAssigned);
var arr=[];
while(cnt.next()){
var test = cnt.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(cnt.getDisplayValue('account').toString());
}
gs.info('MK_' + arr.length);
gs.info('MK_' + arr[0]);
for(var i = 0; i < arr.length; i++){
var gr= new GlideRecord('customer_account');
gr.addEncodedQuery('name='+arr[i]);
gr.query();
while(gr.next()){
gr.setValue('u_sims_connected',true);
gr.update();
}
}
})(current, previous);
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:27 AM
Hi @MichalKeluc ,
If you are to pass the sys_id then don't call for the display value. In your script you are using display value. Using below code you can change it to sys_id (here I am assuming account is a reference field):
var userAssigned = current.getValue('account');
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 04:09 AM
Hello Kamlesh,
So this is the full code:
(function executeRule(current, previous /*null when async*/) {
var cnt= new GlideRecord('ast_contract');
var userAssigned = cnt.getDisplayValue('account').toString();
cnt.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');
cnt.query();
gs.info('MK_'+ userAssigned);
var arr=[];
while(cnt.next()){
var test = cnt.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(current.getDisplayValue('account').toString());
}
gs.info('MK_' + arr.length);
gs.info('MK_' + arr[0]);
for(var i = 0; i < arr.length; i++){
var gr= new GlideRecord('customer_account');
gr.addEncodedQuery('name='+arr[i]);
gr.query();
if(arr.length > 0){
//gs.info('true');
while(gr.next()){
gr.setValue('u_sims_connected',true);
gr.update();
}
}else{
gr.setValue('u_sims_connected',false);
gr.update();
//gs.info('false');
}
}
})(current, previous);
Everywhere there is a "cnt" was a "current" before, since I was told I should not use a glide record for a table in which I'm already working.
Also the "_next" was just to test a theory I found in a different forum.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 04:19 AM
Tried to rectify errors that I saw in your script, try using this :
(function executeRule(current, previous /*null when async*/) {
var userAssigned = current.getDisplayValue('account').toString();
var cnt= new GlideRecord('ast_contract');
cnt.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');
cnt.query();
gs.info('MK_'+ userAssigned);
var arr=[];
while(cnt.next()){
var test = cnt.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(cnt.getDisplayValue('account').toString());
}
gs.info('MK_' + arr.length);
gs.info('MK_' + arr[0]);
for(var i = 0; i < arr.length; i++){
var gr= new GlideRecord('customer_account');
gr.addEncodedQuery('name='+arr[i]);
gr.query();
while(gr.next()){
gr.setValue('u_sims_connected',true);
gr.update();
}
}
})(current, previous);
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:13 AM
I tried moving the
var userAssigned = current.getDisplayValue('account').toString();
As You did to the first lane and I tought it solved the issue, but actually the problem is still there. I just tried my code I had in backend script which actually works.
That is the most confusing part for me. There is a difference in what data I use, because I need to pass sys_id to have specific case to work with, but once I move it to the business rule script it doesn't work. The
while(cnt.next()){ var test = cnt.getDisplayValue('account').toString(); gs.info('MK3_' + test); arr.push(cnt.getDisplayValue('account').toString()); }
Just doesn't proccess.
This is the script from backend script (without filled sys ID):
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:27 AM
Hi @MichalKeluc ,
If you are to pass the sys_id then don't call for the display value. In your script you are using display value. Using below code you can change it to sys_id (here I am assuming account is a reference field):
var userAssigned = current.getValue('account');
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2022 05:33 AM
Thank You so much!
That actually worked.