You need to sign in to do that
Don't have an account?
Apex Trigger to Get Account ID
I have a custom object(ShipTo__c) that has a field (customer_code__c) that holds a value that is common to the account object. When I insert the Ship To record, I would like to use an Apex trigger to get the account id based on the (SELECT id from Account WHERE customer_code__c = XXXX) then populate that value on the custom object to link it to the account object. Am I over-thinking this and should I be joining the tables differently other than trying to SFDC ID's?
trigger SetAccountField on ShipTo__c (before insert, before update) {
for (ShipTo__c shipto : Trigger.new) {
String accid = shipto.Customer_Code__c;
List<Account> account = [SELECT Id FROM Account WHERE Customer_Code__c = :accid];
shipto.Account__c = account.get(0).Id;
}
}
trigger SetAccountField on ShipTo__c (before insert, before update) {
for (ShipTo__c shipto : Trigger.new) {
String accid = shipto.Customer_Code__c;
List<Account> account = [SELECT Id FROM Account WHERE Customer_Code__c = :accid];
shipto.Account__c = account.get(0).Id;
}
}
All I am doing is
1. Creating a Customer code set
2. Querying the Account table to get all accounts in one shot (this is only one SOQL statement)
3. Loading them into the Map collection.
4. Again going and verifying the each shipTo record to get the correct Account.
Please review the following link
http://www.sfdc99.com/beginner-tutorials/
if you want to use your code, you can do (i modified it a little). it will work fine for one or 2 records, but if someone updates or inserts more than 100 your trigger breaks
All Answers
You are writing a SOQL inside a for loop and your assignment is not correct.
Hope this helps.
All I am doing is
1. Creating a Customer code set
2. Querying the Account table to get all accounts in one shot (this is only one SOQL statement)
3. Loading them into the Map collection.
4. Again going and verifying the each shipTo record to get the correct Account.
Please review the following link
http://www.sfdc99.com/beginner-tutorials/
if you want to use your code, you can do (i modified it a little). it will work fine for one or 2 records, but if someone updates or inserts more than 100 your trigger breaks
If you are updating or inserting over 100 ShipTo__c records then your SOQL limit would hit and your forloop will execute 100 times. Whenever you write a Trigger you need to make sure it works in bulk.
01 trigger SetParentAccount on Account (before insert, before update) {
02
03 Set<String> customerCodeSet = new Set<String>();
04 for (Account acc : Trigger.new) {
05 customerCodeSet.add(acc.Group_ID__c);
06 }
07 Map<String,Account> customerCodeAccMap = new Map<String,Account>();
08 for(Account acc : [Select Id,Group_ID__c from Account where Group_ID__c IN :customerCodeSet && RecordType = '01239000000URW3AAO']){
09 customerCodeAccMap.put(acc.Group_ID__c,acc);
10 }
11 for (Account acc : Trigger.new) {
12 if(acc.Group_ID__c != null && customerCodeAccMap.containsKey(acc.Group_ID__c))
13 acc.Parent = customerCodeAccMap.get(acc.Group_ID__c).Id;
14 }
15}