1
trigger ABCD on Account(after insert, after update) {
    List < Account > AccList = [Select id, (Select Id, Status__c from Clients__r) from Account WHERE Id IN: trigger.new];
    for (Account acc: AccList) {
        Integer Field1 = 0;
        for (Client__c pc: acc.Clients__r) {
            If(Status__c != 'Lapsed') {
                Field1=Field1+1;
            }
            acc.Field1__c = Field1;
        }
    }
}

Purpose -

I want to increase the count of a people in the object Account which has a lookup with another object. So I want to traverse the related list records, check if a field value is equal to lapsed, if No - increase the count.

Error -

Didn't understand relationship 'LookupNameinAccount__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Question -

  1. I was reading articles on the relationship name . But not sure what exactly I need to use . I am currently using the name in the Fields section .
  2. Is this code overall okay?
5
  • 2
    You have to give the PluralForm of the Custom Object appending __r for this kind of scenarios like If the Object Name is Student__c you have to give Students__r in that part of Query call
    – Subhash
    May 30, 2016 at 10:06
  • 1
    You should not give the Object Field directly like that. First Take one variable Integer demoField = 0; and do your business logic of Incrementing and Finally use that demoField value to the Object Field Value. According to your scenario, you are using After Insert and After Update, So you should use the DML operation of Update accordingly....
    – Subhash
    May 30, 2016 at 10:28
  • Field1__c belongs to which object? Either Account or Another Object used in it?
    – Subhash
    May 30, 2016 at 10:33
  • @Subhash- Field1 is the field in Account whose count needs to be increased after traversing the Status field in the related records of the lookup object. Can you please help me with the syntax to correct the traversing in lookup object ?for (LookupObject__c pc: acc.LookupNameinAccount) { Say my lookup object/relationship name are both Client
    – SFDCRookie
    May 30, 2016 at 10:37
  • Check my below answer. It will work for your scenario...
    – Subhash
    May 30, 2016 at 11:09

1 Answer 1

2

You cannot do this directly with one trigger. Because it will gives the Recursion if you use the Update DML directly

Try the below procedure

Apex Class (Which will be used to stop the Execution of Trigger more than once)

public class RecursiveClass {
    public static boolean updateOnce = false;
}

Apex Trigger

trigger ABCD on Account (after insert, after update) {
    if(RecursiveClass.updateOnce == false){
        RecursiveClass.updateOnce = true;
        List<Account> AccList = [Select id, Field1__c, 
                                        (Select Id, Status__c 
                                   from Clients__r) from Account WHERE Id IN: trigger.new];
        List<Account> updateList = new List<Account>();
        Integer demoField = 0;
        for (Account acc: AccList) {
            for (Client pc: acc.Clients__r) {
                 /* Change the Status based on your condition because In coding you given 
                 Not equal to 'Lapsed' but in Explanation you have given 
                 Equal to 'Lapsed'. Change that according to your scenario.
                 */
                If(pc.Status__c != 'Lapsed') {
                    demoField++;
                }
            }
            acc.Field1__c = demoField;
            updateList.add(acc);
        }
        if(updateList.size()>0){
            update updateList;
        }
    }
}

Hope this might helps...

8
  • Thanks for the update . However the SOQL is returning Null for the Related object fields. i.e Select id, Field1__c, (Select Id, Status__c from Clients__r) from Account . Here Id/Status of Client object is returned null. And it has records . Any reason why so?
    – SFDCRookie
    May 30, 2016 at 11:44
  • Did you try Debugs, It will work perfectly if your related object has records in it...Please check your Related records once.
    – Subhash
    May 30, 2016 at 12:01
  • -Thanks . It's working. If I want to update the field , once any record in related list is added , what changes needs to be done? Because then , the trigger wont be fired , but I would like the count to be updated as per my requirement..
    – SFDCRookie
    May 30, 2016 at 13:04
  • Is your problem solved? And Sorry, I can't understand what you ask... Can you please explain clearly what you want?
    – Subhash
    May 31, 2016 at 5:03
  • My requirement is - I have Account and Client object in Lookup relationship. Client has a field Status . If Status!=Lapsed , count in a field in Account needs to be increased . So if a new record is added to Client with Status!= Lapsed , the field must be updated +1 .
    – SFDCRookie
    May 31, 2016 at 5:21

You must log in to answer this question.

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