
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 11:47 AM
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();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:32 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:52 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:59 PM
So wait, that appears to have worked right? the log entries were all as expected?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 02:06 PM
Code looks like it is working. Are you still having the issue if you add back your original functions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 02:13 PM
code works as expected

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 02:53 PM
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.