Skip to main content

Notifications

Microsoft Dynamics NAV (Archived)

Modify Record through Change Company

Posted on by 200

Hi,

I have added code to insert customer records when created in company A to company B through change company and transfer fields. Record is getting inserted in company B. Same way I have added code to modify record through change company & transfer fields. But error is thrown when I modify the record in company B which got transferred from company A.

Error shown is

Another User has modified the record for this customer after you retrieved it from the database.

Code

Customer2.CHANGECOMPANY(Companyname);

Customer2.RESET;

Customer2.LOCKTABLE;

Customer2.SETRANGE("No.",Rec."No.");

IF Customer2.ISEMPTY THEN BEGIN

Customer2.INIT;

Customer2.TRANSFERFIELDS(Rec);

ELSE BEGIN

IF NOT Customer2.ISEMPTY THEN BEGIN

Customer2:=Rec;

Customer2.MODIFY;

CusCounter1+=1;

ModArr[CusCounter1]:=Companyname;

END;

END;

 

Customer2.INSERT;

*This post is locked for comments

  • RE: Modify Record through Change Company

    This following Code works for me... Thanks for all your valuable replies.

    Rec.MODIFY;

    Customer2.CHANGECOMPANY(Company);

    Customer2.RESET;

    Customer2.LOCKTABLE;

    Customer2.SETRANGE("No.",Rec."No.");

    IF Customer2.FINDFIRST THEN BEGIN

     Customer2.TRANSFERFIELDS(Rec);

     Customer2.MODIFY;

    END;

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Modify Record through Change Company

    Customer2:=Rec; -> WHY THIS?? use TRANSFERFIELDS

    Customer2.MODIFY;

    send your updated code so we can help further

  • Suggested answer
    4BzSoftware Profile Picture
    4BzSoftware 6,071 on at
    RE: Modify Record through Change Company

    Hi anandb313@gmail.com,

    Try below code:

    Customer2.CHANGECOMPANY(Companyname);

    Customer2.INIT;

    IF Customer2.GET(Rec."No.") THEN BEGIN //Existing customer -> update info

     Customer2.TRANSFERFIELDS(Rec);

     Customer2.MODIFY;

     CusCounter1 += 1;

     ModArr[CusCounter1] := Companyname;

    END

    ELSE BEGIN //Not existing customer -> add new

     Customer2.TRANSFERFIELDS(Rec);

     Customer2.INSERT;

    END;

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Modify Record through Change Company

    Check if this works  (difficult to see your requierements):

    //In the ONINSERT trigger of the Customer table in the company where you create the ITEM

    Customer2.CHANGECOMPANY(CompanyName);

    if Customer2.GET(Rec."No.") then

    begin

     Customer2.TRANSFERFIELDS(Rec);

     Customer2.MODIFY;

    end

    else

    begin

     Customer2.INIT;

     Customer2.TRANSFERFIELDS(Rec);

     Customer2.INSERT;

    end;

  • RE: Modify Record through Change Company

    Thanks for the reply. I get the same error still.

  • RE: Modify Record through Change Company

    on debugging, error is thrown in

    Customer2.MODIFY;

  • RE: Modify Record through Change Company

    Thanks for the reply. No still I get the error when I change the customer record.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Modify Record through Change Company

    In this code seems that the INSERT is executed always, also after the MODIFY. Reorganize your code:

    Customer2.RESET;

    Customer2.LOCKTABLE;

    Customer2.SETRANGE("No.",Rec."No.");

    IF Customer2.ISEMPTY THEN

    BEGIN

    Customer2.INIT;

    Customer2.TRANSFERFIELDS(Rec);

    Customer2.INSERT;

    END

    ELSE

    BEGIN

    Customer2:=Rec;

    Customer2.MODIFY;

    CusCounter1+=1;

    ModArr[CusCounter1]:=Companyname;

    END;

  • Hannes Holst Profile Picture
    Hannes Holst 5,767 on at
    RE: Modify Record through Change Company
    Customer2.CHANGECOMPANY(Companyname);
    
    Customer2.RESET;
    Customer2.LOCKTABLE;
    Customer2.SETRANGE("No.",Rec."No.");
    IF Customer2.ISEMPTY THEN BEGIN
      Customer2.INIT;
      Customer2.TRANSFERFIELDS(Rec);
      Customer2.INSERT;
    ELSE BEGIN
      Customer2.FINDFIRST;
      Customer2:=Rec;
      Customer2.MODIFY;
      CusCounter1+=1;
      ModArr[CusCounter1]:=Companyname;
    END;
    

    Above should work :-)

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Modify Record through Change Company

    Please tell what line in your code is throwing the error

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans