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
Kon DeleKon Dele 

Opportunity Probability Stage Trigger

Hi,
How can I write a trigger that would assign a value from a predefined range to the Account number field when: (1) the Opportunity stage moves to 80% (2) the Account Number field is NULL. The trigger should find the highest number in the predefined number range and assign a number +1. So far this is what i have:

trigger AssignAccountNumber on Opportunity (after update){
   for (Opportunity p:Trigger.old){
     if (p.Probability == 80){
    //Should I insert code here that specifies Account number field == 'NULL'
   }
}
}

Need some ideas on how I would start this off. Thanks
Best Answer chosen by Kon Dele
ShashForceShashForce
Hi,

I didnt get the pre-defined range part, like where is this range pre-defined? Anyway, here's a sample apart from the pre-defined range:

trigger AssignAccountNumber on Opportunity (after insert, after update){
   list<opportunity> oppsToUpdate = new list<opportunity>();
   for (Opportunity p:Trigger.new){
     if ((p.Probability >= 80)&&(p.accountnumber==null)){
		//your processing for pre-defined range, and update p
		oppsToUpdate.add(p);
     }
   }
   if(oppsToUpdate.size()>0)update oppsToUpdate;
}

Thanks,
Shashank

All Answers

ShashForceShashForce
Hi,

I didnt get the pre-defined range part, like where is this range pre-defined? Anyway, here's a sample apart from the pre-defined range:

trigger AssignAccountNumber on Opportunity (after insert, after update){
   list<opportunity> oppsToUpdate = new list<opportunity>();
   for (Opportunity p:Trigger.new){
     if ((p.Probability >= 80)&&(p.accountnumber==null)){
		//your processing for pre-defined range, and update p
		oppsToUpdate.add(p);
     }
   }
   if(oppsToUpdate.size()>0)update oppsToUpdate;
}

Thanks,
Shashank
This was selected as the best answer
Vinit_KumarVinit_Kumar
I would like to modify the previous code written by Shashank to a bit as the record would be read only in after events.So your updated code would be :-

trigger AssignAccountNumber on Opportunity (before insert, before update){
   for (Opportunity p:Trigger.new){
     if ((p.Probability >= 80)&&(p.accountnumber==null)){
         p.accountnumber = <your date range value>;// put your date range value here
     }
   }


Kon DeleKon Dele
Thank you for the input Shashank and Vinit. Let me try these out.

Just to clarify, the predefined range is defined on the AccountNumber field on the Account object (Account is a lookup on Opportunity) and is to be a number sequence between 10000 and 29999, with the trigger each time assigning the highest number+1 in this range.

Another question of clarification, should it be a before or after trigger.

Thanks,

Kon