1

Case Scenario: I need to throw a warning to the user before changing the dates of the standard opportunity. Hence I used Cache.Session to store a variable that checks if the warning has been issued or not. If cache variable is false then warning is issued as Error (addError is the only option I could found in the trigger), otherwise the record is saved when user presses the save button second time. However, this works only when debug mode is enabled. The moment I disable the debug mode for the user, this stops working. Here is the code:

Set<Id> setOppIds = Trigger.newMap.keySet();
if(!Cache.Session.contains('mapOptyDateUpdateCheck')){
    Map<Id, Boolean> mapId = new Map<Id, Boolean>();
    for (Id id : setOppIds) {
        mapId.put(id, false);
    }
    Cache.Session.put('mapOptyDateUpdateCheck', mapId);
}
else{
    Map<Id, Boolean> mapId = (Map<Id, Boolean>)Cache.Session.get('mapOptyDateUpdateCheck');
    for (Id id : setOppIds) {
        if(!mapId.containsKey(id)){
           mapId.put(id, false);
        }
    }            
    Cache.Session.put('mapOptyDateUpdateCheck', mapId);
}
for (Id optyId : setOppIds) {
    if(mapOptyIdDateCheck.containsKey(optyId) && !mapOptyIdDateCheck.get(optyId)){
        mapOptyIdDateCheck.put(optyId, true);
        Cache.Session.put('mapOptyDateUpdateCheck', mapOptyIdDateCheck);
        Opportunity opportunity = Trigger.newMap.get(optyId);
        opportunity.addError(str);
    }
}

Is there any relation between setting logs and maintaining cache? What happens internally when logs are enabled? Also please advice, if there is another way of throwing WARNING message to the user.

| improve this question | | | | |
  • Are you using execute anonymous? – Pranay Jaiswal May 18 '18 at 15:13
  • Yes,. How you are executing this code? Via Api (batch class/future), anonymous window or user interface...oppty edit page? – Ayub May 18 '18 at 18:05
  • This code is executed only from the UI (Opty edit page). – Dev Arya May 19 '18 at 15:08
1

It's a bug; the cache should not be preserved on a non-successful DML operation. addError is meant to completely roll back a transaction on failure (with optional retry for any remaining items). For now, you'll want to vote on this idea. You might also want to log a case to have this fixed. Using addError is always meant to roll back all changes made to the database, including future methods, batchable calls, Queueable calls, etc, anything that would have a side effect if the transaction were rolled back completely.

| improve this answer | | | | |
  • Thanks for the clarification. I understand that but I think sessions should be independent of DML operations. Anyways could you suggest if there is any other work around for this use case?. – Dev Arya May 19 '18 at 15:13
  • @DevArya One main point of a transaction is to be atomic (look up database ACID for more information). That means it must completely process or leave no trace in the system (except for logs). Especially with partial retries, it could mess up the cache anyways (remember, your trigger always runs, not just in the UI). If you want to do this today, you would implement it in Visualforce by overriding the new/edit pages with your own custom code. Do not try to make this behavior work in a trigger. – sfdcfox May 19 '18 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.