Flow Scripting Question

Cirrus
Kilo Sage

Scripting help required please.

When a user subscribes to a service, and the user is later archived without first removing the subscription, Servicenow retains the subscription record in the service_subscribe_sys_user table. However, the email field is empty. This is causing our flows to error as Email validation failed: Email has no recipients, and only a percentage of the required emails are generated in the flow execution.

The below script queries the subscribe table and generates a unique list of email addresses. Is there a way to amend it to ignore any records where the email is blank?

 

var grSU = new GlideRecord('service_subscribe_sys_user');
grSU.addEncodedQuery('service_offering=40c3aeb0974f2550063c7e371153affb^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19^ORservice_offering=e49231771b22b190062afddf1d4bcb39');
grSU.query();
var obj={};

while(grSU.next()) {
obj[grSU.sys_user.email] = true;
}

return Object.keys(obj).toString();
 
 
1 ACCEPTED SOLUTION

Udayrathore049
Tera Contributor

Add a Script Check to Skip Blank Emails (Recommended)
Amend your script to check that the email is not empty or null before adding it to the object:
var grSU = new GlideRecord('service_subscribe_sys_user');
grSU.addEncodedQuery('service_offering=40c3aeb0974f2550063c7e371153affb^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19^ORservice_offering=e49231771b22b190062afddf1d4bcb39');
grSU.query();
var obj = {};

while (grSU.next()) {
var email = grSU.sys_user.email + '';
if (email) {
obj[email] = true;
}
}

return Object.keys(obj).toString();


This adds a basic validation to skip blank or null emails, which directly solves your flow error issue.



If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Uday Rathore
ServiceNow Developer
LinkedIn: https://www.linkedin.com/in/uday-rathore049/

View solution in original post

3 REPLIES 3

Udayrathore049
Tera Contributor

Add a Script Check to Skip Blank Emails (Recommended)
Amend your script to check that the email is not empty or null before adding it to the object:
var grSU = new GlideRecord('service_subscribe_sys_user');
grSU.addEncodedQuery('service_offering=40c3aeb0974f2550063c7e371153affb^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19^ORservice_offering=e49231771b22b190062afddf1d4bcb39');
grSU.query();
var obj = {};

while (grSU.next()) {
var email = grSU.sys_user.email + '';
if (email) {
obj[email] = true;
}
}

return Object.keys(obj).toString();


This adds a basic validation to skip blank or null emails, which directly solves your flow error issue.



If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Uday Rathore
ServiceNow Developer
LinkedIn: https://www.linkedin.com/in/uday-rathore049/

Chaitanya ILCR
Kilo Patron

Hi @Cirrus ,

 

Try this

added sys_user.emailISNOTEMPTY to encodedQuery to filter out records where user's email is empty

 

var grSU = new GlideRecord('service_subscribe_sys_user');
grSU.addEncodedQuery('sys_user.emailISNOTEMPTY^service_offering=40c3aeb0974f2550063c7e371153affb^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19^ORservice_offering=e49231771b22b190062afddf1d4bcb39');
grSU.query();
var obj = {};

while (grSU.next()) {
    obj[grSU.sys_user.email] = true;
}

return Object.keys(obj).toString();

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

anand-bhosle
Tera Guru

@Cirrus Leverage dot-walking in your GlideRecord query so you never even pull back subscriptions whose user’s email is empty:
like this code<

var grSU = new GlideRecord('service_subscribe_sys_user');
// your existing service_offering filters, plus “sys_user.emailISNOTEMPTY”
grSU.addEncodedQuery(
'service_offering=40c3aeb0974f2550063c7e371153affb' +
'^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19' +
'^ORservice_offering=e49231771b22b190062afddf1d4bcb39' +
'^sys_user.emailISNOTEMPTY'
);
grSU.query();

var uniqueEmails = {};
while (grSU.next()) {
// dot-walk directly: grSU.sys_user.email
uniqueEmails[grSU.sys_user.email.toString()] = true;
}

return Object.keys(uniqueEmails).toString();
------------------
On other hand, you can also other approach. Keep your existing query and simply skip blank emails when building the object:

var grSU = new GlideRecord('service_subscribe_sys_user');
// your existing service_offering filters, plus “sys_user.emailISNOTEMPTY”
grSU.addEncodedQuery(
'service_offering=40c3aeb0974f2550063c7e371153affb' +
'^ORservice_offering=27c0565a1bde71d0062afddf1d4bcb19' +
'^ORservice_offering=e49231771b22b190062afddf1d4bcb39' +
'^sys_user.emailISNOTEMPTY'
);
grSU.query();

var uniqueEmails = {};
while (grSU.next()) {
// dot-walk directly: grSU.sys_user.email
uniqueEmails[grSU.sys_user.email.toString()] = true;
}

return Object.keys(uniqueEmails).toString();


Both options work & prevent you from returning empty strings. So choose the helpful one.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Thanks

Anand