Script Trouble- Not Returning Expected Result

jlaps
Kilo Sage

I have an IF activity in a catalog workflow that is not giving expected results.

answer = ifScript();
//
function ifScript() {
	workflow.scratchpad.count = workflow.scratchpad.count + 1;
    var flag = 0;
    gr = new GlideRecord("u_database_termination_data_details");
    gr.addEncodedQuery("u_ritm=" + current.number);
    gr.query();
    if (!gr.next()) {
		return 'no';
		}
	else if (gr.next()) {
		return 'yes';
	}
}

This is the table in question-

jlaps_1-1755095155578.png

I did edit this record so the RITM# would match. What am I missing that the above IF is returning No when the query done manually works?

 

1 ACCEPTED SOLUTION

Viraj Hudlikar
Tera Sage

Hello @jlaps 

Your script has a logical error where gr.next() is called twice. The correct way to check for records is to call gr.next() once within the if condition.


answer = ifScript();
//
function ifScript() {
	workflow.scratchpad.count = workflow.scratchpad.count + 1;
    var flag = 0;
    gr = new GlideRecord("u_database_termination_data_details");
    gr.addEncodedQuery("u_ritm=" + current.number);
    gr.query();
    if (!gr.next()) {
		return 'no';
		}
	else  {
		return 'yes';
	}
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

View solution in original post

2 REPLIES 2

Viraj Hudlikar
Tera Sage

Hello @jlaps 

Your script has a logical error where gr.next() is called twice. The correct way to check for records is to call gr.next() once within the if condition.


answer = ifScript();
//
function ifScript() {
	workflow.scratchpad.count = workflow.scratchpad.count + 1;
    var flag = 0;
    gr = new GlideRecord("u_database_termination_data_details");
    gr.addEncodedQuery("u_ritm=" + current.number);
    gr.query();
    if (!gr.next()) {
		return 'no';
		}
	else  {
		return 'yes';
	}
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ankur Bawiskar
Tera Patron
Tera Patron

@jlaps 

Since you just want to check if record is present or not better to use hasNext() method.

try this

-> if record not found then output is no

answer = ifScript();

function ifScript() {
    workflow.scratchpad.count = workflow.scratchpad.count + 1;
    var flag = 0;
    var gr = new GlideRecord("u_database_termination_data_details");
    gr.addEncodedQuery("u_ritm=" + current.number);
    gr.query();
    if (!gr.hasNext()) {
        return 'no';
    } else {
        return 'yes';
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader