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
Guru sfdcGuru sfdc 

Before update help

Hi Team,
 
We have one requirement like we have 5 conditions. Any one condition is met then check box should be update. For that i have written one trigger on opportunity using before event
 
The problem is: In scorecard object when matching any criteria then in the opportunity "Deal” should be true. Which is working fine my question is after matching any criteria in scorecard object in the opportunity need to updated deal = check when refreshing the opty page. Here we are updating the field every time which is not correct
Can anyone please help me on this?
Thanks
 
Ariel GorfinkelAriel Gorfinkel
Try checking if changed (old value != new value).
Something like
((Opportunity)Trigger.oldMap.get(opportunity.Id)).deal != opportunity.deal
(or any other criteria / field you want to check if changed).

 
Guru sfdcGuru sfdc
Hi Ariel Gorfinkel,

Thanks for your reply.Can you please guide me where i need to change exactly in my below trigger:

trigger BIGDEAL on Opportunity (Before update)
{
    set<Id> oppIds = new set<Id>();
    set<id> uids = new set<id>();
    //set<Id> accIdList = new set<Id>();
    List<Opportunity> OppList = new List<Opportunity>();
    Map<Id,List<Opportunitysplit>> oppMap = new Map<Id,List<Opportunitysplit>>();
    //OPTY TEAM Start
    Map<Id,List<OpportunityTeamMember>> oppTeam = new Map<Id,List<OpportunityTeamMember>>();
    //OPTY TEAM END
   // Map<Id,List<partner>> opppartMap = new Map<Id,List<partner>>();
    Map<Id,List<Opportunity_Splits__c>> opppartnerorderMap = new Map<Id,List<Opportunity_Splits__c>>();
    Map<Id,Sales_Scorecard__c> sscMap = new Map<Id,Sales_Scorecard__c>();
    List<Sales_Scorecard__c> ssList = new List<Sales_Scorecard__c>();

    for(Opportunitysplit ops :[Select id,SplitAmount,SplitPercentage,Role_Name__c,Opportunityid,SplitOwnerid from Opportunitysplit Where Opportunityid IN: trigger.newmap.keyset()])
    {
        uids.add(ops.SplitOwnerid);
        if(oppMap.containskey(ops.Opportunityid))
        oppmap.get(ops.Opportunityid).add(ops);
        else{
            List<Opportunitysplit> opslist = new List<Opportunitysplit>();
            opslist.add(ops);
            oppmap.put(ops.Opportunityid,opslist);
        }
    }
    
    List<Sales_Scorecard__c> ssclist =[select id, name,User_Name__c,Manager__c,Quota__c,Manager_Quota__c,User_Role__c from Sales_Scorecard__c where User_Name__c IN :uids];
   // Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('GEIP-Renewal').getRecordTypeId();
    for(Sales_Scorecard__c ssc : ssclist){
        sscMap.put(ssc.User_Name__c,ssc);
    }   
    
    List<Opportunity_Splits__c> PODS = [SELECT Amount__c,Id,Name,Opportunity__c,Split_Percentage__c,Opportunity_Splits__c.Account__r.name FROM Opportunity_Splits__c where Opportunity__c IN:trigger.newmap.keyset()];
    for(Opportunity_Splits__c POS: PODS ){
        // accIdList.add(POS.Account__c);
        if(opppartnerorderMap.containskey(POS.Opportunity__c))
        opppartnerorderMap.get(POS.Opportunity__c).add(POS);
        else{
            List<Opportunity_Splits__c> podslist = new List<Opportunity_Splits__c>();
            podslist.add(POS);
            // podslist.add(POS);
            opppartnerorderMap.put(POS.Opportunity__c,podslist);
        }
    }  
    

    Map<Id,integer> OppTeamMapChannelRoles= New Map<id,integer>();
    
    Map<Id,integer> OppTeamMapAccountRoles= New Map<id,integer>();
    
    AggregateResult[] teamOppChannelCount =[SELECT OpportunityId,Count(id)FROM OpportunityTeamMember Where 
    Opportunityid IN: oppMap.keyset() and TeamMemberRole ='SW Channel Manager' GROUP BY OpportunityId];
    
    AggregateResult[] teamOppAccountCount =[SELECT OpportunityId,Count(id)FROM OpportunityTeamMember Where 
    Opportunityid IN: oppMap.keyset() and TeamMemberRole ='SW Account Manager' GROUP BY OpportunityId];
 

    for(AggregateResult ar : teamOppChannelCount){
        OppTeamMapChannelRoles.put((id)ar.get('OpportunityId'),(Integer)ar.get('expr0'));
    }
    
    for(AggregateResult ar : teamOppAccountCount){
        OppTeamMapAccountRoles.put((id)ar.get('OpportunityId'),(Integer)ar.get('expr0'));
    }
    

    for(opportunity opp : trigger.new)
    {   
        system.debug('testing opp'+opp);
        if(oppMap.get(opp.id)!=null)
        {
            System.debug('*******--***'+opp);
            for(Opportunitysplit opss : oppMap.get(opp.id))
            { 
                if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') 
                //&& opp.RecordTypeId== devRecordTypeId 
                && sscmap!=null 
                && sscmap.get(opss.SplitOwnerid)!=null              
                && ((sscMap.get(opss.SplitOwnerid).Quota__c!=null
                && (sscMap.get(opss.SplitOwnerid).Quota__c/2 <= opss.SplitAmount))
                || (sscMap.get(opss.SplitOwnerid).Manager_Quota__c!=null 
                &&  opss.SplitAmount >=sscMap.get(opss.SplitOwnerid).Manager_Quota__c*25/100)))
                {   system.debug('testing opp'+opss );
                    opp.Big_Deal__c=true;
                    break;
                }
                
                 else if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') &&                             
                        (OppTeamMapChannelRoles.get(opp.id) > 1 || OppTeamMapAccountRoles.get(opp.id) > 1 )){
                        opp.Big_Deal__c=true;
                    } 
                    else if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') && opppartnerorderMap!=null && opppartnerorderMap.get(opp.id)!=null && opppartnerorderMap.containsKey(opp.id))
                    {              
                        for(Opportunity_Splits__c POS : opppartnerorderMap.get(opp.id)) 
                        if(POS.Account__c!=null && POS.Account__r.name.contains('GE Intelligent platforms')&&(date.valueOf(opp.createdDate).daysBetween(date.valueOf(opp.CloseDate))<=30))
                        {opp.Big_Deal__c=true;
                            break;
                        }
                        else if(POS.Account__c!=null && !POS.Account__r.name.contains('GE Intelligent platforms')&&(date.valueOf(opp.createdDate).daysBetween(date.valueOf(opp.CloseDate))<=45))
                        opp.Big_Deal__c=true;
                        else{
                            system.debug('testing opp');
                            opp.Big_Deal__c=false;
                        }
                    }             
                        
                    else{
                        system.debug('testing opp');
                        opp.Big_Deal__c=false;
                    }
               
            }
        }
    }
}

