function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfadm sfdevsfadm sfdev 

Is there a way in Apex how to pass the information between two classes when the code in the second class will be invoked few days later?

I have a custom buttom which invokes my custom method. I need this custom method to save data or information which will be used few days later, for instance after five days. The infromation will be used by another class and method which is invoked by a trigger.

I have tried to implement it the following way but it doesn't work.
In the first class I have created a field wich is initialized in my custom method.
public class UnlockRecordDuringApprovalController {

    private static String userIdToPressUnlock;

    public static String getUserIdToPressUnlock() {
        if(userIdToPressUnlock == null) {
            System.debug('userIdToPressUnlock1 ' + userIdToPressUnlock);
        	return '';
        }
        System.debug('userIdToPressUnlock7 ' + userIdToPressUnlock);
    	return userIdToPressUnlock;
    }
   
    public static PageReference processRecord() {
        ...
        userIdToPressUnlock = UserInfo.getUserId();
    }
}

When the second method is invoked in second class I'm trying to get the value of the variable but when I invoke the getter method from the first class the returned value is NULL.
 
public class OppHandler implements ITrigger {
    @future
    private static void setUnlockOpp() {
        ...
        String userIdToPressUnlock = UnlockRecordDuringApprovalController.getUserIdToPressUnlock();        
         PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = unlockPsId, AssigneeId = userIdToPressUnlock);
         insert psa;
    }
}

Is there a way in Apex how to pass the information or data from the first method from the first class to the method in the second class having in mind the code in the second class will be invoked after five days?

One way that comes to my mind is to store the infomation in a file.

Is that ok and will it work?


 
Best Answer chosen by sfadm sfdev
NagendraNagendra (Salesforce Developers) 
Hi sfadm sfdev,

Class instances are short-lived in Salesforce with values not preserved between transactions/requests. So in general, it is the database is where long-level values need to be placed.

As this will presumably have to work for many different records and many different users, a good place to save the 
userIdToPressUnlock
value is on the record itself. You can add a custom lookup field to User to the SObject type involved and change your logic to update and read that.

Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P