Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Code for checking gl in different companies

Posted on by Microsoft Employee

Hi,

We have a database with +100 companies. For that reason i have created a codeunit to check if there is some gl accounts there are not in use. If it is in use on 1 of the companies its not allowed to get deleted.

My code looks like this.

I have run into trouble - I get a different result depending on if I run it from company A with no gl entries on the accounts versus if I run it from company B with gl entries in the accounts.

Its like my GlEnrty.FINDFIRST is only looking at the company I run the code from. 

OnRun()

counter_g := 0;
GL.SETFILTER("No.", '78000|78005|78995');
IF GL.FIND('-') THEN BEGIN
  REPEAT
    IF CheckAccount(GL."No.",'*I/S') = 0 THEN BEGIN
      counter_g += 1;      
      END;
  UNTIL GL.NEXT = 0;
END;

IF counter_g = 0 THEN BEGIN
  IF GL.FIND('-') THEN BEGIN
    REPEAT
      MESSAGE('Its safe to delete account if you reach this point.');
    UNTIL GL.NEXT = 0;
  END;
END;

MESSAGE('Codeunit done.'); 

LOCAL CheckAccount(pAcc : Code[10];pCompanyFilter : Text[30]) counter1 : Integer
counter1 := 0;
CLEAR(RecCompany);
RecCompany.SETFILTER(Name, '%1', pCompanyFilter);
//ERROR('%1', RecCompany.COUNT);
IF RecCompany.FIND('-') THEN
  REPEAT
    GL.CHANGECOMPANY(RecCompany.Name);  
    MESSAGE(RecCompany.Name);    
      IF GL.GET(pAcc) THEN BEGIN        
          CLEAR(GLEntry);
          GLEntry.SETRANGE(GLEntry."G/L Account No.", pAcc);
          IF GLEntry.FINDFIRST THEN BEGIN
            text := 'Entry found in : ' + RecCompany.Name + ': account no. ' + pAcc + '\' + text;
            counter1 += 1;            
          END;
      END;            
  UNTIL RecCompany.NEXT = 0;
IF text <> '' THEN
MESSAGE(text);


*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Code for checking gl in different companies

    Hi, you miss a GLEntry.CHANGECOMPANY

    CHANGECOMPANY will only change scope of the specified "var". Any local variable in GL will keep scope on the company you run the code from.

    BR Per

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Code for checking gl in different companies

    Hi Suresh,

    I have change to code to this, but I still get different result depending on what company i run the code from.

    OnRun()
    LoopCompanies('*I/S');
    
    LOCAL CheckAccount(pAcc : Code[10]) counter1 : Integer
    counter1 := 0;
    IF GL.GET(pAcc) THEN BEGIN        
        CLEAR(GLEntry);
        GLEntry.SETRANGE(GLEntry."G/L Account No.", pAcc);
        IF GLEntry.FINDFIRST THEN BEGIN
          text := 'Entry found in account no. ' + pAcc;
          counter1 += 1;
        END;
    END; 
    IF text <> '' THEN
    MESSAGE(text);
    
    LOCAL LoopCompanies(pCompanyFilter : Text[30])
    
    counter_g := 0;
    GL.SETFILTER("No.", '78000|78005|78995');
    Company.SETFILTER(Name, pCompanyFilter);
    IF Company.FINDSET THEN BEGIN
      REPEAT
        GlAccRec.CHANGECOMPANY(Company.Name);
        IF GL.FIND('-') THEN BEGIN
          REPEAT
            IF CheckAccount(GL."No.") = 0 THEN BEGIN
              counter_g += 1;      
              END;
          UNTIL GL.NEXT = 0;
        END;
      UNTIL Company.NEXT = 0;
    END;
    
    IF counter_g = 0 THEN BEGIN
      IF Company.FINDSET THEN BEGIN
        REPEAT
          GlAccRec.CHANGECOMPANY(Company.Name);
          IF GL.FIND('-') THEN BEGIN
            REPEAT
              MESSAGE('Its safe to delete account if you reach this point.');
            UNTIL GL.NEXT = 0;
          END;
        UNTIL Company.NEXT = 0;
      END;
    END;
    
    MESSAGE('Codeunit done.');


  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Code for checking gl in different companies

    Christopher,

    Change your code to look at company even before you call the function CheckAccount,  that is in your main function you need loop through companies and in each company set the filter for specific G/L Account and do the your processing.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans