
- 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:49 PM
Did you try my suggestion Steve?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 11:54 AM
If I understand correctly is never getting to the false statement, this should make it.
if(userPh.getRowCount() != 0) {
} else {
createNewUserPhoneRecord();
}
Try that code

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:11 PM
You understood correctly, but using your suggestion did not work on either side (if records found or no records found).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:14 PM
can i see the code before the userPH.query()
also... the console.log(
console.log(userPh.getRowCount())
in the case there is no record

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2017 01:26 PM
var userPh = new GlideRecord('u_user_phone');
userPh.addQuery('u_user', current.assigned_to);
userPh.orderByDesc('u_phone_code');
userPh.query();
if(userPh.getRowCount() != 0) {
createUserPhoneRecord();
} else {
createNewUserPhoneRecord();
}
Ultimately, any insert into this table that the business rule runs on needs to be inserted into the u_user_phone table.
I just need to know what code to use. If no records found, phone code would be M1.
If one or more records found, my function determines the phone code based on the highest current code and adds one (this works currently).