Posting to G/L from multiple Codeunits

davonessdavoness Member Posts: 22
G'day All,

I have a problem, I'm wondering if anyone has come across something similar.

In Cod 22, Item Jnl Post Line, I have a mod that under certain circumstances needs to build a Gen Jnl Line and Post it running GenJnlPostLine.RunWithCheck.

I am getting an error saying "G/L Entry No. %1 already exists" as Inventory Posting to G/L has already inserted G/L Entries and the instance of GenJnlPostLine in Cod 22 does not know about them. Therefore when the InitCodeunit function runs to determine the NextEntryNo it returns an Entry Number taken in Inventory Posting to G/L.

What I'm curious about is how this works in a standard sales post, where Inventory Posting to G/L inserts G/L Entries, yet when Sales Post calls GenJnlPostLine and the InitCodeunit function is run, the GLEntry.FIND('+') picks up the entries inserted.

Thanks
Dave

Comments

  • ara3nara3n Member Posts: 9,257
    You need to use the use the same variable that posts to G/L. You can CLEAR the CU but that could cause inconsistency depending on what part you are posting your entry.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • ClausHamannClausHamann Member Posts: 80
    Yes, I have seen this a few times before but I have been able to post what I wanted by rearranging my code.

    In codeunit 80 all inventory postings (and related GL postings) are completed before codeunit 80 start to post to GL directly. The last GL Entry No. read by GenJnlPostLine in codeunit 80 is the last Entry No. created by the item related GL posting (from C22 - > C5802).

    The issue you have with GL postings from codeunit 22 is that you are posting in between item related GL posting from codeunit 80 and the GL postings posted directly from codeunit 80.

    Regards

    Claus
  • ClausHamannClausHamann Member Posts: 80
    One way to solve your issue could be to move you GL posting code from codeunit 22 to codeunit 5802 and use the existing instance of GenJnlPostLine, but this depends of you code.

    I hope this helps.

    Claus
  • davonessdavoness Member Posts: 22
    Thanks for the replies

    ClausHammann, I see what you're saying, however the problem occurs even when I'm posting an Item Journal. In this case the theory is similar to a standard sales post, ie. two different codeunits posting to G/L through their own instances of GenJnlPostLine one after the other.

    What is it that allows the instance in Cod80 to see the lines posted in Cod5802, where if I'm just posting an Item Jnl the instance in Cod22 can't find the lines created by Cod5802.

    Dave
  • davonessdavoness Member Posts: 22
    Yeah as far as moving the code goes, I've considered that.

    I think I may have to do that, I have created a new ledger which is posted to through Cod22, and I think to be technically correct I should have the G/L impact posted through Cod5802 which would allow for switching Automatic Cost Posting on/off. Unfortunately I am upgrading the mod from V2.60 this would mean a major re-write.

    I may end up going that way but I am still interested in the reasons behind the problem as I'm sure they will be relevant to other things I do.

    Dave
  • ClausHamannClausHamann Member Posts: 80
    Hi davoness

    Are you getting the issue when you only post one line from an Item Journal or do you only get the issue when you post more than one line from an Item Journal?

    I don’t think your issue is that you code in C22 can’t see GL lines created from C5802. I think you issue is that you code in C22 is posting in between the C5802 postings and since the GL posting from you code and C5802 only reads the GL Entry No. once, the two GL postings will eventual use the same GL entry No.

    The reason it work for C80 is that C80 finished all C22 (C5802) GL postings first and then starts to post GL Entries directly from C80.

    I hope this makes sense.

    Regards

    Claus
  • davonessdavoness Member Posts: 22
    Ahhhhh!

    It all makes sense now. Thanks mate.

    Dave
  • ClausHamannClausHamann Member Posts: 80
    You are welcome.
  • jreynoldsjreynolds Member Posts: 175
    It is possible to pass a codeunit as a parameter to a function.

    It may be possible to structure your code so that you create an instance of codeunit 12 in codeunit 22. You can use this instance to to do your G/L postings in codeunit 22 and change the calls to codeunit 5802 in codeunit 22 so that this same instance of codeunit 12 is passed into the functions of codeunit 5802. Then when codeunt 5802 does its G/L postings it is using the same instance of codeunit 12 that you used.
  • ara3nara3n Member Posts: 9,257
    jreynolds
    You can use single instance property of a codeunit, but this won't work and defintely not the solution. He could write them to a different single instace codeunit and then post them later.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • jreynoldsjreynolds Member Posts: 175
    ara3n wrote:
    jreynolds
    You can use single instance property of a codeunit, but this won't work and defintely not the solution. He could write them to a different single instace codeunit and then post them later.
    I'm not talking about a single instance codeunit, I'm talking about passing a codeunit as a parameter to a function in another codeunit. When you do this you are in fact passing the same instance of the codeunit to the other function.
  • ara3nara3n Member Posts: 9,257
    I know what you are saying, it just it cannot be done in navision.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • jreynoldsjreynolds Member Posts: 175
    ara3n wrote:
    I know what you are saying, it just it cannot be done in navision.
    I have done this so I know that it can be done.
  • ara3nara3n Member Posts: 9,257
    You are right it can be done. :oops:. Never actually tried it.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
Sign In or Register to comment.