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 challenging Account Number Trigger Requirement

Greetings,
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=0);
                    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,0-String.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.

Best Answer chosen by Kon Dele
Vinit_KumarVinit_Kumar
Kon,

My bsad,I forgot to update the Account.Try below code,this should work :-

trigger updateAccountNumber11 on Opportunity(before insert,before update)
{
    List<Id> accIds = new List<Id>();
    
    for(Opportunity opp:trigger.new)
    {
        if(opp.AccountId!=null)
        {
            accIds.add(opp.AccountId);
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
    
    for(Opportunity opp :trigger.new)
    {
        if(!accMap.IsEmpty())
        {
        if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
        {
            if(accMap.get(opp.AccountId).Region__c=='Americas')
            {
                accMap.get(opp.AccountId).AccountNumber = '10000';
            }
            else if(accMap.get(opp.AccountId).Region__c=='International')
            {
                accMap.get(opp.AccountId).AccountNumber = '20000';
            }
        }
        else if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber!=null))
        {
            if(accMap.get(opp.AccountId).Region__c=='Americas')
            {
                accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
            }
            else if(accMap.get(opp.AccountId).Region__c=='International')
            {
                accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
            }
        }
        else
        {
        
        }
        
        }
    }
    
    update accMap.values();
    
}

If this helps,please mark it as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
Your requirement is still not clear ??Could you explain what do you want to achieve.Is it you want to update the Opportunity field or Account field and what is the pupose of you custom object here.

Try to elaborate in detail,then we can help you !!
Kon DeleKon Dele
I want to update the Account field(Account Number) which is a standard text field, but the trigger should only fire when Opportunity Probability reaches 85%. The purpose of my custom object was to assign a number sequence from 10000, 10001,10002,...based on the region (which is a custom field on the Account object). 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(10000,10001...if Americas, & 20000,20001..if International). The idea of the custom number object was just from another post on here. Thanks
Vinit_KumarVinit_Kumar
Kon,

You don't need custom object,just a Trigger on Opportunity would work.Try below code :-

trigger updateAccountNumber on Opportunity(before insert,before update)
{
	List<Id> accIds = new List<Id>();
	
	for(Opportunity opp:trigger.new)
	{
		if(opp.AccountId!=null)
		{
			accIds.add(opp.AccountId);
		}
	}
	
	Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
	
	for(Opportunity opp :trigger.new)
	{
		if(!accMap.IsEmpty())
		{
		if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = 10000;
			}
			else if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
				accMap.get(opp.AccountId).AccountNumber = 20000;
			}
		}
		}
	}
	
}

If this helps,please mark it as best answer to help others :)
Kon DeleKon Dele
Thanks Vinit. I'm trying this code but I'm getting a Complie error on line 23 for illegal assgnment from Integer to String. Is there a reason as to why this is happening?
Vinit_KumarVinit_Kumar
Ohk AccountNumber is of Text type,hence the error.Try this one :-
trigger updateAccountNumber on Opportunity(before insert,before update)
{
	List<Id> accIds = new List<Id>();
	
	for(Opportunity opp:trigger.new)
	{
		if(opp.AccountId!=null)
		{
			accIds.add(opp.AccountId);
		}
	}
	
	Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
	
	for(Opportunity opp :trigger.new)
	{
		if(!accMap.IsEmpty())
		{
		if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = '10000';
			}
			else if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
				accMap.get(opp.AccountId).AccountNumber = '20000';
			}
		}
		}
	}
	
}

If this helps,please mark it as best answer to help others :)

Kon DeleKon Dele
Thanks @Vinit_Kumar! I still have a question about the line 
accMap.get(opp.AccountId).AccountNumber = '10000';

