+ Start a Discussion
Matt FieldMatt Field 

trigger for multiple statuses

Hi All,

I am working on a trigger that looks at the status of a project and then updates the StageName of the associated opportunity.  I have it working for a single project status (Project Closed - Live) where it will update the Opportunity StageName to "Implementation Complete", but I need help to figure out how to change the Opportunity StageName to 'Closed - Inactive' if the project status = 'Project Closed - Not Implemented'.  I have attached the trigger below:

trigger UpdateBillDate on Milestone1_Project__c (before insert, before update) {

List<ID> OppIds = New List<ID>();
 
    for(Milestone1_Project__c o : Trigger.new){
        if(o.Projected_Live_Date__c != null && o.Status__c == 'Project Closed - Live'){
            OppIds.add(o.OpportunityID__c);
        }
    }
 
    List<Opportunity> oppList = [SELECT id, Billing_Date__c
                                                        FROM Opportunity
                                                       WHERE id in :OppIds];
    List<Milestone1_Project__c> mproj =[SELECT OpportunityID__c, Status__c, Projected_Live_Date__c
                                                                         FROM Milestone1_Project__c
                                                                        WHERE OpportunityID__c in: OppIds];
    for (Milestone1_Project__c milestone: Trigger.new) {
      
            for(Opportunity opp:oppList){
                    opp.Billing_Date__c = milestone.Projected_Live_Date__c;
                 opp.StageName = 'Implementation Complete';
            }
      }
       
 
    update oppList;
   
}

Any help you can provide will be GREATLY appreciated.

Thanks,

Matt
rohitsfdcrohitsfdc
Hello Matt,
Try the code below

trigger UpdateBillDate on Milestone1_Project__c (before insert, before update) {

List<ID> OppIds = New List<ID>();
list<opportunity> opptoupdate = new list<opportunity>();

map<id,id> projecttooppmap = new map<id,id>();
 
    for(Milestone1_Project__c o : Trigger.new){
        if(o.Projected_Live_Date__c != null && (o.Status__c == 'Project Closed - Live' || o.status__c == 'Project Closed - Not Implemented')){
            OppIds.add(o.OpportunityID__c);
			projecttooppmap.put(o.id,o.OpportunityID__c);
        }
    }
 
    List<Opportunity> oppList = [SELECT id, Billing_Date__c
                                                        FROM Opportunity
                                                       WHERE id in :OppIds];
   // List<Milestone1_Project__c> mproj =[SELECT OpportunityID__c, Status__c, Projected_Live_Date__c
                                                                         FROM Milestone1_Project__c
                                                                        WHERE OpportunityID__c in: OppIds];
    for (Milestone1_Project__c milestone: Trigger.new) {
      
		if(projecttooppmap.get(milestone.id)!=null){
		
			Opportunity opp = new opportunity(id=projecttooppmap.get(milestone.id));
			if(milestone.Status__c == 'Project Closed - Live')
			opp.StageName = 'Implementation Complete';
			else
			opp.StageName = 'Closed - Inactive';
			opptoupdate.add(opp);
		
		}
      }
       
	if(opptoupdate.size()>0)
    update oppList;
   
}




Matt FieldMatt Field
Hi Rohitsfdc,

I tried using the code that you included, but when I tested it the opportunity stage name is not changing.  Any thoughts?  I have included the debug logs below:

Thanks!!!

