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

So, I added an additional BR for testing with


// check if we have a record of this users phone number


var userPh = new GlideRecord('u_user_phone');


userPh.addQuery('u_user', current.assigned_to);


userPh.orderByDesc('u_phone_code');


userPh.query();


gs.log(userPh.getRowCount());


gs.log(userPh.getEncodedQuery());


if(userPh.getRowCount() != 0) {


gs.log('records found');


} else {


gs.log('no records');



When I test with someone that I know does not have a record in u_user_phone, I get:


  • 0
  • u_user=03e9a60f0f0322404b451d2be1050e4f^ORDERBYDESCu_phone_code
  • no records

When testing with a known user that has records, I get:


  • 5
  • u_user=53c8c5ef0a0a3c1c010c291fc7123535^ORDERBYDESCu_phone_code
  • records found

So wait, that appears to have worked right?   the log entries were all as expected?


Code looks like it is working. Are you still having the issue if you add back your original functions


vrfox
Giga Contributor

code works as expected


shill
Mega Sage

OK, not sure what I did in my first BR (I created this stripped down version for testing).


Thanks to all who helped. I sometimes get lost in the logic and I may have just missed something originally.