0

I have the following trigger for the Account object:

trigger LatestCaseBatchTrigger on Account (after update, before update) {

    public list<Issues__c> listCases = new List<Issues__c>();    
    Set<Id> AccountId = trigger.newMap.keySet();    
    public List<Account> account = [Select Id from Account where id IN :AccountId];

    List<Issues__c> casesUK = [SELECT Id, Name, RecordTypeId, CreatedDate FROM Issues__c WHERE Account__c = :AccountId AND Status__c IN ('KYC Review Complete', 'KYC Finalized', 'On-boarding Finalized') AND Client_Setup_with__c = 'BCS UK' AND RecordTypeId IN ('012b0000000kHRa', '012b0000000ULxm', '012b0000000kH4g', '012b0000000US2R') ORDER BY CreatedDate DESC NULLS LAST limit 1];     

    Set<Issues__c> setCaseToUpdate = new Set<Issues__c>();
    listCases = new List<Issues__c>();

    for(Account acc : account){             
        for(Issues__c iss : casesUK){
            iss.Latest__c = true;
            setCaseToUpdate.add(iss);
        }        
    }

    listCases.addAll(setCaseToUpdate);
    update listCases; 
}

It works fine when I update an account record manually. But when I try to make updates for all account records (through DataLoader) it doesn't run for all the accounts that I update. Please point out where I made a mistake.

1 Answer 1

2

I suggest something along these lines (I haven't tested this):

trigger LatestCaseBatchTrigger on Account (before update) {

    Issues__c[] issues = [
            SELECT Id, Name, RecordTypeId, CreatedDate, Account__c
            FROM Issues__c
            WHERE Account__c = :Trigger.newMap.keySet()
            AND Status__c IN ('KYC Review Complete', 'KYC Finalized', 'On-boarding Finalized')
            AND Client_Setup_with__c = 'BCS UK'
            AND RecordTypeId IN ('012b0000000kHRa', '012b0000000ULxm', '012b0000000kH4g', '012b0000000US2R')
            ORDER BY CreatedDate DESC NULLS LAST
            ];

    Set<Id> latestFound = new Set<Id>();
    for (Issues__c issue : issues) {
        issue.Latest__c = !latestFound.contains(issue.Account__c);
        latestFound.add(issue.Account__c);
    }

    update issues;
}

So the idea is to note when the latest issue is found for an Account by putting the Account Id in a set. (The LIMIT 1 in your query is one problem.)

If the number of issues per Account is large this will break due to governor limits and something fancier will be needed that e.g. only unsets the Latest__c field on the issues that have it set; this code unsets all but the first. There may be an aggregate query that would help in that too.

Seems a bit strange that you only want to do this when the Account changes and not when a new issue is added.

7
  • Hi my friend! I have 1-7 Issues__c per account record. I need to catch only the senior Issue__c record per account record. If I delete "Limit 1" statement i will get all Issue__c record per account record. I just try to create Butch process but because the query is to difficult I prefer to use the trigger.
    – Viktor
    Oct 28, 2016 at 10:16
  • Also if remove "Limit 1" it will iterate through all account records, but it will make changes for all Issues__c records, not for the senior.
    – Viktor
    Oct 28, 2016 at 10:19
  • @Viktor With a few issues per account the code I posted should be OK. As multiple Account records can be involved (the bulk case that a trigger must support) you can't just return 1 Issues__c record. Another reason to return may records is that if you are marking 1 record as the latest you need to unmark the previous latest record.
    – Keith C
    Oct 28, 2016 at 10:27
  • 1
    @KeithC why not query the account with a issue subquery. Then you could limit the issue to the one you want to update
    – Eric
    Oct 28, 2016 at 11:27
  • @Eric Sounds like a much better solution - I'll give it +1 if you post it as an answer. (Might need another query to find and unset the previous Latest__c.)
    – Keith C
    Oct 28, 2016 at 11:30

You must log in to answer this question.

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