Hi all,
hope to get some help with this issue.
I need this functionality: When a new G/L account is created in a company this G/L account is also created in all other companies. (only the primary key = "No.")
I have tried:
Table 15 G/L AccountOnInsert()
GlNoVar := Rec."No.";
CompanyRec.SETFILTER(Name,'<>%1',COMPANYNAME);
CompanyRec.FINDFIRST;
REPEAT
GLAccountRec.CHANGECOMPANY(CompanyRec.Name);
GLAccountRec."No." := GlNoVar;
GLAccountRec.INSERT(TRUE);
UNTIL CompanyRec.NEXT = 0;
but this (ofcourse) causes a recursive error. Any ideas on how this could be done?
Thanks
Comments
the insert(true) will be the culprit. The Changecompany() function only affects TableData, not the underlying C/AL context of the record. So, your insert trigger will always be executed in the company you've opened.
with best regards
Jens
Aside from the recursive problem you're creating, that is... The source of the error is the same.
with best regards
Jens
What about fields onvalidate code - it that the same case?
it's the same for any code in tables. The context of these variables doesn't change when a changecompany() was issued on the record. I would try to avoid structures which try to synchronize data over companies in table triggers. There is the "except sometimes" rule which always applies (except sometimes;). This would make the whole structure complex and complicated to manage.
Synchronizing accounts is a master data management problem. The best practice I use is having a master company defining the allowed accounts and synchronizing from there (for the records/fields that make sense to be synchronized). To make it more usable, there is a "get new account" function showing the allowed accounts for the company I'm working in. It's the only way to create new accounts, so there are no deviations/conflicts in the local companies.
with best regards
Jens
Br,
Alvi
This will essentially mean you have one table for all companies to share. Flow fields (e.g., balances, etc.) will work fine - it's only the master configuration that is shared. This saves you from having to write code to keep things in sync when accounts are added or edited.
One side effect of this approach is that if you're accessing the SQL table directly for reporting, etc., it will no longer include the company name in it.
- Mark
Nooo. Please don't do this. :thumbsdown:
:thumbsup:
This is almost exactly the way I do it.
I add a boolean in the COMPANY data field "Use As Master" and a field "Master Company" linked back on it self to the company table filtered by Use As Master = FALSE and code to lock out a loop.
Then a new boolean per each table to sync
"Sync Master G/L"::Boolean
"Sync Master Customer"::Boolean
"Sync Master Item"::Boolean
etc.
Now in each master table add code that prevents modification if "Master Company" is selected. So you can only work on the master tables from the Master company. And you can still have normal companies AND you can even have multiple master companies if you need. There will be locking issues, but they are simple to resolve.
By the way I normally also create a new ledger table that is common to all companies, and adds company name to the primary key (Company Name, Entry No.) and a batch to keep those synced at night, which makes it easy to view items by company or consolidated AR etc.
The DataPerCompany property is actually a very good option in a lot of cases, and shouldn't be dismissed out-of-hand without a discussion of it merits and limitations.
We've used this numerous times with simple tables like units of measure (though never with slightly more complex tables like the G/L), and it is a much better approach with basic tables than writing code to synchronize a master company (which we've also done for more complex tables like Item where we also want to copy BOMs, extended text, etc. based on user selections).
If, as I stated, you want the chart of accounts to match exactly between companies (i.e., have essentially a common chart of accounts), then the DataPerCompany property may be worth evaluating.
Now, the downsides to using this approach:
The upside, of course, is that no modifications, special master company, or processes are required to keep the master data (but not related data) synchronized between companies. If the functionality matches your requirements, then it is an approach worth at least evaluating.
- Mark
How many more times do you think it needs to be discussed?