Async Business Rule not working with Condition

Carl Fransen1
Tera Guru

Hi Team,

A weird one today that I ca't figure out, hoping someone can help me with this.  My requirements is to copy the attachments from the originating HR Case to the Approval record - this works fine with the below and all attachments are copied to each approval task created as part of the workflow attached when creating the HR Case via the portal (using a record producer).

BR on sysapproval_approver table, no Condition, Async Rule, on insert, advanced script is:

(function executeRule(current, previous /*null when async*/) {

GlideSysAttachment.copy('sn_hr_core_case_workforce_admin', current.sysapproval.sys_id, 'sysapproval_approver', current.sys_id);

})(current, previous);

So the above works perfectly - for all cases.  However now I want to limit this to only a particular HR Service.

However - if I add a condition in the 'when to run' tab this code it stops working.  I have tested a simple message using my condition and this works, so I know the condition is correct.  However when using them both together this doesn't work. 

The condition is dot walked approval for->HR Case->HR Service.  If I use a simpler condition - like status='Not Yet Requested' this works.

Anyone have ideas why this might be the case?

 

Thanks

Carl.

1 ACCEPTED SOLUTION

Carl Fransen1
Tera Guru

Hi All,

I found a few solutions to this so am documenting these for anyone else in the future.  The best solution will depend on your requirements.

After investigating this further it seems the 'approval for' (syspaproval) field wansn't blank, but seemed to have an invalid link (or incomplete HR Case as it couldn't find the HR Service) when the Approval Task was created.

Solution1

-Add a 'timer' into the workflow BEFORE the 'Generate Approvals' action (2 minutes worked for me).

-Write the BR: Table='Approval', Async, on Insert, condition 'Approval for.HR Case. HR Service=<required service>'

Script is:

(function executeRule(current, previous /*null when async*/) {
GlideSysAttachment.copy('sn_hr_core_case_workforce_admin', current.sysapproval.sys_id, 'sysapproval_approver', current.sys_id);
})(current, previous);

 

This will wait before creating the approval records and this seems to ensure the link to the HR Case is complete, with all the HR Case details, so the condition to the HR Service works great.

Note:  This means the user won't see the approval tasks being generated at the time of logging, so this might lose some visibility for them.

Solution2

-Add a 'timer' into the workflow AFTER the 'Generate Approvals' action (2 minutes worked for me) and ensure it's BEFORE the first approval.

-Write the BR: Table='Approval', Async, on Update, condition: 'Approval for.HR Case. HR Service=<required service>' AND 'updates is 1''

Script is the same as above.

 

This solution will create the approval records as soon as the HR Case is logged and the workflow is run.  It then WAITS 2 minutes and then moves to the approval task which updates the status from 'not yet requested' to 'requested' - at this time the 'updates' field changed from 0 to 1 and therefore the condition is valid, the attachments are copied correctly (ONLY ONCE).

The additional benefit of this solution is that the attachments are't copied until the approval task is activated (set to 'requested') so if any attachments are added, removed, updated etc.. these will show correctly in the later approval tasks.

It allows the customer to see the approvals tasks being created right at the front, which provides them the view of these when they first log their HR Case.

 

Hope this helps someone else out - thanks everyone for your help with this.

 

Cheers

Carl.

View solution in original post

18 REPLIES 18

thanks, however I tried that - and then the attachments are copied EACH time the approval record is updated and not just once.

I'm looking at currently using the 'updates' field along with 'update' so it only runs once....

 

Thanks

Carl.

Can you share the details of your business rule? What you report makes me suspect the condition is implemented differently than what I had intended. If the condition is:

current.sysapproval.changes() (or the condition builder under 'when to run' says "Approval for | changes", then it shouldn't be running every time the record is updated - only when that field is changed.

Hi Chuck - I reread your suggestion and realised I had missed the 'sysapproval' field and just done the 'update'.  Testing this however didn't work as it seems that the value must be set but not changed/updated.

I tried adding a timer in before the 'generate approvals' in the workflow and this seemed to work fine - interesting as to why, but I can only think that the HR Case record is completed and then the approvals are generated with a valid link back to the HR Case as now the attachments copy fine.

Thanks for your help with this.

 

Cheers

Carl.

Carl Fransen1
Tera Guru

Hi All,

I found a few solutions to this so am documenting these for anyone else in the future.  The best solution will depend on your requirements.

After investigating this further it seems the 'approval for' (syspaproval) field wansn't blank, but seemed to have an invalid link (or incomplete HR Case as it couldn't find the HR Service) when the Approval Task was created.

Solution1

-Add a 'timer' into the workflow BEFORE the 'Generate Approvals' action (2 minutes worked for me).

-Write the BR: Table='Approval', Async, on Insert, condition 'Approval for.HR Case. HR Service=<required service>'

Script is:

(function executeRule(current, previous /*null when async*/) {
GlideSysAttachment.copy('sn_hr_core_case_workforce_admin', current.sysapproval.sys_id, 'sysapproval_approver', current.sys_id);
})(current, previous);

 

This will wait before creating the approval records and this seems to ensure the link to the HR Case is complete, with all the HR Case details, so the condition to the HR Service works great.

Note:  This means the user won't see the approval tasks being generated at the time of logging, so this might lose some visibility for them.

Solution2

-Add a 'timer' into the workflow AFTER the 'Generate Approvals' action (2 minutes worked for me) and ensure it's BEFORE the first approval.

-Write the BR: Table='Approval', Async, on Update, condition: 'Approval for.HR Case. HR Service=<required service>' AND 'updates is 1''

Script is the same as above.

 

This solution will create the approval records as soon as the HR Case is logged and the workflow is run.  It then WAITS 2 minutes and then moves to the approval task which updates the status from 'not yet requested' to 'requested' - at this time the 'updates' field changed from 0 to 1 and therefore the condition is valid, the attachments are copied correctly (ONLY ONCE).

The additional benefit of this solution is that the attachments are't copied until the approval task is activated (set to 'requested') so if any attachments are added, removed, updated etc.. these will show correctly in the later approval tasks.

It allows the customer to see the approvals tasks being created right at the front, which provides them the view of these when they first log their HR Case.

 

Hope this helps someone else out - thanks everyone for your help with this.

 

Cheers

Carl.