The CreatorCon Call for Content is officially open! Get started here.

Query that returns no rows

shill
Mega Sage

I am trying to create a business rule that queries a second table for any entries (based on user).

If records found, I need to create a new record that second table with an incremental code.

If NO records are found for that user, I need to add a record.

What I can't seem to figure out is the second part.

I can get the query to run and add additional records associated to the user, but I cannot figure out how to run the secondary code to insert when no records returned from the query.

Below is one attempt. If the user is not in the second table, I can never get the to the else code (or log message).

I don't recall ever attempting a no records returned type query before, so not sure how to proceed.

userPh.query();

if(userPh.next()){

phCode.push(userPh.u_phone_code.toString());

createUserPhoneRecord();

}

else{

gs.log('got to the else');

createNewUserPhoneRecord();

}

1 ACCEPTED SOLUTION

Chris M3
Tera Guru

Whats your query lines before userPh.query()



add some logs, before the if statement


gs.log(userPh.getEncodedQuery());


gs.log(userPh.getRowCount());



Those may both provide clues.   My guess would be the query is not formatted correctly.


View solution in original post

19 REPLIES 19

manikorada
ServiceNow Employee
ServiceNow Employee

Whats the code in createNewUserPhoneRecord function?


function createNewUserPhoneRecord(){


// none found, so create a M1 record


userPh.initialize();


userPh.u_user = current.assigned_to;


userPh.u_phone_number = current.number;


userPh.u_phone_code = 'M1';


userPh.u_mobichord_record = current.sys_id;


userPh.insert();


}




Ultimately, it creates the initial user entry with the default of M1 as the phone code.


In the first part of the code if I find records, I grab the last code (like M3) and increment to use M4.


manikorada
ServiceNow Employee
ServiceNow Employee

Steve,


You are never initializing the userPh object.



I would do something like this :





  1. function createNewUserPhoneRecord(){  
  2. // none found, so create a M1 record
  3. var userPh1 = new GlideRecord(<<table_nmae>>); //
  4. userPh1.initialize();  
  5. userPh1.u_user = current.assigned_to;  
  6. userPh1.u_phone_number = current.number;  
  7. userPh1.u_phone_code = 'M1';  
  8. userPh1.u_mobichord_record = current.sys_id;  
  9. userPh1.insert();  
  10. }  

Wouldn't the object be initialized from the first query?


It's the same table the query comes from.


I also would have assumed that my log statement would be triggered regardless.