+ Start a Discussion
RahulRahul 

Hello friends, Iam able to display count of Accounts in bucket1__c custom object which is the parent, but i also want to display the Account name in borrower name field.I tried to do it, but it gives error. Please help.Find my code below

trigger updateborrowerinbucket1 on Account (after insert, after update) {

set<id> setid = new set<id>();
list<bucket1__C> bucketlist = new list<bucket1__C>();

for(Account a : trigger.new){
setid.add(a.Bucket_1__c);

}

for(bucket1__c bb : [select id,Borrower_Name__c,(select id, name, Mobile_Number__c from Accounts__r)from bucket1__C]){

for(bucket1__C bb5 : bb.Accounts__r){
bucket1__C bb2 = new bucket1__C();
bb2.id=bb.id;
bb2.count_of_patients__c=bb.Accounts__r.size();
bb2.Borrower_Name__c=bb.Name;

bucketlist.add(bb2);
}
}

update bucketlist;
}
Rameshwar gaurRameshwar gaur
You don't need to make a new object for update.
just change value ,add in the list and update.
for(bucket1__c bb : [select id,Borrower_Name__c,(select id, name, Mobile_Number__c from Accounts__r)from bucket1__C]){

for(bucket1__C bb5 : bb.Accounts__r){

//Here Is the Change

bb5.count_of_patients__c=bb.Accounts__r.size();
bb5.Borrower_Name__c=bb.Name;

bucketlist.add(bb5);
}
}

Hope this help you.