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 

Help with Trigger to that will index Account Number field

I have an urgent requirement as follows:

--Whenever the Opportunity Probability is moved to 80%, an Integer should be added to the AccountNumber field on the Account object. There is a lookup relationship between the Opportunity(child) and Account(parent) object.

--The Account number will never be changed by the Trigger; only set once if the AccountNumber field is NULL when the Opportunity Probability gets to 80%.

--The trigger will find the highest number in the appropriate number range and assign a number one higher.

--The number assigned by the trigger will depend on the type of Account(Partner or User) & the region of the Account (Americas or Offshore). as follows:

       If Account = Partner & Region = Americas assign a number between 3000-3999
       If Account = Partner & Region = Offshore assign a number between 9000-9999
       If Account = User & Region = Americas assign a number between 1000-1999
       If Account = User & Region = Offshore assign a number between 8000-8999

Here is what I have so far but i'm stuck with the logic:

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

Any direction will be appreciated
Here-n-nowHere-n-now
That's a project worth of questions.  :-)  I think the forum is more suitable for specific questions about issues, techniques, and features, not full design or solutioning.  That said I have a few suggestions.
  1. You actually need to update account records, not opportunities.  Your trigger seems to be end on an opportunity update, which doesn't seem to be right.
  2. Please make sure in your if condition check to include checking for changed value in probability.  Otherwise your trigger will stamp new account number for any record update once the opportunity reaches 80%.
  3. Your need is to obtain an unique and increasing auto number.  That's not a simple thing to do.  There's concurrency issues to deal with as well as maintaining the numbering state.  If you haven't done that kind of logic that before, I'd suggest using the autonumber field feature in Salesforce.  It's standard and easy to use, but you'll need probably 4 objects to manage the 4 ranges you have.