You need to sign in to do that
Don't have an account?

Apex Code not ablet to reference the Account Name field
I have written some apex code to basically do an update of account records where all the children of a master account will be updated with certain information. We have a custom field called "Parent_Account_Name" and this field should be updated with the master account's new name.
I create an array of accounts and try to access the Name field, but I get a null value. The problem is I can reference the name field by displaying accts[0].Name, (where accts is my array of accounts returned from a SOQL query) but not reference it by doing the 'for (Account a1 : accts)' and then displaying a1.Name. a1.Name shows as Null.
Below is the code for the trigger:
I have viewed the code in debug mode and see some wierd behavior.
Partial print of the debug code is below:
The debug statement at line 53 shows 2 accounts that are children of my master account. If I traverse the accts array that I created, I see that 'accts[0].Name' does show the correct name of 'SKM-Test-2'. Then I use a for loop to go through the accts array, 'for (Account a1 : accts)', and try to print out the values, but the 'Name' field is now Null! So how is it that I can reference the name field by dowing accts[0].Name, but not reference it by doing the 'For loop'?
I create an array of accounts and try to access the Name field, but I get a null value. The problem is I can reference the name field by displaying accts[0].Name, (where accts is my array of accounts returned from a SOQL query) but not reference it by doing the 'for (Account a1 : accts)' and then displaying a1.Name. a1.Name shows as Null.
Below is the code for the trigger:
Code:
if (Trigger.isAfter) { /* Once the insert/update trigger is completed, the cascading phase is as follows: 1. Get the Acct Id of the record that was just inserted/updated 2. Do a search to see which records have their ParentId the same as the Acct Id found in 1. 3. Update the new Parent Name for all those records in 2. */ for (Account a : Trigger.new) { System.Debug('isAfter-a: ' + a); Account[] accts = [select id, ParentId, Name from Account where ParentId = :a.Id]; System.Debug('SKM-Acct.Size: ' + accts.size()); if (accts.size() > 0) { System.Debug('accts[0]: ' + accts[0].Name); for (Account a1 : accts) { a1.Name = a.Parent_Account_Name__c; System.Debug('a1= ' + a1 + ', acct.Name= ' + a1.Name + ', a.Parent_Account.Name= ' + a.Parent_Account_Name__c); } } }
Partial print of the debug code is below:
Code:
20080219162042.523:Trigger.ParentNameUpdateTrigger: line 6, column 3: Starting ParentNameUpdate 20080219162042.523:Trigger.ParentNameUpdateTrigger: line 51, column 10: isAfter-a: Account:{SystemModstamp=Tue Feb 19 16:20:42 GMT 2008, Type=Master Customer, Parent_ID_Confirm__c=1, RecordTypeId=0125000000097vkAAA, OwnerId=00550000000s4qAAAQ, IsExcludedFromRealign=false, IsLocked=false, LastModifiedById=00550000000s4qAAAQ, MayEdit=false, IsDeleted=false, Master_Customer_Level__c=SKM-Tes1a, CreatedDate=Tue Feb 19 16:20:38 GMT 2008, LastModifiedDate=Tue Feb 19 16:20:42 GMT 2008, Account_ID_Display__c=001S0000002f8AY, Name=SKM-Tes1a, Id=001S0000002f8AYIAY, CreatedById=00550000000s4qAAAQ} 20080219162042.523:Trigger.ParentNameUpdateTrigger: line 53, column 10: SKM-Acct.Size: 2 20080219162042.523:Trigger.ParentNameUpdateTrigger: line 55, column 13: accts[0]: SKM-Test2 20080219162042.523:Trigger.ParentNameUpdateTrigger: line 58, column 17: a1= Account:{ParentId=001S0000002f8AYIAY, Name=null, Id=001S0000002f8AZIAY}, acct.Name= null, a.Parent_Account.Name= null 20080219162042.523:Trigger.ParentNameUpdateTrigger: line 58, column 17: a1= Account:{ParentId=001S0000002f8AYIAY, Name=null, Id=001S0000002f8AaIAI}, acct.Name= null, a.Parent_Account.Name= null
BTW, since I will be updating maybe more than one account, Is this the best approach?
//Creating a list object of accounts
List<Account> updatedaccts = new Account[]{};
then, update the accts in bulk using the list object of accounts:
for (Account a1 : accts) {
a1.Parent_Account_Name__c = a.Name;
System.Debug('a1= ' + a1 + ', a1.Parent_Account_Name= ' + a1.Parent_Account_Name__c + ', a1.Name= ' + a1.Name);
updatedaccts.add(a1);
}
update updatedaccts;
for (Account a1 : accts) {
a1.Parent_Account_Name__c = a.Name;
System.Debug('a1= ' + a1 + ', a1.Parent_Account_Name= ' + a1.Parent_Account_Name__c + ', a1.Name= ' + a1.Name);
updatedaccts.add(a1);
}
update updatedaccts;
This, however is not:
for (Account a : Trigger.new) {
System.Debug('isAfter-a: ' + a);
Account[] accts = [select id, ParentId, Name from Account where ParentId = :a.Id];
System.Debug('SKM-Acct.Size: ' + accts.size());
if (accts.size() > 0) {
System.Debug('accts[0]: ' + accts[0].Name);
for (Account a1 : accts) {
a1.Name = a.Parent_Account_Name__c;
System.Debug('a1= ' + a1 + ', acct.Name= ' + a1.Name + ', a.Parent_Account.Name= ' + a.Parent_Account_Name__c);
}
}
It's not acceptable because you are issuing a query for each account in the request. If a user sends in more than 20 accounts in one request (API, Import wizard, etc.) your trigger will fail because of the SOQL query call limit.