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
Mathew Andresen 5Mathew Andresen 5 

bulk trigger review

Hi,

I'm just starting to really get a handle on writing code for bulk triggers, did I do this right?  I want to update a field on my opportunity every time a task ray project gets archieved

Thanks,
 
trigger TaskRayProject on TASKRAY__Project__c (before update) {
    set<id> projSet = new set<id>();
    List<Opportunity> oppList = new List<Opportunity>();
    
     // get the taskray projects that have been have just been archieved and add to set 
    for (TASKRAY__Project__c proj :Trigger.new) {
        if(proj.TASKRAY__Status__c == true) {
            
            TASKRAY__Project__c oldProj = trigger.oldmap.get(proj.Id);
            if (oldProj.TASKRAY__Status__c != true) {
                if(proj.Opportunity__c !=NULL) {
                	projSet.add(proj.Opportunity__c);
                }
            }
        }
    }
	
    // query the opportunty for all items in the set
	oppList = [SELECT Id, Name, Task_Ray_Project_Completed__c FROM Opportunity WHERE ID IN :projSet ];
    
    // change field to true
    for(Opportunity opp :oppList) {
        opp.Task_Ray_Project_Completed__c = true;
    }
    
    
     update(oppList);
    
}



 
Best Answer chosen by Mathew Andresen 5
AMIT KAMBOJAMIT KAMBOJ
Hi Mathew,
Code looks good but I did some minor changes. Please have  a look. If you think this is good please mark this as solution.
trigger TaskRayProject on TASKRAY__Project__c (before update) 
{
    Set<Id> projSet = new Set<Id>();
    List<Opportunity> oppList = new List<Opportunity>();
    
     // get the taskray projects that have been have just been archieved and add to set 
    for (TASKRAY__Project__c proj :Trigger.new) 
	{
        if(proj.TASKRAY__Status__c == true) 
		{
            if (trigger.oldmap.get(proj.Id).TASKRAY__Status__c != true && proj.Opportunity__c !=NULL) 
			{
                projSet.add(proj.Opportunity__c);
            }
        }
    }
	
    // query the opportunty for all items in the set
	if(projSet.size() > 0)
		{
			oppList = [SELECT Id, Name, Task_Ray_Project_Completed__c FROM Opportunity WHERE ID IN :projSet];
			if(oppList != null )
				{
					for(Opportunity opp :oppList) 
					{
						opp.Task_Ray_Project_Completed__c = true;
					}
				update(oppList);
				}
		}	
    
}

 

All Answers

AMIT KAMBOJAMIT KAMBOJ
Hi Mathew,
Code looks good but I did some minor changes. Please have  a look. If you think this is good please mark this as solution.
trigger TaskRayProject on TASKRAY__Project__c (before update) 
{
    Set<Id> projSet = new Set<Id>();
    List<Opportunity> oppList = new List<Opportunity>();
    
     // get the taskray projects that have been have just been archieved and add to set 
    for (TASKRAY__Project__c proj :Trigger.new) 
	{
        if(proj.TASKRAY__Status__c == true) 
		{
            if (trigger.oldmap.get(proj.Id).TASKRAY__Status__c != true && proj.Opportunity__c !=NULL) 
			{
                projSet.add(proj.Opportunity__c);
            }
        }
    }
	
    // query the opportunty for all items in the set
	if(projSet.size() > 0)
		{
			oppList = [SELECT Id, Name, Task_Ray_Project_Completed__c FROM Opportunity WHERE ID IN :projSet];
			if(oppList != null )
				{
					for(Opportunity opp :oppList) 
					{
						opp.Task_Ray_Project_Completed__c = true;
					}
				update(oppList);
				}
		}	
    
}

 
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
Thanks for taking a look.