Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to trigger second activity set based on the advance condition and audience

vijani
Giga Guru

Hello All, I want to trigger the current activity set based on audience criteria if the previous activity set was not triggered due to its task conditions.
I have set the trigger type to ‘Advanced’ with conditions based on the previous activity set and its tasks. It works as expected when the previous activity set is triggered. However, if the previous activity set is not triggered, the current activity set does not trigger based on the assigned audience. so may i know how to fix this.
Help would be highly appreciated.

   (function shouldActivitySetTrigger(parentCase /* GlideRecord for parent case */,
                                   hrTriggerUtil /* hr_TriggerUtil script include instance */) {
 
    var parentCaseSysId = parentCase.getValue('sys_id');
     var hrService = gs.getProperty('sn_hr_core.service');
 
    // Previous Activity Set IDs to check
    var previousActivitySetIds = ['f72a18a01bb886540b9a11739b4aab78'];
    
    // Check if previous activity sets are completed
    if (hrTriggerUtil.checkActivitySetsCompleted(previousActivitySetIds)) {
         var grTask = new GlideRecord('sn_hr_core_case_talent_management');
        grTask.addQuery('parent', parentCaseSysId);
        grTask.addQuery('hr_service', hrService);
        grTask.addQuery('state', '3'); // Closed Complete
        grTask.addQuery('approval', 'approved');
           grTask.query();
             if (grTask.next()) {
         
            return true;
        } else {
                        return false;
        }
       }
 
})(parentCase, hrTriggerUtil);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             @michaelj_sherid                                                                                                                                                                                                                                                                                                                                                                                                                           
1 ACCEPTED SOLUTION

vijani
Giga Guru

Hi @Kieran Anson Sorry for the late reply. I applied the audience condition, and now it’s working as expected.
Thank you so much for your quick response.

(function shouldActivitySetTrigger(parentCase /* GlideRecord for parent case */ ,
    hrTriggerUtil /* hr_TriggerUtil script include instance */ ) {
 
    var parentCaseSysId = parentCase.getValue('sys_id');
    var subjectPerson = parentCase.getValue('subject_person');
    var journeyUsers = gs.getProperty('First_Activity_set_Audience'); // first activity set audience 
    var journeyUsersArray = journeyUsers ? journeyUsers.split(',') : [];
 
    // Check if subjectPerson is in journeyUsers list
    if (journeyUsersArray.indexOf(subjectPerson) !== -1) {
        var hrService = gs.getProperty('sn_hr_core.test_service'); // HR service 
 
        // Previous Activity Set IDs to check
        var previousActivitySetIds = gs.getProperty('sn_jny.Previous_Activity_Set'); // Previous activity set sys_id
        var activitySetArray = previousActivitySetIds ? [previousActivitySetIds] : [];
 
        if (hrTriggerUtil.checkActivitySetsCompleted(activitySetArray)) {
            var grCase = new GlideRecord('sn_hr_core_case_payroll');
            grCase.addQuery('parent', parentCaseSysId);
            grCase.addQuery('hr_service', hrService);
            grCase.addQuery('state', '3'); 
            grCase.addQuery('approval', 'approved');
             grCase.query();
 
            if (grCase.next()) {
 
                return true;
 
            } else {
 
                return false;
            }
        } else {
 
            return false;
        }
    } else {
 
        return true;
    }
 
})(parentCase, hrTriggerUtil);

 

View solution in original post

4 REPLIES 4

Kieran Anson
Kilo Patron

Hi,

If you navigate to the sn_hr_le_activity_set_context table, and filter on the case you're using for testing, do you see the activity set listed? What state is it in?

Hi @Kieran Anson the state is showing skipped

Thanks for confirming, if you add logging, does the if statement log out? It should based on the function checking for activities not in a state of 'finished'

 

 (function shouldActivitySetTrigger(parentCase /* GlideRecord for parent case */ ,
     hrTriggerUtil /* hr_TriggerUtil script include instance */ ) {

     var parentCaseSysId = parentCase.getValue('sys_id');
     var hrService = gs.getProperty('sn_hr_core.service');

     // Previous Activity Set IDs to check
     var previousActivitySetIds = ['f72a18a01bb886540b9a11739b4aab78'];

     // Check if previous activity sets are completed
     if (hrTriggerUtil.checkActivitySetsCompleted(previousActivitySetIds)) {
		gs.info('HRLog - Checking Activity State');
         var grTask = new GlideRecord('sn_hr_core_case_talent_management');
         grTask.addQuery('parent', parentCaseSysId);
         grTask.addQuery('hr_service', hrService);
         grTask.addQuery('state', '3'); // Closed Complete
         grTask.addQuery('approval', 'approved');
         grTask.query();
         if (grTask.next()) {
				gs.info('HRLog - Checking Activity State - Found Case');
             return true;
         } else {
			gs.info('HRLog - Checking Activity State - Did Not Find Case');
             return false;
         }
     }

 })(parentCase, hrTriggerUtil);

vijani
Giga Guru

Hi @Kieran Anson Sorry for the late reply. I applied the audience condition, and now it’s working as expected.
Thank you so much for your quick response.

(function shouldActivitySetTrigger(parentCase /* GlideRecord for parent case */ ,
    hrTriggerUtil /* hr_TriggerUtil script include instance */ ) {
 
    var parentCaseSysId = parentCase.getValue('sys_id');
    var subjectPerson = parentCase.getValue('subject_person');
    var journeyUsers = gs.getProperty('First_Activity_set_Audience'); // first activity set audience 
    var journeyUsersArray = journeyUsers ? journeyUsers.split(',') : [];
 
    // Check if subjectPerson is in journeyUsers list
    if (journeyUsersArray.indexOf(subjectPerson) !== -1) {
        var hrService = gs.getProperty('sn_hr_core.test_service'); // HR service 
 
        // Previous Activity Set IDs to check
        var previousActivitySetIds = gs.getProperty('sn_jny.Previous_Activity_Set'); // Previous activity set sys_id
        var activitySetArray = previousActivitySetIds ? [previousActivitySetIds] : [];
 
        if (hrTriggerUtil.checkActivitySetsCompleted(activitySetArray)) {
            var grCase = new GlideRecord('sn_hr_core_case_payroll');
            grCase.addQuery('parent', parentCaseSysId);
            grCase.addQuery('hr_service', hrService);
            grCase.addQuery('state', '3'); 
            grCase.addQuery('approval', 'approved');
             grCase.query();
 
            if (grCase.next()) {
 
                return true;
 
            } else {
 
                return false;
            }
        } else {
 
            return false;
        }
    } else {
 
        return true;
    }
 
})(parentCase, hrTriggerUtil);