29.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
09:34:29.082 (82032328)|EXECUTION_STARTED
09:34:29.082 (82063261)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
09:34:29.082 (82093075)|CODE_UNIT_STARTED|[EXTERNAL]|01q40000000PaHT|UpdateBillDate on Milestone1_Project trigger event BeforeUpdate for [a3g190000000B4j]
09:34:29.082 (82944670)|SYSTEM_CONSTRUCTOR_ENTRY|[3]|<init>()
09:34:29.082 (82973363)|SYSTEM_CONSTRUCTOR_EXIT|[3]|<init>()
09:34:29.083 (83077326)|SYSTEM_CONSTRUCTOR_ENTRY|[4]|<init>()
09:34:29.083 (83096236)|SYSTEM_CONSTRUCTOR_EXIT|[4]|<init>()
09:34:29.083 (83191795)|SYSTEM_METHOD_ENTRY|[8]|LIST<Milestone1_Project__c>.iterator()
09:34:29.083 (83307531)|SYSTEM_METHOD_EXIT|[8]|LIST<Milestone1_Project__c>.iterator()
09:34:29.083 (83332894)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
09:34:29.083 (83357753)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
09:34:29.091 (91711867)|SYSTEM_METHOD_ENTRY|[10]|LIST<Id>.add(Object)
09:34:29.091 (91757264)|SYSTEM_METHOD_EXIT|[10]|LIST<Id>.add(Object)
09:34:29.091 (91804264)|SYSTEM_METHOD_ENTRY|[11]|MAP<Id,Id>.put(Object, Object)
09:34:29.091 (91840783)|SYSTEM_METHOD_EXIT|[11]|MAP<Id,Id>.put(Object, Object)
09:34:29.091 (91849140)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
09:34:29.091 (91860054)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
09:34:29.092 (92065019)|SOQL_EXECUTE_BEGIN|[15]|Aggregations:0|select id, Billing_Date__c from Opportunity where id IN :tmpVar1
09:34:29.094 (94481519)|SOQL_EXECUTE_END|[15]|Rows:1
09:34:29.094 (94535649)|SYSTEM_METHOD_ENTRY|[21]|LIST<Milestone1_Project__c>.iterator()
09:34:29.094 (94554715)|SYSTEM_METHOD_EXIT|[21]|LIST<Milestone1_Project__c>.iterator()
09:34:29.094 (94562718)|SYSTEM_METHOD_ENTRY|[21]|system.ListIterator.hasNext()
09:34:29.094 (94572879)|SYSTEM_METHOD_EXIT|[21]|system.ListIterator.hasNext()
09:34:29.094 (94601691)|SYSTEM_METHOD_ENTRY|[23]|MAP<Id,Id>.get(Object)
09:34:29.094 (94621923)|SYSTEM_METHOD_EXIT|[23]|MAP<Id,Id>.get(Object)
09:34:29.094 (94740473)|SYSTEM_METHOD_ENTRY|[25]|MAP<Id,Id>.get(Object)
09:34:29.094 (94760357)|SYSTEM_METHOD_EXIT|[25]|MAP<Id,Id>.get(Object)
09:34:29.094 (94870938)|SYSTEM_METHOD_ENTRY|[30]|LIST<Opportunity>.add(Object)
09:34:29.094 (94891201)|SYSTEM_METHOD_EXIT|[30]|LIST<Opportunity>.add(Object)
09:34:29.094 (94898515)|SYSTEM_METHOD_ENTRY|[21]|system.ListIterator.hasNext()
09:34:29.094 (94908344)|SYSTEM_METHOD_EXIT|[21]|system.ListIterator.hasNext()
09:34:29.094 (94920079)|SYSTEM_METHOD_ENTRY|[35]|LIST<Opportunity>.size()
09:34:29.094 (94932811)|SYSTEM_METHOD_EXIT|[35]|LIST<Opportunity>.size()
09:34:29.094 (94975211)|DML_BEGIN|[36]|Op:Update|Type:Opportunity|Rows:1

