Conditional coalesce script for sys_id creates record

SANJEEV4
Tera Contributor

I have a scenario on multiple coalesce conditions. So I created coalesce on the sys_id field(script)

var gr = new GlideRecord("table");
gr.addQuery("field1", source.fieldA);
gr.query();
if (gr.next()) {
return gr.sys_id; // I tried with - answer = gr.sys_id;
} else {
var gr1 = new GlideRecord("table");
gr1.addQuery("field1", source.fieldB);
gr1.query();
if (gr1.next()) {
return gr1.sys_id; // I tried with - answer = gr1.sys_id;
} else{
return null; //I tried with : return -1 (and) return GlideGuid.generate(null); (and) answer = -1
}
}

// Its not working

 If It does not match, should not create a new record. for that, I created onBefore with 

 

if (action == 'insert') {
ignore = true;
}

Still creating new records. 

 

Thanks in advance 

2 REPLIES 2

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello @SANJEEV4 ,

                                   I would recommend to debug your script first. Check Field Values: Make sure that source.fieldA and source.fieldB have the correct values that you're expecting. It's possible that the values of these fields are not what you're assuming. using gs.info. There might be chance field1 might be reference field and you are checking with string value. source.fieldA/B

gs.info("source.fieldA: " + source.fieldA);
gs.info("source.fieldB: " + source.fieldB);

Check Conditions: Ensure that the conditions inside your if statements are evaluating correctly. You might want to log the query conditions and the number of records returned to understand if they are as expected

gr.addQuery("field1", source.fieldA);
gr.query();
gs.info("Records found for fieldA: " + gr.getRowCount());

 

Kindly mark correct and helpful if applicable

@Chetan Mahajan If does not match any condition what should I return the value