Friday, 28 February 2025

How Can We Update a Field Upon Workflow Approval in Business Central?

 

Business Central’s workflows automate actions based on workflow responses. One common requirement is to update a specific field once a workflow is approved.

Updating the Description Field in General Journal Batch Workflow.

In this scenario, when a General Journal Batch is approved, the Description field should be updated with "Account Type + Account No.". This can be achieved by leveraging Business Central’s approval response mechanism.

Step 1: Identify the Approval Response Code Path

  • The approval process navigates through codeunit 1521 and then to codeunit 1535.
  • Within codeunit 1535, there is a publisher OnApproveApprovalRequestsForRecordOnBeforeApprovalEntryToUpdateModify.
  • This publisher allows modify records upon approval.

Step 2: Implement the Code to Update the Description Field

Use the following AL code to subscribe to the event and update the Description field:

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Approvals Mgmt.", OnApproveApprovalRequestsForRecordOnBeforeApprovalEntryToUpdateModify, '', false, false)]

local procedure "Approvals Mgmt._OnApproveApprovalRequestsForRecordOnBeforeApprovalEntryToUpdateModify"(var ApprovalEntryToUpdate: Record "Approval Entry")

var

    RecRef: RecordRef;

    GenJournalBatch: Record "Gen. Journal Batch";

    GenJournalLine: Record "Gen. Journal Line";

begin

    if not RecRef.Get(ApprovalEntryToUpdate."Record ID to Approve") then

        exit;

 

    case RecRef.Number of

        Database::"Gen. Journal Batch":

            begin

                RecRef.SetTable(GenJournalBatch);

                GenJournalLine.Reset();

                GenJournalLine.SetRange("Journal Template Name", GenJournalBatch."Journal Template Name");

                GenJournalLine.SetRange("Journal Batch Name", GenJournalBatch."Name");

                if GenJournalLine.FindSet() then

                    repeat

                        GenJournalLine.Description := format(GenJournalLine."Account Type") + ' ' + GenJournalLine."Account No.";

                        GenJournalLine.Modify();

                    until GenJournalLine.Next() = 0;

            end;

 

        Database::"Gen. Journal Line":

            begin

                RecRef.SetTable(GenJournalLine);

                if GenJournalLine.FindSet() then

                    repeat

                        GenJournalLine.Description := 'Set the value as per your requirement';

                        GenJournalLine.Modify();

                    until GenJournalLine.Next() = 0;

            end;

    end;

end;

Step 3: Deploy and Test the Code

1.      Deploy the extension containing the event subscriber.

2.      Submit a General Journal Batch for approval.

3.      Approve the batch and verify that the Description field is updated.



Conclusion

This approach can be extended to other workflows and responses by identifying the proper publisher.

 


No comments:

Post a Comment