Is there a way to have this assign a sequential number, so that if an Account Number = 10000, then the next will be 10001, 10002,...and so on? Thanks
Vinit_KumarVinit_Kumar
Yes,you can have that you need to change the code as per your requirement,Try below code :-
trigger updateAccountNumber on Opportunity(before insert,before update)
{
	List<Id> accIds = new List<Id>();
	
	for(Opportunity opp:trigger.new)
	{
		if(opp.AccountId!=null)
		{
			accIds.add(opp.AccountId);
		}
	}
	
	Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
	
	for(Opportunity opp :trigger.new)
	{
		if(!accMap.IsEmpty())
		{
		if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = '10000';
			}
			else if(accMap.get(opp.AccountId).Region__c=='International')
			{
				accMap.get(opp.AccountId).AccountNumber = '20000';
			}
		}
		else if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber!=null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1;
			}
			else if(accMap.get(opp.AccountId).Region__c=='International')
			{
				accMap.get(opp.AccountId).AccountNumber = Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1;
			}
		}
		else
		{
		
		}
		
		}
	}
	
}

If this helps,please mark it as bestanswer to help others :)


Kon DeleKon Dele
Thanks Vinit. But it still returns error on line 34 and 38 for 'illegal assignment of integer to string. any ideas?
Vinit_KumarVinit_Kumar
Kon,

Try this one,I think this should work :-

trigger updateAccountNumber on Opportunity(before insert,before update)
{
	List<Id> accIds = new List<Id>();
	
	for(Opportunity opp:trigger.new)
	{
		if(opp.AccountId!=null)
		{
			accIds.add(opp.AccountId);
		}
	}
	
	Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
	
	for(Opportunity opp :trigger.new)
	{
		if(!accMap.IsEmpty())
		{
		if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = '10000';
			}
			else if(accMap.get(opp.AccountId).Region__c=='International')
			{
				accMap.get(opp.AccountId).AccountNumber = '20000';
			}
		}
		else if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber!=null))
		{
			if(accMap.get(opp.AccountId).Region__c=='Americas')
			{
			    accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
			}
			else if(accMap.get(opp.AccountId).Region__c=='International')
			{
				accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
			}
		}
		else
		{
		
		}
		
		}
	}
	
}

If this helps,please mark it as best answer to help others :)
Kon DeleKon Dele
 Hi again. It still will not populate the AccountNumber field. I tried after insert/update events also. I even tried writing it on the Account object but to no avail. Any other ideas??
Vinit_KumarVinit_Kumar
Kon,

My bsad,I forgot to update the Account.Try below code,this should work :-

trigger updateAccountNumber11 on Opportunity(before insert,before update)
{
    List<Id> accIds = new List<Id>();
    
    for(Opportunity opp:trigger.new)
    {
        if(opp.AccountId!=null)
        {
            accIds.add(opp.AccountId);
        }
    }
    
    Map<Id,Account> accMap = new Map<Id,Account>([select id,Region__c,AccountNumber from Account where id in:accIds]);
    
    for(Opportunity opp :trigger.new)
    {
        if(!accMap.IsEmpty())
        {
        if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber==null))
        {
            if(accMap.get(opp.AccountId).Region__c=='Americas')
            {
                accMap.get(opp.AccountId).AccountNumber = '10000';
            }
            else if(accMap.get(opp.AccountId).Region__c=='International')
            {
                accMap.get(opp.AccountId).AccountNumber = '20000';
            }
        }
        else if((opp.Probability>=85) && (accMap.get(opp.AccountId).AccountNumber!=null))
        {
            if(accMap.get(opp.AccountId).Region__c=='Americas')
            {
                accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
            }
            else if(accMap.get(opp.AccountId).Region__c=='International')
            {
                accMap.get(opp.AccountId).AccountNumber = String.ValueOf(Integer.Valueof(accMap.get(opp.AccountId).AccountNumber) + 1);
            }
        }
        else
        {
        
        }
        
        }
    }
    
    update accMap.values();
    
}

If this helps,please mark it as best answer to help others :)
This was selected as the best answer
Kon DeleKon Dele
Thanks Vinit!!! It now populates the fields!!! Thank you. One more clarification, is there a way I could run a soql query, so that it can find the highest number and add 1 to that, so that I dont have duplicates? Thanks
Vinit_KumarVinit_Kumar
You should be using Aggregate Function in SOQL to get the max value in a particular field.Go through the below link to learn more,look for MAX() function :-

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_agg_functions.htm

Hope this helps !!
Kon DeleKon Dele
Thanks again Vinit! Finally go it working!!!