0

I have a trigger that I am trying to update the custom object (Share_Reference__c) within the Account object. For some reason my query in List accounts isn't working, saying that it didn't understand the relationship 'Share_References__r'. Any ideas? It should be the child object, like I have it. Thanks!

Trigger

        trigger UpdateIsShareRef on Share_Reference__c (after insert, after update, after delete, after undelete) {

    Set<ID> accountIDs = new set<ID>();
    List<Share_Reference__c> records;

    If  (trigger.isDelete) {
        records = trigger.old;
    } else {
        records = trigger.new;
    }

    for (Share_Reference__c record : records) {
        accountIds.add(record.Account);
    }

    **List<Account> accounts = [SELECT Id, is_Share_Ref_USAT__c, is_Share_Ref_Digital__c, (SELECT id, SalesTeam__c FROM Share_References__r) FROM Account WHERE ID IN :accountIds];**

    For (Account act : accounts){
        act.is_Share_Ref_USAT__c = false;
        act.is_Share_Ref_Digital__c = false;

        for (Share_Reference__c ref : act.Share_References__r){

            if (ref.SalesTeam__c == 'USAT Integrated Brand Sales'){
                act.is_Share_Ref_USAT__c = true;
            }

            if (ref.SalesTeam__c == 'Digital Sales'){
                act.is_Share_Ref_Digital__c = true;
            }
        }

        update accounts;
    }
}
4
  • are you getting error in this stared part of code. SELECT Id, is_Share_Ref_USAT__c, is_Share_Ref_Digital__c, (SELECT id, SalesTeam__c FROM ************Share_References__r**********) FROM Account WHERE ID IN :accountIds
    – Suresh
    Commented Apr 27, 2015 at 20:04
  • Yeah, that's the issue.
    – Stephen
    Commented Apr 27, 2015 at 20:09
  • 1
    if you have eclipse please check the child relationship api names on Account. your Child Relationship API name might be different then the one you are using in query.
    – Suresh
    Commented Apr 27, 2015 at 20:18
  • if you didn't specify a plural for share_reference__c object during definition, you might have an extraneous s in share_references__r
    – cropredy
    Commented Apr 28, 2015 at 2:11

2 Answers 2

1

Each relationship has a standard or custom relationship name, except for a few exceptions. Any time a relationship is available, you can query it in your sub-query. Please note that the name of the relationship may not reflect the name of the object, the field that created the relationship, or anything else.

You'll need to specifically look at the relationship to figure out what the correct name should be. Go to the Share_Reference__c object, find your Lookup or Master-Detail field, and click on the field's label (Setup > Create > Objects > Share Reference > Custom Fields and Relationships). In the lower-right corner of the field's detail page will be a field called "Child Relationship Name." Since it's custom, you will append the __r, even though it won't show in the relationship name field.

1
  • It turned out that it wasn't Account that I needed, but a different field that related Share_Reference__c to Account. Thanks!
    – Stephen
    Commented Apr 28, 2015 at 12:19
0

*(SELECT id, SalesTeam__c FROM Account.Share_References__r) FROM Account .

Use Account.Share_References__r .....in the Query ....

3
  • That would only work if Share References was the parent of the Account object. The aim in this case is to query child records.
    – Matt Lacey
    Commented Apr 28, 2015 at 6:15
  • Sample Program Fetching multiple Records Using Parent to Child Relationship ............. List<Account> a = [Select Name,(Select FirstName,LastName from Account.Contacts) From Account where Name='Prasad']; System.debug('The Account Records are' +a); for(Account acc1:a) { for(Contact c:acc1.Contacts) { System.debug('The Contacts are' +c); }}
    – Vamshi
    Commented Apr 28, 2015 at 6:24
  • /* mutiple record related list c-p(contact-account)*/ list<contact> c=[select LastName,MobilePhone,account.name,account.AnnualRevenue from contact where name='Peter']; system.debug('contact fields'+c); for(contact c1:c){ system.debug('account.name'+ c1.account.name); system.debug('account.AnnualRevenue'+ c1.account.AnnualRevenue); }
    – Vamshi
    Commented Apr 28, 2015 at 6:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .