1

I'm new to apex and have been trying to design a trigger that would populate the Account Number field with a value starting at 10000, when the Opportunity Probability moves to 85%. Before I added my 'if' statements, it worked fine, but after that nothing. I even tried writing the trigger on Opportunity and Account objects.

I created a custom object accNumber__c and custom field Ones__c, where the generated number would be stored.

Please, please, i need some help on this one. Here is what I have so far:

  trigger AccountNumberUpdate on Opportunity(before insert, before update) {

      accNumber__c value = new accNumber__c(Name='1',Ones__c=10000);
      for(accNumber__c record:[SELECT Id,Name,Ones__c FROM accNumber__c WHERE Name='1' FOR UPDATE]) {
          value = record;
      }

      for(Opportunity opps:Trigger.new) {
          if((opps.Probability>=85)&&(opps.Account.Region__c=='Americas')&&  (opps.AccountNumber==null)) {
              opps.Account.AccountNumber ='0'.repeat(math.max(0,0String.valueOf(value.Ones__c).length()))+String.valueOf(value.Ones__c);
              value.Ones__c+=1;
          }
      }
      update value;
  }

The Account object is a lookup on the Opportunity object. Is there another way I can approach this problem? Any help is appreciated.

5
  • 1
    I don't understand what you're trying to do with the value variable and the for loop below it... Please explain. May 30, 2014 at 4:26
  • I agree with @Bachovski. What you are doing doesn't make much sense, specifically your for loop where you loop over your accNumber__c and constantly assign a new record to value. What was your intent there? Can you try to explain your use case again because I am not seeing it in your code? May 30, 2014 at 12:14
  • The value variable comes from the separate object (accNumber) that I created that would assign the appropriate number to the Account Object account number field. Since the number being assigned is based on Account region(10000,10001,..for americas & 20000,20001 for International) i thought creating a separate object with to number fields (Ones__c, Twos__c) would be the best way to assign the number. Once a number is assigned, it should not be utilized again.
    – user8556
    May 30, 2014 at 12:17
  • in order for the trigger to fire, all of these 3 conditions must be met. 1)Opportunity probability moves to 85%. (2) Account Number is null (3) depending on the region, assign appropriate number to Account Number field(Americas,International).
    – user8556
    May 30, 2014 at 12:20
  • There are multiple issues here -- 1) as you want to assign a unique ascending number to an Account based on some event occurring to the Oppo, you need to use an after update/insert trigger on Oppo that does a DML statement on the parent Account; 2) you can keep track of the highest number used by querying the extant Accounts; and 3) using semaphores to prevent two triggers from updating diff accounts with the same account number
    – cropredy
    May 30, 2014 at 19:09

0

You must log in to answer this question.

Browse other questions tagged .