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
HelloSanHelloSan 

when Opportunity Stage is changed from Preprospect to any other stage i want one record to be mandatory in activity history either log a call or sendemail how can this be done using apex coding

Keyur  ModiKeyur Modi
Hi,
You want to put validation in your opprotunity level. Please try this code
trigger oneActivityMandatory on Opportunity (before update){
set<id> oppId=new set<id>();
map<id,id> mapActivityOpp= new map<id,id>();
List<ActivityHistory>lstActivity =new List<ActivityHistory>();

for(opportunity each:Trigger.New){
  if(each.Trigger.Oldmap.get(each.stage)=='Preprosepect'  &&    each.stage!='Preprosepect'){
      oppId.add(each.id);
   }

}

lstActivity =[SELECT id,name,whoId FROM ActivityHistory WHERE whoId IN:oppId];

for(ActivityHistory  each:lstActivity){
mapActivityOpp.put(each.WhoId,id);
}

for(opportunity eachOpp:Trigger.New){
      IF(mapActivityOpp.get(eachOpp)==null){
            eachOpp.addError('Atleast one Activity mandatory');
       }
}

}
please let me know if this will help.

Thanks,
Keyur Modi
 
HelloSanHelloSan
Thanks Keyur for your response. Giving error message entity type ActivityHistory does not support query