2

I would like to know that is there any way my trigger will be fired when any record is added to a related list .
Right now , The trigger iterates through the related list records and updates a field in Account .
So suppose a new record is added to the related list , the field will not be updated unless the account is edited . Is there any way it can be updated ?

Edit :Here Account and Client are in Lookup relationship.

trigger ABCD on Account (before 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) {
                If(pc.Status__c != 'Lapsed') { 
                    demoField++;
                }
            }
            acc.Field1__c = demoField;
            updateList.add(acc);
        }
        if(updateList.size()>0){
            update updateList;
        }
    }
}
  • 1
    Your trigger is on account, that is incorrect, your trigger should be on child object – C0DEPirate May 30 '16 at 14:11
  • 1
    Which field are you referring to that would be updated if edited in Account, account.Field1__c? Right now, your trigger doesn't make sense. How would Account know if a new Clients__r is inserted? What will cause it to fire? – crmprogdev May 30 '16 at 14:16
  • There is an already created solution for a similar problem github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries – Oles Malkov May 30 '16 at 14:29
  • @crmprogdev - I would want to update count of Field1__c in Account if status__c in any related record != lapsed. – SFDCRookie May 30 '16 at 14:33
3

Based on your answer to my comments, your trigger needs to be firing on your related objects, NOT on Account. This is what's referred to as a roll-up count trigger. The entry condition would be for it to fire when a record's current status__c != lapsed (taken from trigger.new) and the previous value (taken from trigger.old if there was one) of status__c == lapsed. There's a managed package from the App Exchange that will do this for free for a limited number of fields on a single object called Roll-up Helper.

1

Write trigger on the related record. Whenever a new Client is added update its corresponding parent Account.

  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review – Sebastian Kessel May 30 '16 at 14:38
  • 2
    @SebastianKessel why do yo think that this is not an answer? Maybe it is not very detailed but it describes what to do to solve the problem. – Oles Malkov May 30 '16 at 15:08
  • 1
    Because a one line post with no context or code should be a comment to the question, not an answer. If you are going to answer the question then give the OP more than a passing comment about how to solve his/her problem. – Sebastian Kessel May 30 '16 at 15:12
  • @SebastianKessel He has written most of answer by him self. He just had to write it at the right place. Anyways thanks for the valuable feedback. I will improve as per site standards. :) – Shankish May 30 '16 at 17:41
  • 1
    @Shankish, that's all that it's needed. We should strive to make our answers comprehensive and detailed (ergo, helpful), otherwise, rookie/newbie developers might end up with more questions than answers. – Sebastian Kessel May 30 '16 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.