Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Save / reach variable data from other classes.

(0) ShareShare
ReportReport
Posted on by 11
This is surely really trivial and I am embarrassed to ask, but I must be missing something and just cant get it to work (new to x++).
 
In class A I read some data from a external source. I want to use this data in class B, so I try to save the variable in the class declaration in class A.
 
However class A then makes a method call to class C (I follow it in the debugger) and when it later jumps back to class A the saved variable is gone (0). So when A then makes the call to class B where I want the variable to be sent along, it is no longer the value that was initially read in.
 
The debugger goes something like this:
 
Class A (read variable) > class C > class A (variable value gone) > class B (cant find the initial read value).  
 
How can I reach or send the data read by class A in the class of B (when it has to go to class C before class B)?
  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    Save / reach variable data from other classes.
    Well, if you're unable to tell us what your code does, then there is no way to tell how it should be fixed. But it sounds that you need to add one piece of information to class B - either an invoice ID or a reference to class A.
     
    Forget pack/unpack if you have no intention to serialize object.
     
    I'll move the thread to the Finance forum.
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Save / reach variable data from other classes.
    I think it's better to go with creating intermediate tables for storage of buffer values instead of pack/unpack method, since you don't have any connection within Class A and Class B.
     
    Thanks,
    Girish S.
  • Community member Profile Picture
    Community member 11 on at
    Save / reach variable data from other classes.
    I am not sure I understand how to use the pack/unpack methods with contrainer.
     
    So I added the two methods in class A. 
     
    container pack() {
    return [recId];
    }
     
    boolean unpack(container _packedValues) {
    [recId] = _packedValues;
    return true;
    }
     
    After I read the data I want to save in class A, how would I save it?
     
    It looks backward to me,
    pack() (witch I thought should pack/save a value) returns it? And unpack() (which I thought should retrieve it) requires I send something in and then it just return a boolean. 
  • GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Save / reach variable data from other classes.
    If there is no relation between Class A and Class B, then my suggestion will be creating intermediate table and insert the record into that table. So, while inserting delete the existing record and re-insert them - This should be done everytime whenever new record is inserted into the table.
    In class B you can add a select statement to get the buffer values. It will be time consuming process as it required database call to insert and delete the record, but this is the one solution.
    Another solution will be using the pack and unpack method as I mentioned earlier.
    Since you confirm this is related to D365 FO - I am moving this thread to Finance forum.
     
    Thanks,
    Girish S.
  • Community member Profile Picture
    Community member 11 on at
    Save / reach variable data from other classes.
    Hello Martin.
     
    It is D365 FO, so come to think of it, did I post in the wrong forum? If so maybe I should delete and post is D365 instead.
     
    Its hard to show the code since it is part of a huge system, I am trying to fix a bug in it.
     
    But basically class A reads an invoice from an external source, then a lot of classes/methods gets called (following them all in the debugger take a long time) finally when class B is called I need to use the data that was originally read, or rather I need the invoice id so I can find the specific invoice from the source. 
     
    Instances was my first thought too, the class A that reads the data might be a different instance of the class than that which calls class B. This is why I wanted to save the data somehow to be reached later, is that possible? All the data I need is in the database, I just need the invoice id to find the correct invoice to lookup in the database. 

    @GirishS

    I tried to use containers, but did not get it to work. 

    After the data is read I put it in a container:

    container testC; //Declared at start of class.

    testC = [_recid];  //Inside the metod

    But when the call to class B is made the container is empty. 
  • Martin Dráb Profile Picture
    Martin Dráb 230,836 Most Valuable Professional on at
    Save / reach variable data from other classes.
    Can you show us your code? It's not clear what you mean by "jumping back to a class".
     
    Are you sure that you're still working with the same instance of class A? The data is associated with an instance (object), not the class itself (unless we talk about static members, but let's ignore them in this case).
     
    GirishS's reply is related to special case when serialization is involved - you want to deserialize an object state and then load the same data to a different object. It's not clear whether you have something like that in your code.

    By the way, which version of AX is it about?
  • Suggested answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Save / reach variable data from other classes.
    Hi,
     
    Have you considered using pack/unpack method in Class A.
     
    Thanks,
    Girish S.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans