0

pretty high level question here based on some of the erroneous updates I'm seeing (less than 1% of total records processed).

I'm currently running a batch class (50 record batch size).

It is a simple query to get parent records that need to be updated. The relationship works like this:

Custom_Object__c -- 1-Many --> Contact -- 1-Many --> Opportunity

Start Method

Query Custom_Object__c (get all records) approximately 10,000.

Execute Method

This is where it gets a little odd...

for (Custom_Object__c cust : customers) {
            //Check for opps that were missed and need to be reset. Split these into reset list and mark list
            if(cust.Custom_Field__c == true && cust.is_Deleted__c == false){
                cust.Custom_Field__c = false;

            } else if (cust.Custom_Field__c == false && cust.Contact__c !=null) {
                conOppMap.put(cust.Contact__c, new List<Opportunity>());
                targetCustomers.add(cust);
                contacts.add(cust.Contact__c);
                totalContactsProcessed++;
            }
            totalCustProcessed++;
        }


        List<Opportunity> opps = [
            SELECT Id,
                Name,
                StageName,
                Sale_Date__c,
                CloseDate,
                LookUp_Contact__c
            FROM Opportunity
            WHERE LookUp_Contact__c IN:contacts
        ];


        for (Opportunity opp : opps){

            totalOppsProcessed++;
            Integer openOpps = 0;
            if (conOppMap.containsKey(Id.valueOf(opp.LookUp_Contact__c))){

                if (!isNotClosed(opp)){
                    System.debug('CALCULATION FOUND OPEN OPPORTUNITY');
                    openOpps++;
                    totalOtherOpenOpps++;
                }else if(isNotClosed(opp) && opp.StageName == 'ClosedWon' && calculateDays(opp.Delivery_Date__c) < daysAfterDelivery){
                    System.debug('ClosedWon Opp Invalid: ' + opp.Id);
                    openOpps++;
                    openClosedWonOpps++;
                }else if(opp.StageName == 'ClosedWon'){
                    del_ClosedWonOpps++;
                    System.debug('Del-ClosedWon: ' + opp.Id);
                }else if(isNotClosed(opp) && opp.StageName == 'Cancelled' && calculateDays(opp.CloseDate) < daysAfterCancelled){
                    openOpps++;
                    openCancelledOpps++;
                }else if (opp.StageName == 'Cancelled'){
                    del_CancelledOpps++;
                }else if(isNotClosed(opp) && opp.StageName == 'Closed Lost' && calculateDays(opp.CloseDate) < daysAfterClosedLost){
                    openOpps++;
                    openClosedLostOpps++;
                }else if(opp.StageName == 'Closed Lost'){
                    del_ClosedLostOpps++;
                }

                if(openOpps > 0) {
                    System.debug('OPENOPP FOUND: ' + opp.Id + ' | ' + opp.StageName);
                    conOppMap.get(opp.LookUp_Contact__c).add(opp);
                }
            }
        }

        for (Custom_Object__c sc_del : targetCustomers) {
            while (conOppMap.get(sc_del.Contact__c).size() == 0 && conOppMap.get(sc_del.Contact__c).size() != null && (del_customers.size() < testDeleteLimit || testDeleteLimit == null)) {
                sc_del.Custom_Field__c = true;
                del_customers.add(sc_del);
                break;
            }
        }

        if (del_customers.size() > 0) {
            totalDeletes += del_customers.size();
            jobID = System.enqueueJob(new Queueable_RemoveCustomers(del_customers));
        }

^^ My question in the above is related to the Opp SOQL query. Is it possible that I'm not getting all the opps related to a contact?

If not, is it possible that because I'm not ordering that query, that certain opps are resetting the openOpp counter back to 0? Really bizarre.

Finish Method

Send Emails with static data.

  • 1
    What exactly is the issue you're seeing? You mention your counter being 0, but what ends up happening (or better yet, not happening that you expect) – Kris Goncalves Jan 13 at 17:05
  • @KrisGoncalves the counter is 'openOpps' it detects whether or not a an opp is open and if it is an open opp it adds a +1 – thinker Jan 13 at 17:12
  • The issue I'm running into is I had a customer with 4 opps. 1 of them met the delete criteria, but 3 were still open. The only thing I can figure at this point is that the SOQL query for opps. did not find all the opps for the customer and only returned the Closed opp. – thinker Jan 13 at 17:13
  • 1
    so it's never going past if(openOpps > 0) in these 1% of scenarios? – Kris Goncalves Jan 13 at 17:42
  • Correct, so I have had it happen 2 times now (so far we have processed less than 1000 records). Where contact had open opps (i.e. should have entered into the if(openOpps>0) condition, and then skipped the next for loop because the conOppMap would have a list in it greater than 0. However, it appears that the opps that qualified for delete got through in the SOQL query, but the open opps did not. – thinker Jan 13 at 17:56

Your Answer

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

Browse other questions tagged or ask your own question.