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
onionbagleonionbagle 

Opportunity Trigger

Hi All,

This is my first attempt at an apex trigger so please bear with me.  I am trying to write a trigger that will update other opportunity records.  The logic is as follows.

Any Opportunity is updated.
Take EVAL_serial_num__c from updated record and search all exsisting opportunity records for matching serial numbers
    If match then update the matching opportunity's field  EVAL_Stage__c = 'Returned';

I have the following code which isn't throwing any errors but at the same time it isn't updating opportunities with matching serial number fields.

Code:
trigger evaluationUpdate on Opportunity (after update) {

 for (Opportunity opp : Trigger.new)
 {
 String serialNum;
 serialNum = opp.EVAL_serial_num__c;
 
 Opportunity[] Opps = [select ID, EVAL_Stage__c from Opportunity where EVAL_serial_num__c = serialNum];
 for (Opportunity o : Opps)
 {
  if(EVAL_Stage__c != 'Returned'){
   o.update(EVAL_Stage__c = 'Returned');
   o.EVAL_Stage__c = 'Returned';
  }
 }
}
}

Any help will be appreciated.
Thanks


JoeK.ax278JoeK.ax278
I've made some changes needed to get the trigger to compile.

Give this version a try:

Code:
trigger evaluationUpdate on Opportunity (after update) {

  for (Opportunity opp : Trigger.new)
  {
    String serialNum;
    serialNum = opp.EVAL_serial_num__c;
 
    Opportunity[] Opps = [select ID, EVAL_Stage__c from Opportunity 
                                   where EVAL_serial_num__c = :serialNum];
    for (Opportunity o : Opps)
    {
      if(o.EVAL_Stage__c != 'Returned'){
        //o.update(EVAL_Stage__c = 'Returned');
        o.EVAL_Stage__c = 'Returned';
        update o;
      }
    }
  }

}

Structuring updates this way is a start, but you'll soon hit the governor limits.   To delay hitting the limits, it is necessary to pull the select and update statements outside of the for loops.

Hope this helps, JoeK

TehNrdTehNrd
I'll help you out, give me a sec to throw something together.
TehNrdTehNrd
I didn't test this but I think it should work.......

Code:
trigger test on Opportunity (before update) {

//Create a unique set of serial numbers, we will use this in our query later
Set<String> serialNumbers = new Set<String>();

//Need to process in bulk, this cycles through each processed record and adds the Serial number to the set
for(Opportunity opp : Trigger.new){
serialNumbers.add(opp.EVAL_serial_num__c);
}

//Define and populate a map of opportunities where it has the serial number in the set and also get
//the field that we are going to want to modify, EVAL_Stage__c
Map<Id,Opportunity> oppSerialMap = new Map<Id,Opportunity>([select EVAL_Stage__c from Opportunity where EVAL_serial_num__c IN :serialNumbers]);

//Go through all the opportunites returned in the query and update the Eval Stage
for(Opportunity opp : oppSerialMap.values()){
if(opp.EVAL_Stage__C != 'Returned'){
opp.EVAL_Stage__c = 'Returned';
}
}

//We have set the values to returned but we need to finalize the change by doing an update
update oppSerialMap.values();
}

The version by JoeK still has the queries inside the for loops which will cause your trigger to break anytime it processes more than 20 records.


Message Edited by TehNrd on 10-01-2007 03:43 PM

onionbagleonionbagle
Thanks guys this really helps.  I was out of the office last week so excuse my late reply. 

-Thanks again!