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

ok done that, where do I look to see this log entry?

type syslog.list

 

Find your log

if its not present then there is something wrong with the br condition

 

if its present then we have to chk the script

I can see the log entry so I know it's being run.

I updated the rule to also run on 'update' and found that this now works when the approval record is updated, so all the conditions and script are working fine.

 

I believe this is not working on 'insert' because the 'approval for' field does not have a value when first created by the workflow and therefore BR condition is being run and failing as there is no value.

 

Any idea how to get around this?

Carl Fransen1
Tera Guru

Update:  I believe this is not working on 'insert' because the 'approval for' field does not have a value when first created by the workflow and therefore BR condition is being run and failing as there is no value.

Does anyone know how I can work around this creation timing for approval tasks and BR's?

 

Thanks

Carl.

If the sysapproval field is getting a value AFTER the sysapproval_approver record is being created, how about change your BR to trigger when sysapproval changes?