Please help me 

Thanks
Ariel GorfinkelAriel Gorfinkel
I modified your code a bit.
trigger BIGDEAL on Opportunity (Before update)
{
    set<Id> oppIds = new set<Id>();
    set<id> uids = new set<id>();
    //set<Id> accIdList = new set<Id>();
    List<Opportunity> OppList = new List<Opportunity>();
    Map<Id,List<Opportunitysplit>> oppMap = new Map<Id,List<Opportunitysplit>>();
    //OPTY TEAM Start
    Map<Id,List<OpportunityTeamMember>> oppTeam = new Map<Id,List<OpportunityTeamMember>>();
    //OPTY TEAM END
   // Map<Id,List<partner>> opppartMap = new Map<Id,List<partner>>();
    Map<Id,List<Opportunity_Splits__c>> opppartnerorderMap = new Map<Id,List<Opportunity_Splits__c>>();
    Map<Id,Sales_Scorecard__c> sscMap = new Map<Id,Sales_Scorecard__c>();
    List<Sales_Scorecard__c> ssList = new List<Sales_Scorecard__c>();

    for(Opportunitysplit ops :[Select id,SplitAmount,SplitPercentage,Role_Name__c,Opportunityid,SplitOwnerid from Opportunitysplit Where Opportunityid IN: trigger.newmap.keyset()])
    {
        uids.add(ops.SplitOwnerid);
        if(oppMap.containskey(ops.Opportunityid))
        oppmap.get(ops.Opportunityid).add(ops);
        else{
            List<Opportunitysplit> opslist = new List<Opportunitysplit>();
            opslist.add(ops);
            oppmap.put(ops.Opportunityid,opslist);
        }
    }
    
    List<Sales_Scorecard__c> ssclist =[select id, name,User_Name__c,Manager__c,Quota__c,Manager_Quota__c,User_Role__c from Sales_Scorecard__c where User_Name__c IN :uids];
   // Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('GEIP-Renewal').getRecordTypeId();
    for(Sales_Scorecard__c ssc : ssclist){
        sscMap.put(ssc.User_Name__c,ssc);
    }   
    
    List<Opportunity_Splits__c> PODS = [SELECT Amount__c,Id,Name,Opportunity__c,Split_Percentage__c,Opportunity_Splits__c.Account__r.name FROM Opportunity_Splits__c where Opportunity__c IN:trigger.newmap.keyset()];
    for(Opportunity_Splits__c POS: PODS ){
        // accIdList.add(POS.Account__c);
        if(opppartnerorderMap.containskey(POS.Opportunity__c))
        opppartnerorderMap.get(POS.Opportunity__c).add(POS);
        else{
            List<Opportunity_Splits__c> podslist = new List<Opportunity_Splits__c>();
            podslist.add(POS);
            // podslist.add(POS);
            opppartnerorderMap.put(POS.Opportunity__c,podslist);
        }
    }  
    

    Map<Id,integer> OppTeamMapChannelRoles= New Map<id,integer>();
    
    Map<Id,integer> OppTeamMapAccountRoles= New Map<id,integer>();
    
    AggregateResult[] teamOppChannelCount =[SELECT OpportunityId,Count(id)FROM OpportunityTeamMember Where 
    Opportunityid IN: oppMap.keyset() and TeamMemberRole ='SW Channel Manager' GROUP BY OpportunityId];
    
    AggregateResult[] teamOppAccountCount =[SELECT OpportunityId,Count(id)FROM OpportunityTeamMember Where 
    Opportunityid IN: oppMap.keyset() and TeamMemberRole ='SW Account Manager' GROUP BY OpportunityId];
 

    for(AggregateResult ar : teamOppChannelCount){
        OppTeamMapChannelRoles.put((id)ar.get('OpportunityId'),(Integer)ar.get('expr0'));
    }
    
    for(AggregateResult ar : teamOppAccountCount){
        OppTeamMapAccountRoles.put((id)ar.get('OpportunityId'),(Integer)ar.get('expr0'));
    }
    

    for(opportunity opp : trigger.new)
    {   
        system.debug('testing opp'+opp);
        if(oppMap.get(opp.id)!=null)
        {
            System.debug('*******--***'+opp);
            for(Opportunitysplit opss : oppMap.get(opp.id))
            { 
                if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') 
                //&& opp.RecordTypeId== devRecordTypeId 
                && sscmap!=null 
                && sscmap.get(opss.SplitOwnerid)!=null              
                && ((sscMap.get(opss.SplitOwnerid).Quota__c!=null
                && (sscMap.get(opss.SplitOwnerid).Quota__c/2 <= opss.SplitAmount))
                || (sscMap.get(opss.SplitOwnerid).Manager_Quota__c!=null 
                &&  opss.SplitAmount >=sscMap.get(opss.SplitOwnerid).Manager_Quota__c*25/100)))
                {   system.debug('testing opp'+opss );
					//Checking if an update is required.
					if (opp.Big_Deal__c != true){ //might suffice for what you need, but if not, try the following line
                    //if(((Opportunity)Trigger.oldMap.get(opportunity.Id)).Big_Deal__c != true){
						opp.Big_Deal__c=true;
					}
					break;
                }
                
                 else if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') &&                             
                        (OppTeamMapChannelRoles.get(opp.id) > 1 || OppTeamMapAccountRoles.get(opp.id) > 1 )){
                        if (opp.Big_Deal__c != true)//Checking if an update is required.
							opp.Big_Deal__c = true;
                    } 
                    else if((opp.ForecastCategoryName=='Best Case' || opp.ForecastCategoryName=='Commit') && opppartnerorderMap!=null && opppartnerorderMap.get(opp.id)!=null && opppartnerorderMap.containsKey(opp.id))
                    {              
                        for(Opportunity_Splits__c POS : opppartnerorderMap.get(opp.id)) 
                        if(POS.Account__c!=null && POS.Account__r.name.contains('GE Intelligent platforms')&&(date.valueOf(opp.createdDate).daysBetween(date.valueOf(opp.CloseDate))<=30))
                        {
							if (opp.Big_Deal__c != true){//Checking if an update is required.
								opp.Big_Deal__c=true;
                            }
							break;
                        }
                        else if(POS.Account__c!=null && !POS.Account__r.name.contains('GE Intelligent platforms')&&(date.valueOf(opp.createdDate).daysBetween(date.valueOf(opp.CloseDate))<=45))
                        opp.Big_Deal__c=true;
                        else{
                            system.debug('testing opp');
                            if (opp.Big_Deal__c != false){//if true or null
								opp.Big_Deal__c=false;
                            }
                        }
                    }             
                        
                    else{
                        system.debug('testing opp');
						if (opp.Big_Deal__c != false){//if true or null
							opp.Big_Deal__c=false;
						}
                    }
               
            }
        }
    }
}
Added checks to see if the value requires updating (in bold).
However, I may have not fully understood your question.


 
Guru sfdcGuru sfdc
Hi Ariel Gorfinkel,

Thank you, I understand the logic which is mentioned above. But my problem is
Let say: I have two scenarios from Scorecard object. Any one condition is met in the score card i want to update Opportunity field (Checkbox) Big deal = True. When we are refreshed the same Opty page which means without editing any field from opportunity. The above my trigger is working fine when editing any field from opportunity. But i don't want editing any field from Oppty  it should be update when we refreshed the page . I hope clear now. Please help me for this.
 
 
Thanks
 
Ariel GorfinkelAriel Gorfinkel
If I understand you correctly, then maybe you should have a scorecard trigger instead of the opportunity one.
Use "on after update". It will update the related opportunity whenever a scorecard changes (and the changes meet the criteria).
Guru sfdcGuru sfdc
Hi Ariel Gorfinkel,

Can you please help me to create afterupdate trigger on scorecard object.That would be great help for us

Thanks