+ Start a Discussion
Stephen BundaStephen Bunda 

Trigger to populate Account lookup based on a account number.

Hi.  I'm completely new to triggers and need some help.  I want to use the Standard "Orders" object to populate orders for an account.  We are pulling in account data automatically from an external dataabase using a jitterbit integration.  We will also be feeding the order object via the same ETL tool and a different database table.  

The indexed field on the account is the "Account Number" field.  What I am trying to do is based on teh account number on the order feed is populate the account lookup with the account that matches the account number.  I would like a trigger to do this but I know nothing about writing triggers.Any help would be appreciated.
Best Answer chosen by Stephen Bunda
Neetu_BansalNeetu_Bansal
Hi Stephen,

Use the below trigger. Use your account number fields API name instead of Account_Number__c on Order and Account.
trigger OrderTrg on Order( before insert )
{
	Set<String> accountNumbers = new Set<String>();
	for( Order o : trigger.new )
	{
		if( o.Account_Number__c != null )
		{
			accountNumbers.add( o.Account_Number__c );
		}
	}
	
	Map<String, Id> accountNumbersToAccountMap = new Map<String, Id>();
	for( Account acc : [ Select Id, Account_Number__c from Account where Account_Number__c IN: accountNumbers ])
	{
		accountNumbersToAccountMap.put( acc.Account_Number__c, acc.Id );
	}
	
	for( Order o : trigger.new )
	{
		if( o.Account_Number__c != null && accountNumbersToAccountMap.containsKey( o.Account_Number__c ))
		{
			o.AccountId = accountNumbersToAccountMap.get( o.Account_Number__c );
		}
	}
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Stephen,

Use the below trigger. Use your account number fields API name instead of Account_Number__c on Order and Account.
trigger OrderTrg on Order( before insert )
{
	Set<String> accountNumbers = new Set<String>();
	for( Order o : trigger.new )
	{
		if( o.Account_Number__c != null )
		{
			accountNumbers.add( o.Account_Number__c );
		}
	}
	
	Map<String, Id> accountNumbersToAccountMap = new Map<String, Id>();
	for( Account acc : [ Select Id, Account_Number__c from Account where Account_Number__c IN: accountNumbers ])
	{
		accountNumbersToAccountMap.put( acc.Account_Number__c, acc.Id );
	}
	
	for( Order o : trigger.new )
	{
		if( o.Account_Number__c != null && accountNumbersToAccountMap.containsKey( o.Account_Number__c ))
		{
			o.AccountId = accountNumbersToAccountMap.get( o.Account_Number__c );
		}
	}
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Stephen BundaStephen Bunda
I'm using this "AccountNumber" for the API name but I keep getting this error:

Invalid Field AccountNUmber for SObject Order
Stephen BundaStephen Bunda
Thank you by the way.
Neetu_BansalNeetu_Bansal
Hi Stephan,

AccountNumber is not standard field on Order, it must be AccountNumber__c, change the field name with this and try again. If you still faces the same issue then will it be possible for you to share your salesforce org login credentials on that I can have a look into the issue. You can contact me either on my gmail id: neetu.bansal.5@gmail.com or Skype id: neetu.bansal.5

Let me know, if you are able to fix this.

Thanks,
Neetu
Stephen BundaStephen Bunda
I'm showing "AccountNumber" as a standard field on Order.

User-added image

So I'm a bit confused.  
Neetu_BansalNeetu_Bansal
I guess you have created this field, this was not by default added by Salesforce. You will not see __c in the screen, if the field is custom, you have to add it as suffix. Try once by adding __c and check if it works for you.

Thanks,
Neetu
Stephen BundaStephen Bunda
I really appreciate your help and I hate to be stubborn about this..but I didn't create that field.

Here is a list of the standard fields on the Order object:

User-added image

Here is what it looks like when I click on "Edit" next to Account Number

User-added image

And here are my custom fields:

User-added image
So, I can't change the name of that field.  If I need to create a custom field in order for this to work that's not a problem.  I was just curious if it would work with a standard field.
Stephen BundaStephen Bunda
Thanks for your help.  I created a new custom field and will use that one instead of the Standard field.
Neetu_BansalNeetu_Bansal
Hi Stephan,

I tried to save this trigger in my free developer org and its strange that I'm also getting the same error although the field is visible to all the profiles. I am still analysing and will let you know once I know the reason.

In the meantime, you can log a case to Salesforce regarding the same.

Thanks,
Neetu
Stephen BundaStephen Bunda
Thanks...and will I need a class and test class to move this to production?
 
Neetu_BansalNeetu_Bansal
Hi Stephan,

You need to create test class for this trigger to move this to production, If you need any help, you can contact me anytime.

Thanks,
Neetu
Stephen BundaStephen Bunda
Would you mind helping with a class and test class?  I would greatly appreciate it.
Neetu_BansalNeetu_Bansal
I would not mind helping with test class but can you please contact me on any of my personal id gmail: neetu.bansal.5@gmail.com or Skype: neetu.bansal.5 as that would be easy for me to communicate.

Thanks,
Neetu
Leslie ReinosoLeslie Reinoso
Hello, I am currently in need of a similar solution and this would actually work. I was hoping someone could help me with the test class please? Thank you!
Neetu_BansalNeetu_Bansal
Hello Leslie,

I would not mind helping with test class, Please contact me on any of my personal id gmail: neetu.bansal.5@gmail.com or Skype: neetu.bansal.5 to share your requirement.

Thanks,
Neetu