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);
}
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);
}
}
}