Trigger an email based on two conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2022 05:20 PM
I have the following code which obviously does not work. The idea is to send an email using Event is fired.
I want to include in the email the part numbers where the Ready for UDI Data Entry is "Yes" and the Complete Status is "In Progress".
Each record is associated to an SCTASK, so I need to send one email where the Task No's are the same so I can include the part numbers for that Task No.
In this example when I change the Ready for UDI Data Entry to Yes, it should trigger an email notification for SCTASK0080343 that includes its respective Part Numbers, same case for SCTASK0080220 but that should be a separate email.
This is the scheduled script I have. Any help on this please?
var udi_gr = new GlideRecord('u_udi_tracking_data');
var gr = new GlideRecord('sc_task');
//gr.addQuery('state', 'Open'); //Check if the state is open
udi_gr.addEncodedQuery('u_status=Yes^u_complete_status!=Complete');
udi_gr.addQuery('udi_gr.u_task', 'gr.getUniqueValue()');
udi_gr.query();
while(udi_gr.next()) {
gs.eventQueue('UDI.EmailSME', udi_gr); //It will fire the event
}
- Labels:
-
Notifications

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2022 06:24 PM
Hello,
I'm unsure why you decided to post the same question 2-3+ times on the forums. Especially when time has been taken by others...including myself, to help you in those other threads...
Duplicate posts:
- https://community.servicenow.com/community?id=community_question&sys_id=facce6d4db08dd542dc24f781396...
- https://community.servicenow.com/community?id=community_question&sys_id=3808374edba74dd0770be6be1396...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2022 09:11 AM
You didn't help on my question, that's the reason I posted my question on another community to improve the chance on getting a solution. I don't know why you have to say "Duplicated Posts". This is the purpose of forums, I'm just looking for solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2022 02:12 PM
Here's what I'd try:
- create two business rules, one onBefore (order 100000+), on onAfter
- make both only run if condition
GlideTransaction.get().getRequestParameter('sysparm_processor') == 'com.glide.ui_list_edit.AJAXListEdit';
* is met. - make the Script of the onBefore BR to be:
var U_UPDATED_COUNT = U_UPDATED_COUNT || getUpdatedRowsCount(); var U_UPDATED_UNIQUEVALUE = U_UPDATED_UNIQUEVALUE || []; var U_UPDATED_COMMITTED = U_UPDATED_COMMITTED || 0; var U_UPDATED_ABORTED = U_UPDATED_ABORTED || 0; if (current.isActionAborted()) U_UPDATED_ABORTED = U_UPDATED_ABORTED + 1; function getUpdatedRowsCount () { var sysparm_xml = GlideTransaction.get().getRequestParameter('sysparm_xml'), payload = gs.xmlToJSON(sysparm_xml); return Object.prototype.toString.call(payload.record_update.record) == '[object Array]' ? payload.record_update.record.length : 1; }​
- make the Script of the onAfter BR to be:
U_UPDATED_COMMITTED = U_UPDATED_COMMITTED + 1; U_UPDATED_UNIQUEVALUE.push(current.getUniqueValue()); if ((U_UPDATED_COUNT - U_UPDATED_COMMITTED - U_UPDATED_ABORTED) == 0) // Add code to raise event: // gs.eventQueue('<an event name>', <a GlideRecord, probably that of the current user>, U_UPDATED_UNIQUEVALUE); gs.addInfoMessage('U_UPDATED_UNIQUEVALUE: ' + (U_UPDATED_UNIQUEVALUE || []).join(', '));​
That's about it.
Of course the final version of the onAfter BR would not have the addInfoMessage instruction, and would contain code to load the current user's record and the event raising instruction would be un-commented. This is "necessary" because using current might not be the best option: is just one of the updated records, whereas one would need something common to all.
Needless to say the code must be hardened to handle faulty, malformed or missing input data (especially in function getUpdatedRowsCount
).
*) The condition (just as the function counting the updated rows) makes use of the fact that when updating multiple records in UI16, processor com.glide.ui_list_edit.AJAXListEdit
is called (among others with parameter sysparm_xml
that contains the list of updated records).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2022 02:20 PM
This is how it looks just with the addInfoMessage
instruction after having updated the first 3 rows with "8" in "Short description":
There is just 1 message, containing all 3 sys_id
s updated, right after the update is done, without later detection, update of custom columns, flows, etc.
Forgot to raise attention in previous post, very important, both Business Rule must not be written as in IEFE, so no:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
})(current, previous);
The variables used in the Business Rules must be super global. If written using the "regular" way, those will not be global and the solution will not work. That's why the variables are prefixed by U_
: to make those a bit more global-safe.