09:34:29.849 (849088819)|CODE_UNIT_FINISHED|UpdateBillDate on Milestone1_Project trigger event BeforeUpdate for [a3g190000000B4j]
09:34:29.851 (851966103)|CODE_UNIT_STARTED|[EXTERNAL]|01q40000000PYff|Milestone1_Project_Trigger on Milestone1_Project trigger event BeforeUpdate for [a3g190000000B4j]
09:34:29.858 (858944656)|METHOD_ENTRY|[29]|01p40000000MX2R|Milestone1_Project_Trigger_Utility.Milestone1_Project_Trigger_Utility()
09:34:29.858 (858958394)|SYSTEM_MODE_ENTER|false
09:34:29.858 (858964934)|SYSTEM_MODE_EXIT|false
09:34:29.858 (858973612)|METHOD_EXIT|[29]|Milestone1_Project_Trigger_Utility
09:34:29.859 (859999763)|METHOD_ENTRY|[5]|01p40000000MX2R|Milestone1_Project_Trigger_Utility.handleProjectUpdateTrigger(LIST<Milestone1_Project__c>)
09:34:29.860 (860035077)|SYSTEM_MODE_ENTER|false
09:34:29.860 (860116735)|SYSTEM_METHOD_ENTRY|[41]|LIST<Milestone1_Project__c>.iterator()
09:34:29.860 (860154244)|SYSTEM_METHOD_EXIT|[41]|LIST<Milestone1_Project__c>.iterator()
09:34:29.860 (860166214)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
09:34:29.860 (860182268)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
09:34:29.860 (860270539)|SYSTEM_METHOD_ENTRY|[42]|String.valueOf(Object)
09:34:29.860 (860300180)|SYSTEM_METHOD_EXIT|[42]|String.valueOf(Object)
09:34:29.860 (860323086)|SYSTEM_METHOD_ENTRY|[42]|System.debug(ANY)
09:34:29.860 (860332338)|USER_DEBUG|[42]|DEBUG|*** Project "batch Deposit Chek - SACT 3.0" with Id a3g190000000B4jAAE begin trigger
09:34:29.860 (860337743)|SYSTEM_METHOD_EXIT|[42]|System.debug(ANY)
09:34:29.860 (860369229)|SYSTEM_METHOD_ENTRY|[43]|MAP<Id,Milestone1_Project__c>.put(Object, Object)
09:34:29.860 (860393626)|SYSTEM_METHOD_EXIT|[43]|MAP<Id,Milestone1_Project__c>.put(Object, Object)
09:34:29.860 (860768433)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
09:34:29.860 (860780776)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
09:34:29.860 (860792176)|SYSTEM_METHOD_ENTRY|[64]|MAP<Id,Milestone1_Project__c>.keySet()
09:34:29.860 (860835430)|SYSTEM_METHOD_EXIT|[64]|MAP<Id,Milestone1_Project__c>.keySet()
09:34:29.860 (860852714)|SYSTEM_METHOD_ENTRY|[64]|String.valueOf(Object)
09:34:29.860 (860872673)|SYSTEM_METHOD_EXIT|[64]|String.valueOf(Object)
09:34:29.860 (860885104)|SYSTEM_METHOD_ENTRY|[64]|System.debug(ANY)
09:34:29.860 (860890222)|USER_DEBUG|[64]|DEBUG|*** project key set: {a3g190000000B4jAAE}
09:34:29.860 (860894519)|SYSTEM_METHOD_EXIT|[64]|System.debug(ANY)
09:34:29.860 (860909687)|SYSTEM_METHOD_ENTRY|[66]|MAP<Id,Milestone1_Project__c>.keySet()
09:34:29.860 (860939101)|SYSTEM_METHOD_EXIT|[66]|MAP<Id,Milestone1_Project__c>.keySet()
09:34:29.863 (863064593)|SOQL_EXECUTE_BEGIN|[66]|Aggregations:0|select Id, Name, Project__c, Parent_Milestone__c, Complete__c, Deadline__c, Total_Actual_Hours__c, Total_Estimated_Hours__c, Total_Actual_Expense__c, Total_Estimated_Expense__c, Total_Hours_Budget__c, Total_Expense_Budget__c, Total_Complete_Tasks__c, Total_Open_Tasks__c, Total_Late_Tasks__c, Total_Blocked_Tasks__c from Milestone1_Milestone__c where Project__c = :tmpVar1
09:34:29.878 (878165549)|SOQL_EXECUTE_END|[66]|Rows:0
09:34:29.878 (878301568)|SYSTEM_METHOD_ENTRY|[85]|LIST<Milestone1_Milestone__c>.size()
09:34:29.878 (878328989)|SYSTEM_METHOD_EXIT|[85]|LIST<Milestone1_Milestone__c>.size()
09:34:29.878 (878347507)|SYSTEM_METHOD_ENTRY|[85]|String.valueOf(Object)
09:34:29.878 (878361017)|SYSTEM_METHOD_EXIT|[85]|String.valueOf(Object)
09:34:29.878 (878375484)|SYSTEM_METHOD_ENTRY|[85]|System.debug(ANY)
09:34:29.878 (878381305)|USER_DEBUG|[85]|DEBUG|*** milestones queried for projects: 0
09:34:29.878 (878385983)|SYSTEM_METHOD_EXIT|[85]|System.debug(ANY)
09:34:29.878 (878393775)|SYSTEM_METHOD_ENTRY|[87]|LIST<Milestone1_Milestone__c>.iterator()
09:34:29.878 (878497738)|SYSTEM_METHOD_EXIT|[87]|LIST<Milestone1_Milestone__c>.iterator()
09:34:29.878 (878521717)|SYSTEM_METHOD_ENTRY|[87]|system.ListIterator.hasNext()
09:34:29.878 (878534876)|SYSTEM_METHOD_EXIT|[87]|system.ListIterator.hasNext()
09:34:29.878 (878542993)|SYSTEM_MODE_EXIT|false
09:34:29.878 (878552365)|METHOD_EXIT|[5]|01p40000000MX2R|Milestone1_Project_Trigger_Utility.handleProjectUpdateTrigger(LIST<Milestone1_Project__c>)
09:34:29.846 (878565512)|CUMULATIVE_LIMIT_USAGE
09:34:29.846|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 16 out of 100
  Number of query rows: 46 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 232 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 10
  Number of Mobile Apex push calls: 0 out of 10
