function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dchris222Dchris222 

Field Update Based On Criteria Not Working

Afternoon everyone,

 

Running into problems debugging this apex class. It has no runtime errors so it's obiviously logic errors. I am attemping to querry all Opportunity "Last Activitity Date" and also Chatter "Created Date" and then update the Activity Status field based on critera. This apex class will be ran everynight for a report. Any ideas on why the Activity status field is not updating?

 

public with sharing class OppPoliceController {
    
    public void Opportunity() {
    
     List<OpportunityFeed> Opptys = [SELECT id, (SELECT CreatedDate FROM FeedComments ORDER BY CreatedDate DESC) FROM OpportunityFeed];
     List <Opportunity> Opptys2 = [SELECT LastActivityDate, Activity_Status__c FROM Opportunity ORDER BY LastActivityDate];
  
       for(OpportunityFeed o : Opptys){   
            for(Opportunity Op : Opptys2){
         
            if(system.now() > o.CreatedDate.addDays(14) || system.now() > Op.LastActivityDate.addDays(14)) {

                Op.Activity_Status__c = 'High';
                update Opptys2;  
            }
            
            else if(system.now() >= o.CreatedDate.addDays(7) && system.now() < o.CreatedDate.addDays(14)
                      ||  system.now() >= Op.LastActivityDate.addDays(7) || system.now() < Op.LastActivityDate.addDays(14)) {
            
               Op.Activity_Status__c = 'Medium';
                update Opptys2;
            }
            
            else if(system.now() > o.CreatedDate.addDays(7) || system.now() > Op.LastActivityDate.addDays(7)) {

                Op.Activity_Status__c = 'Low';
                update Opptys2;    
            }
            }   
       }         
    }     
}

bob_buzzardbob_buzzard

A few thoughts:

 

(1) Do you have any comments in the opportunity feed?  If not, it doesn't make any difference how old the opportunity is, as you'll never go into the nested loop to iterate the opportunities

(2) You don't appear to be tying the entries from the opportunity feed to an opportunity.  As it stands, each entry in the feed will cause the entire list of opportunities to be iterated and potentially updated.

(3) You are updating the entire Opptys2 list every time you make a change to a status - it would be better to store all the changed opportunities and make a single update at the end.

 

I suspect #2 is going to be the most work to solve - you really need to be checking the age of the opportunity and then the age of the latest feed entry associated with that opportunity.

Dchris222Dchris222

Can you expand on your second thought please? I am very new to visual force development and not sure where to begin in regards to fixing what you suggested. In response to your 1st thought, the Opportunity does have comments in the opportunity feed. 

 

Chris

bob_buzzardbob_buzzard

At the moment you pull all the records from the OpportunityFeed and all the records from the Opportunity table.  You then iterate the OpportunityFeed records and for each of these, iterate all Opportunity records.  The tests that are then carried out look to be flagging an opportunity that hasn't had any activity for a while.  However, your comparison of the chatter record doesn't attempt to match up to the opportunity that it was created for.  E.g. if you had a chatter feed that was created more than 14 days ago, you would set the Activity_Status__c field for every opportunity to 'High'.  Presumably you should only be considering chatter feed records that relate to the opportunity?

Dchris222Dchris222

Any ideas on the best course of action to get this code working. I have tried a few things, but am having no luck so far....:smileysad: