- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:39 AM
Hello,
I have a requirement to check if all the values on the list are the same for all records, if all values are the same on all records on the list, then do some update. If even one record contains a different value, then stop the script.
I found a similar case here but this just checks if the vales are the same, how can I check if the values are not the same: https://www.servicenow.com/community/developer-forum/compare-one-field-value-in-a-list-with-the-fiel...
I want to check that if I found a value that is not the same with the previous value then stop the script
var state = '';
var inc = new GlideRecord('incident');
inc.orderByDesc('number');
inc.setLimit(5);
inc.query();
while (inc.next()){
if (state== '') {
state = inc.state;
} else if (state== inc.state) {
//returns records that are the same
} else {
state = inc.state
}
}
Appreciate all the help. Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:52 AM - edited 06-11-2024 12:58 AM
Hi @Evren Yamin ,
To achieve your requirement of stopping the script if even one record contains a different value, you can add a flag variable to track whether there is a difference in values. Here's how you can modify your script:
var state = '';
var sameValues = true; // Flag variable to track if all values are the same
var inc = new GlideRecord('incident');
inc.orderByDesc('number');
inc.setLimit(5);
inc.query();
while (inc.next() && sameValues){
if (state == '') {
state = inc.state;
} else if (state == inc.state) {
// Returns records that are the same
} else {
sameValues = false; // Set flag to false if a different value is encountered
}
}
if (sameValues) {
// All records have the same value, perform update
} else {
// Stop the script or perform any other actions needed
}
Explanation:
Variables Initialization:
- state: This variable is used to keep track of the 'state' field value of the previous record.
- sameValues: This flag variable is initialized as true to indicate that initially, all values are assumed to be the same.
GlideRecord Query Setup:
- inc: A GlideRecord object is created for the 'incident' table.
- orderByDesc('number'): Records are ordered by the 'number' field in descending order.
- setLimit(5): The query is limited to retrieve only 5 records.
Record Processing Loop:
- The while loop iterates through each record fetched by the query.
- Inside the loop, it checks if the current 'state' value is the same as the previous one. If yes, it continues the loop. If not, it sets the sameValues flag to false and breaks out of the loop.
Check sameValues Flag:
- After the loop, the script checks the value of the sameValues flag.
- If sameValues is still true, it means all records have the same value, and you can perform the update or any other necessary actions.
- If sameValues is false, it means even one record contains a different value, so you can stop the script or perform other actions.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 12:52 AM - edited 06-11-2024 12:58 AM
Hi @Evren Yamin ,
To achieve your requirement of stopping the script if even one record contains a different value, you can add a flag variable to track whether there is a difference in values. Here's how you can modify your script:
var state = '';
var sameValues = true; // Flag variable to track if all values are the same
var inc = new GlideRecord('incident');
inc.orderByDesc('number');
inc.setLimit(5);
inc.query();
while (inc.next() && sameValues){
if (state == '') {
state = inc.state;
} else if (state == inc.state) {
// Returns records that are the same
} else {
sameValues = false; // Set flag to false if a different value is encountered
}
}
if (sameValues) {
// All records have the same value, perform update
} else {
// Stop the script or perform any other actions needed
}
Explanation:
Variables Initialization:
- state: This variable is used to keep track of the 'state' field value of the previous record.
- sameValues: This flag variable is initialized as true to indicate that initially, all values are assumed to be the same.
GlideRecord Query Setup:
- inc: A GlideRecord object is created for the 'incident' table.
- orderByDesc('number'): Records are ordered by the 'number' field in descending order.
- setLimit(5): The query is limited to retrieve only 5 records.
Record Processing Loop:
- The while loop iterates through each record fetched by the query.
- Inside the loop, it checks if the current 'state' value is the same as the previous one. If yes, it continues the loop. If not, it sets the sameValues flag to false and breaks out of the loop.
Check sameValues Flag:
- After the loop, the script checks the value of the sameValues flag.
- If sameValues is still true, it means all records have the same value, and you can perform the update or any other necessary actions.
- If sameValues is false, it means even one record contains a different value, so you can stop the script or perform other actions.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 05:22 PM
Hello @Astik Thombare, thanks so much for your response, apologies for replying late. Your solution really helped. Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 01:09 AM
@Evren Yamin Please try this and see if it works.
var state = '';
var count = 0;
var tempCount = 0;
var inc = new GlideRecord('incident');
inc.orderByDesc('number');
inc.setLimit(5);
inc.query();
count = inc.getRowCount();
while (inc.next()){
if (state== '') {
tempCount = tempCount+1;
state = inc.state;
} else if (state== inc.state) {
//returns records that are the same
tempCount = tempCount+1;
} else if(state!=inc.state()) {
break; //Break the look iteration
}
}
if(count==tempCount){
//All records have same state do some update here
}
else{
//records have different states
}
Hope this helps.