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
Abhishek Rajput 20Abhishek Rajput 20 

display child account name on Parent Account

Hi, 

I am trying to write a trigger to fetch all child account name on parent account. But my trigger is pulling only one child account on which we are making chages instead all child accounts. Please advise me on this.

trigger childaccount on Account (after update) {

map<id,Account> map1 = new map<id,Account>();

String str;

for (Account a : trigger.new)
{
if(a.parentId != null)
map1.put(a.parentId,a);
}

List<Account> lstanct = [Select name, ChildAccountName__c From Account where id in: map1.keyset()];

For (Account ac : lstanct){

str = str + ',' + map1.get(ac.id).name;
ac.ChildAccountName__c = str;
}

update lstanct;
}
Best Answer chosen by Abhishek Rajput 20
Amit Chaudhary 8Amit Chaudhary 8
Please try below code that will help you. No need to add two query
trigger childaccount on Account (after update) 
{
	Set<String> SetParentId = new set<String>();
	String str;
	for (Account a : trigger.new)
	{
		if(a.parentId != null)
		{
			SetParentId.add(a.parentId);
		}
	}

	if(SetParentId.size() > 0 )
	{
		List<Account> lstParentAcc = [ Select name, ChildAccountName__c , (Select Name From ChildAccounts) From Account where id in :SetParentId ];	
		
		For (Account ac : lstParentAcc)
		{
			List<Account> lstChildAcc = ac.ChildAccounts;
			str='';
			for( Account childAcc : lstChildAcc )
			{
				str = str + ',' + childAcc.name;
			}
			ac.ChildAccountName__c = str;
		}
		if(lstParentAcc.size() > 0 )
		{
			update lstParentAcc;
		}	
	}	
}
Please let us know if this will help you

Thanks
Amit Chaudhary

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. i hope that will help you
trigger childaccount on Account (after update) 
{
	map<id,List<Account>> map1 = new map<id,List<Account>>();
	Set<String> SetParentId = new set<String>();
	String str;
	for (Account a : trigger.new)
	{
		if(a.parentId != null)
		{
			SetParentId.add(a.parentId);
			if(map1.containsKey(a.parentId))
			{
				List<Account> lstAcc = map1.get(a.parentId);
				lstAcc.add(a);
			}
			else
			{
				List<Account> lstAcc = new list<Account>();
				lstAcc.add(a);
				map1.put(a.parentId,lstAcc);
			}	
		}
	}

	List<Account> lstanct = [Select name, ChildAccountName__c From Account where id in :SetParentId ];

	For (Account ac : lstanct)
	{
		if(map1.containsKey(ac.id))
		{	
			List<Account> lstCAcc = map1.get(ac.id);
			str='';
			for(Account childAcc : lstCAcc)
			{
				str = str + ',' + childAcc.name;
			}
		}	
		ac.ChildAccountName__c = str;
	}
	update lstanct;
}


 
Abhishek Rajput 20Abhishek Rajput 20
Thnaks for reply Amit.but still facing the same issue only one child account is pulling.
Laura Walker 7Laura Walker 7
Hiya  Have you thought of using the Declarative Rollup Summary Tool.
https://developer.salesforce.com/page/Declarative_Rollup_Summary_Tool_for_Force.com_Lookup_Relationships 
I have used this where I needed a Rollup Summary without a Master Detail relationship.
Let me know how it goes :-)
 
Abhishek Rajput 20Abhishek Rajput 20
Still I am facing same issue. In meanwhile I wrote this trigger.

trigger childaccount on Account (after update) {
 
map<id,Account> map1 = new map<id,Account>();
Set<id> setids = new Set<id>();
 
String str='';
 
for (Account a : trigger.new)
{
if(a.parentId != null)
map1.put(a.parentId,a);
 
}
List<Account> lst = [Select name, parentid, id From Account Where parentid in: map1.keyset()];
List<Account> lstanct = [Select name, ChildAccountName__c From Account where id in: map1.keyset()];
 
For (Account ac : lstanct){
       For(Account acc : lst){
 
str = str + ',' + acc.name;
 
}
ac.ChildAccountName__c = str;
}
update lstanct;
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code that will help you. No need to add two query
trigger childaccount on Account (after update) 
{
	Set<String> SetParentId = new set<String>();
	String str;
	for (Account a : trigger.new)
	{
		if(a.parentId != null)
		{
			SetParentId.add(a.parentId);
		}
	}

	if(SetParentId.size() > 0 )
	{
		List<Account> lstParentAcc = [ Select name, ChildAccountName__c , (Select Name From ChildAccounts) From Account where id in :SetParentId ];	
		
		For (Account ac : lstParentAcc)
		{
			List<Account> lstChildAcc = ac.ChildAccounts;
			str='';
			for( Account childAcc : lstChildAcc )
			{
				str = str + ',' + childAcc.name;
			}
			ac.ChildAccountName__c = str;
		}
		if(lstParentAcc.size() > 0 )
		{
			update lstParentAcc;
		}	
	}	
}
Please let us know if this will help you

Thanks
Amit Chaudhary

 
This was selected as the best answer
Abhishek Rajput 20Abhishek Rajput 20
Thanks Amit. Its working fine. Actually I was not aware about  (Select Name FromChildAccounts) query.