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
RahulRahul 

hellow friends, Iam getting the following error."Error: Compile Error: A non foreign key field cannot be referenced in a path expression: Accounts__r at line 16 column 25. Please help"

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,name,Borrower_Name__c,(select id, name, Mobile_Number__c from Accounts__r)from bucket1__C]){

bucket1__C bb2 = new bucket1__C();
bb2.id=bb.id;
bb2.count_of_patients__c=bb.Accounts__r.size();
bb2.Borrower_Name__c=bb.Accounts__r.Name;

bucketlist.add(bb2);

}

update bucketlist;
}
Best Answer chosen by Rahul
Priyananth RPriyananth R
Hi sumit,

In Parent-to-Child query, it's fetch parent record and related all child records
like, one account have multiple contact(List of contact).
instead of yours, use this bb.Accounts__r[0].Name (line no. 16)

Just try this,
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,name,Borrower_Name__c,(select id, name, Mobile_Number__c from Accounts__r)from bucket1__C]){

bucket1__C bb2 = new bucket1__C();
bb2.id=bb.id;
bb2.count_of_patients__c=bb.Accounts__r.size();
bb2.Borrower_Name__c=bb.Accounts__r[0].Name;

bucketlist.add(bb2);

}

update bucketlist;
}

Thanks,

All Answers

Priyananth RPriyananth R
Hi sumit,

In Parent-to-Child query, it's fetch parent record and related all child records
like, one account have multiple contact(List of contact).
instead of yours, use this bb.Accounts__r[0].Name (line no. 16)

Just try this,
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,name,Borrower_Name__c,(select id, name, Mobile_Number__c from Accounts__r)from bucket1__C]){

bucket1__C bb2 = new bucket1__C();
bb2.id=bb.id;
bb2.count_of_patients__c=bb.Accounts__r.size();
bb2.Borrower_Name__c=bb.Accounts__r[0].Name;

bucketlist.add(bb2);

}

update bucketlist;
}

Thanks,
This was selected as the best answer
RahulRahul
Thank you so much Priyananth.