[EventSubscriber(Codeunit,1521,OnAddWorkflowResponsesToLibrary,"",Skip)] PROCEDURE CreateResponsesLibrary(); VAR WorkflowRespHandl: Codeunit 1521; BEGIN WorkflowRespHandl.AddResponseToLibrary('<Your response code>', DATABASE::"Purchase Header",'<response desctiption>' ,'GROUP 0'); //the 4th parameter controls workflow step parameters, the value of 'GROUP 0' shows none END
[EventSubscriber(Codeunit,1521,OnExecuteWorkflowResponse)] PROCEDURE ExecuteResponse(VAR ResponseExecuted : Boolean;Variant : Variant; xVariant: Variant;ResponseWorkflowStepInstance : Record 1504); VAR WorkflowResponse : Record 1521; BEGIN IF WorkflowResponse.GET(ResponseWorkflowStepInstance."Function Name") THEN BEGIN CASE WorkflowResponse."Function Name" OF '<Your response code>': BEGIN YourCheckFunction(Variant); ResponseExecuted := TRUE; END; END; END; END;
Answers
Nope, but just want to share my experience with handling similar requirements.
My first approach was similar to yours - subscribe to OnSendPurchaseDocForApproval and put my checking code into the handler.
I found this a bit problematic as even If my code thrown error and stop the workflow the error message was shown after the standard confirmation message. This of course was the result of binding order - standard subscriber was fired first and then mine, just like in your case.
I ended up creating a function adding workflow step response event, and plugged it into the purchase approval workflow. This apporach has, in my opinion a few advantages - it is clearly visible for the users thas there is a step checking validity of certain data, you can control order of checking by changing workflow configuration, and of course it can be fired selectively for some purchase approval workflows but not for others - all user configurable.
It is still fully event-based, all encompassed in a single codeunit, no standard object modification required.
I guess the bug like you described should be reported to Microsoft, I don't think that you can do anyting in the code to sort it aout - apart from complete redesign of your solution.
The 'dirty' trick would be to modify standard workflow event handling codeunit and make your subscriber function to appear first before the standard subscriber to OnApproveApprovalRequest, or modify some other codeuin with lower ID and add your subscribe funtion there.
It seems that subscribers are bound to publishers in ascending order of object ID and then in order of apperance in the code. This is my observation only, if correct it could help firing the code in the 'righ' order, but of course it goes agains all event based coding practice as you would have to modify some standard object to get it working.
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
I guess I will have to look closer into this guide:
https://msdn.microsoft.com/en-us/library/mt574349(v=nav.90).aspx
Or did you find a better guide?
Peter
I didn't find anyting better, and I think that Microsoft's doc is a bit overcomplicated
All you need to have is a setup function which will add your Response to the library. The setup is a subscriber to OnAddWorkflowResponsesToLibrary in codeunit 1521:
Then you need a handler function, which is a subscriber to ExecuteResponse in codeunit 1512:
Your check function needs to retrieve Purchase Header from th Variant passed in the call, and do whatever is supposed to do.
There is of course more you can add to it, define dependencies, '<Your response code>' should be a function call returing the response code, to conform with how Microsoft did theirs, but all in all this is the very basic skeleton which should work for you.
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
I'm just sorry I already flagged your first reply with Awesome How do I now raise my review for this new reply?
If you are going to NavTechDays in Antwerpen I'll surely get you a beer or whatever you are into.
Peter
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
However, we kept getting purchase documents in an invalid state in other situations, and ended up removing the COMMIT in Codeunit 453. This seems to have solved our problems.
Can you see any purpose of the COMMIT? We concluded that it was safe to remove the COMMIT, and it was even an advantage to get the JobQueueEntry rolled back and thereby get rid of the notification e-mail if an error happens. The "Job Queue Error Handler" quietly exits if the JobQueueEntry has been rolled back.
Peter
Slawek
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
Peter