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
MaggieSumitMaggieSumit 

I am trying to Update Opportunity Based on Event Field. But getting issue to validation two condition at same time

Hi, I have written helper class for updating opportunity. if Event Status = 'Cancelled' then I am updating opportunity Stage Field else if Event Type = 'Consultation'.  then I updating Opportunity Date field. I am trying to update both logic if both matching matched.

This the class I have written.
  1. public class EventHandler{
  2.     
  3.     public static void OpportunityStageUpdateBaseOnEventStatus (List<Event> evnList){
  4.         Boolean condition1=false;
  5.         Boolean condition2=false;
  6.         Map<Id,Event> mapID = new Map<Id,Event>();
  7.         Set<Opportunity> OppSet = new Set<Opportunity>();
  8.         for(Event evn: evnList){
  9.             mapID.put(evn.Id,evn);
  10.         }
  11.         System.debug('mapID'+mapID);
  12.         for(Event evn: mapID.values()){
  13.             if(evn.Type__c == 'Consultation'){
  14.                 System.debug('1');
  15.                 condition1=true;
  16.                 Opportunity opp = new Opportunity();
  17.                 opp.Id = evn.WhatId;
  18.                 Opp.Demo_Date_Time__c = evn.StartDateTime;
  19.                 OppSet.add(opp);                
  20.             }else if(evn.Status__c == 'cancelled'){
  21.                 System.debug('2');
  22.                 condition2=true;
  23.                 Opportunity opp = new Opportunity();
  24.                 opp.Id = evn.WhatId;
  25.                 Opp.StageName = 'Closed Lost';
  26.                 OppSet.add(opp);
  27.             }else if(condition1 ==true && condition2 ==true){
  28.                 System.debug('3');
  29.                 Opportunity opp = new Opportunity();
  30.                 opp.Id = evn.WhatId;
  31.                 Opp.Demo_Date_Time__c = evn.StartDateTime;
  32.                 Opp.StageName = 'Closed Lost';
  33.                 OppSet.add(opp);
  34.             }         
  35.         }
  36.         
  37.         if(OppSet.Size()>0){
  38.             System.debug('SIZE >>'+OppSet.Size());
  39.             update new List<Opportunity>(OppSet);
  40.             System.debug('SIZE >>'+OppSet);
  41.         }
  42.         
  43.                 
  44.     }
  45.     
  46. }
Prithviraj_ChavanPrithviraj_Chavan
Hi,
can you please make me understand what do you mean by :-
I am trying to update both logic if both matching matched what do you mean by this?
MaggieSumitMaggieSumit
Hi @Prithviraj_Chavan

Like if Event Status = 'Cancelled'  and Event Type = 'Consultation' both field are updated at the same time then I want to update these two Demo_Date_Time__c and StageName, and if Event Status = 'Cancelled' updated only then update Demo_Date_Time__c  only. else
if Event Type = 'Consultation' updated only then update StageName.
Prithviraj_ChavanPrithviraj_Chavan
Hi,
Opportunity opp = new Opportunity();
if( evn.Status__c == 'cancelled' && evn.Type__c == 'Consultation' ){
	opp.Id = evn.WhatId;
    Opp.Demo_Date_Time__c = evn.StartDateTime;
    Opp.StageName = 'Closed Lost';
	OppSet.add(opp);
}else if(evn.Type__c == 'Consultation'){
	opp.Id = evn.WhatId;
	Opp.Demo_Date_Time__c = evn.StartDateTime;
	OppSet.add(opp);
}else if(evn.Status__c == 'cancelled'){
	opp.Id = evn.WhatId;
	opp.stage = 'Closed Lost';
	OppSet.add(opp);
}
check if aboves code works.. replace your forloop body with given code
 
MaggieSumitMaggieSumit
Is there any way to use this logic as a recursive way. Like If child field is updated then parent field updated. If Parent Update then the child will be updated.
Prithviraj_ChavanPrithviraj_Chavan
Hi Sumit,
Write Trigger on parent as well as on child object and perform operations from there..