How should I do a comparison between two fields of a table and print the name of the ones that are different?

Betzaida Garci1
Kilo Contributor

Hi everyone!

I want to compare the field 'version' and 'latest_version' and if they dont match print the value of the name field of the 'sys_store_app' table, I have the following script but it just prints the whole table 

Can someone help me, please 🙂 

(function runMailScript(template, current, email, email_action, event) {
var tb = new GlideRecord ('sys_store_app');
tb.query();

try{
while (tb.next()){
tb.addQuery('version', 'latest_version');
tb.version == tb.latest_version;
template.print('Name1: ' + tb.name + '\n');
}
}catch(ex){
gs.info(ex);
}

})(template, current, email, email_action, event);

 

1 ACCEPTED SOLUTION

Tony Chatfield1
Kilo Patron

Hi, unfortunately your requirement\intention is not clear from your script.
You are running the script as a notification email script - you are populating into an email body?
Currently you query all records on sys_store_app and are querying the version filed for a string value 'latest_version'.

I am guessing you want to query 'sys_store_app' and return all records into the email body where the 'version' is not the same as 'latest_version' ?
Perhaps try something like this

(function runMailScript(template, current, email, email_action, event) {

    var tb = new GlideRecord('sys_store_app');
    tb.query();

    while (tb.next()) {
        // check each record as you loop through them looking for mismatches and print to the email body if you find one
        if (tb.version != tb.latest_version) {
            template.print('Name1: ' + tb.name + '\n');
        }
    }


})(template, current, email, email_action, event);

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, unfortunately your requirement\intention is not clear from your script.
You are running the script as a notification email script - you are populating into an email body?
Currently you query all records on sys_store_app and are querying the version filed for a string value 'latest_version'.

I am guessing you want to query 'sys_store_app' and return all records into the email body where the 'version' is not the same as 'latest_version' ?
Perhaps try something like this

(function runMailScript(template, current, email, email_action, event) {

    var tb = new GlideRecord('sys_store_app');
    tb.query();

    while (tb.next()) {
        // check each record as you loop through them looking for mismatches and print to the email body if you find one
        if (tb.version != tb.latest_version) {
            template.print('Name1: ' + tb.name + '\n');
        }
    }


})(template, current, email, email_action, event);

Thanks! It worked! 

In this case the code can be made a bit more optimized by selecting only those records that fulfill the if condition:

(function runMailScript (template, current, email, email_action, event) {
	var tb = new GlideRecord('sys_store_app');

	tb.addEncodedQuery('versionNSAMEASlatest_version');

	tb.query();

	while (tb.next()) {
		// No check needed, the version will be different from latest version
		// by defintion for all retrieved records
		template.print('Name1: ' + tb.name + '\n');
	}
})(template, current, email, email_action, event);

Madden Boden
Kilo Explorer

Thanks the support members for sharing the script. As I was also facing the issue in the comparison names at adope lightroom. But after reading their replies and the script that share worked for me. when I applied the script got the desired results for which I was looking.