Hi Readers,
I am sure everyone was able to publish your extension based on the Last article. If you haven't read the earlier article, then refer to the table of content.
As promised in the article we will add code to check Source of sales is filled in during the posting of sales order as per customer requirements.
The user should be able to select the Source of sales in Sales documents and its a mandatory field to key in before posting a sales document.
Let's try to add this Code to our extension.
Let's go back to our C/AL Knowledge - At what point during sale order posting, Microsoft checks the document?
If you remember Microsoft use a function in Codeunit 80 - "CheckAndUpdate". Let's see do we an event similar to that in Codeunit 80.
Now, this is not Codeunit Extension. We will be subscribing events that are defined in Codeunit 80 so we will not call new file as Codeunit extension. We will call this Subscribing Codeunit.
In one Codeunit we can subscribe to multiple events defined in multiple base objects. In this case, I would like to keep all my related code segment in one Codeunit. This Codeunit will have all subscribers.
Add a new folder "Codeunit" & a file on the Left-hand Panel - right click & select New File as shown below.
As per our standard page file name is - SDH_Codeunit_50000_ExtensionBlogSubscribers.al
Let's follow the routine and use snippet tcodeunit to get the structure of a Codeunit.
Next Step to set these Page Properties -
Assign ID and
Provide a name to the Codeunit that you are adding.
As you can see in above image, snippet added default onRun Trigger in the Codeunit and a Global variables section which was added.
Next is to add subscriber for Codeunit 80 Events. We will use snippet teventsub to get the skeleton for subscribing as shown below.
Now once we select that snippet we will see the parameters that require to be defined as shown below.
Parameters are -
Remember it like this -If something happens, in some object name of some object type then call the procedure. Below are the details which should help too -
Let's Start assigning values to parameter -
Next Question that you might be thinking - We know that CheckSalesOrderSourceofSales will be called when sales order posting routine is called but how can I check the record of sales Header?
If you notice record variable passed are passed as VAR that means if we modify those records here it will update actual record. So I will add all available variables and write our code. To add the available parameter you can use ; as the separator as shown below.
The last thing is to add code for which we all are experts 😎
With this code system will check Source of sales is mandatory while posting sales order.
Hope it makes sense and you learn how to subscribe to Events published.
Do share your views as comments to this article. Next article we will add more events in the same Codeunit to complete user requirements.
Stay Tuned.
Regards,
Saurav Dhyani
www.sauravdhyani.com
I am sure everyone was able to publish your extension based on the Last article. If you haven't read the earlier article, then refer to the table of content.
As promised in the article we will add code to check Source of sales is filled in during the posting of sales order as per customer requirements.
The user should be able to select the Source of sales in Sales documents and its a mandatory field to key in before posting a sales document.
Let's try to add this Code to our extension.
Let's go back to our C/AL Knowledge - At what point during sale order posting, Microsoft checks the document?
If you remember Microsoft use a function in Codeunit 80 - "CheckAndUpdate". Let's see do we an event similar to that in Codeunit 80.
Now, this is not Codeunit Extension. We will be subscribing events that are defined in Codeunit 80 so we will not call new file as Codeunit extension. We will call this Subscribing Codeunit.
In one Codeunit we can subscribe to multiple events defined in multiple base objects. In this case, I would like to keep all my related code segment in one Codeunit. This Codeunit will have all subscribers.
Add a new folder "Codeunit" & a file on the Left-hand Panel - right click & select New File as shown below.
As per our standard page file name is - SDH_Codeunit_50000_ExtensionBlogSubscribers.al
Let's follow the routine and use snippet tcodeunit to get the structure of a Codeunit.
Next Step to set these Page Properties -
Assign ID and
Provide a name to the Codeunit that you are adding.
As you can see in above image, snippet added default onRun Trigger in the Codeunit and a Global variables section which was added.
Next is to add subscriber for Codeunit 80 Events. We will use snippet teventsub to get the skeleton for subscribing as shown below.
Now once we select that snippet we will see the parameters that require to be defined as shown below.
Parameters are -
Remember it like this -If something happens, in some object name of some object type then call the procedure. Below are the details which should help too -
Let's Start assigning values to parameter -
- ObjectType::Codeunit
- Codeunit::"Sales-Post"
- OnAfterCheckSalesDoc - Event that we want to subscribe.
- '' - ** This parameter only applies to Table Fields.
- TRUE
- TRUE
- CheckSalesOrderSourceofSales - Function Name that we want to name the function.
Next Question that you might be thinking - We know that CheckSalesOrderSourceofSales will be called when sales order posting routine is called but how can I check the record of sales Header?
- Every event that is published will have some parameters that we can utilize in our function/procedure. Let's see how can I check what parameters are available.
- In Procedure parameter, if you click Ctrl+Spacebar system will show you all available parameter available as shown below.
If you notice record variable passed are passed as VAR that means if we modify those records here it will update actual record. So I will add all available variables and write our code. To add the available parameter you can use ; as the separator as shown below.
The last thing is to add code for which we all are experts 😎
With this code system will check Source of sales is mandatory while posting sales order.
Hope it makes sense and you learn how to subscribe to Events published.
Do share your views as comments to this article. Next article we will add more events in the same Codeunit to complete user requirements.
Stay Tuned.
Regards,
Saurav Dhyani
www.sauravdhyani.com
Comments
Post a Comment