+ Start a Discussion
sieb4mesieb4me 

activityhistory and check to see if any record is there, if not, dont allow change of status on case

All,

I am trying to query for activity history and not allow change of status if there is no record for it.

Since activity history is special type, i could not use activityhistory in map since it cannot be queried so had to use

case. however, my code is not working it allows to change status despite there being no activityhistory.

 

what needs to be changed below?

 

thanks

 

 

 

 

trigger Checkcaseactivity on Case (before update) {
     public Set<Id> setCaseId = new Set<Id>();
     public Map<Id,case> mapActivityHisstory=new Map<Id,case>();
    
     for(Case c: Trigger.New){
         if(c.Status !=null && c.Status=='New' && c.Status != trigger.OldMap.get(c.Id).Status){
              setCaseId.add(c.Id);
         }
     }
     if(setCaseId.ISEMPTY()) return;
                     
      For(case ct:  [SELECT (SELECT ActivityDate, Description,id,activitytype from ActivityHistories) FROM case
 WHERE Id  IN : setCaseId]){
            mapActivityHisstory.put(ct.Id,ct);
     }

     for(Case c: Trigger.New){
              if(mapActivityHisstory.get(c.Id) ==null){
                   c.addError('No Activity');
              }
     }
}

Best Answer chosen by Admin (Salesforce Developers) 
Shiv ShankarShiv Shankar

This Code May Help You..

 

trigger CaseBeforeInsertBeforeUpdate on case (after insert, before update){
    Set<Id> caseIds = new Set<Id>();
    List<Case> caseList = new List<Case>();
    for(Case rec : trigger.new ){
        caseIds.add(rec.id);
        caseList.add(rec);
    }
     
     Map<id,Case> idToCase = new Map<id,Case> ([SELECT id, (Select id From Tasks Where Status != 'Completed') FROM Case Where id in : caseIds ]);
     for(Case c : caseList){
         if(idToCase.get(c.id).Tasks.size() != 0 && c.status == 'Closed'){
             c.addError('There is an open task related to this case');
         }
     }

}

 If this is the solution please mark as solution. Thanks

All Answers

sfdcfoxsfdcfox

mapactivityhistory..get(c.id) won't return null, because there is obviously a case in the map. Instead, you need to check this:

 

if(mapactivityhistory.get(c.id).activityhistories==null || mapactivityhistory.get(c.id).activityhistories.isempty()) {

Strictly speaking, activityhistories shouldn't be null, but may be depending on the API version you choose (older versions tend to return a null list, while newer API versions appear to always return a valid object that may simply be empty).

sieb4mesieb4me
it shows this error
Error: Compile Error: Method does not exist or incorrect signature: [LIST<ActivityHistory>].ise​mpty() at line 18 column 48
Shiv ShankarShiv Shankar

This Code May Help You..

 

trigger CaseBeforeInsertBeforeUpdate on case (after insert, before update){
    Set<Id> caseIds = new Set<Id>();
    List<Case> caseList = new List<Case>();
    for(Case rec : trigger.new ){
        caseIds.add(rec.id);
        caseList.add(rec);
    }
     
     Map<id,Case> idToCase = new Map<id,Case> ([SELECT id, (Select id From Tasks Where Status != 'Completed') FROM Case Where id in : caseIds ]);
     for(Case c : caseList){
         if(idToCase.get(c.id).Tasks.size() != 0 && c.status == 'Closed'){
             c.addError('There is an open task related to this case');
         }
     }

}

 If this is the solution please mark as solution. Thanks

This was selected as the best answer
sieb4mesieb4me
=​=null did not work, it still allows status change
Shiv ShankarShiv Shankar

If you want to implement same on event also , just replace case to event...

sieb4mesieb4me
thanks, what are we doing, here ..checking to see if case has any task or not, right? samething i can do for event too?

thanks
sieb4mesieb4me

if(mapActivityHisstory.get(c.id).ActivityHistories.size() == 0) solved the issue and code worked.

Thanks to Shiv Shankar