rohitsfdcrohitsfdc

Hello Matt,
Code looks fine to me.
 

Adding few debug statements, please execute and share the debug log

 

trigger UpdateBillDate on Milestone1_Project__c (before insert, before update) {

List<ID> OppIds = New List<ID>();
list<opportunity> opptoupdate = new list<opportunity>();

map<id,id> projecttooppmap = new map<id,id>();
 
    for(Milestone1_Project__c o : Trigger.new){
        if(o.Projected_Live_Date__c != null && (o.Status__c == 'Project Closed - Live' || o.status__c == 'Project Closed - Not Implemented')){
            OppIds.add(o.OpportunityID__c);
			projecttooppmap.put(o.id,o.OpportunityID__c);
        }
    }
 
    List<Opportunity> oppList = [SELECT id, Billing_Date__c
                                                        FROM Opportunity
                                                       WHERE id in :OppIds];
   // List<Milestone1_Project__c> mproj =[SELECT OpportunityID__c, Status__c, Projected_Live_Date__c
                                                                         FROM Milestone1_Project__c
                                                                        WHERE OpportunityID__c in: OppIds];
    for (Milestone1_Project__c milestone: Trigger.new) {
      
		if(projecttooppmap.get(milestone.id)!=null){
		
			Opportunity opp = new opportunity(id=projecttooppmap.get(milestone.id));
system.debug('Rohit1-->'+milestone.Status__c);
			if(milestone.Status__c == 'Project Closed - Live')
			opp.StageName = 'Implementation Complete';
			else
			opp.StageName = 'Closed - Inactive';

system.debug('Rohit2-->'+opp.StageName);
			opptoupdate.add(opp);
		
		}
      }
       
	if(opptoupdate.size()>0)
    update oppList;
   
}