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 

Having problems setting up a loop query.

What I would like to accomplish is a apex class that will trigger everytime a report is generated. It will query all Oppourtunities and then assign them a Activity Status based on the last Activity date. This is what I have so far with the current error I am recieving. I know it needs ton of work and any help would be apprecited.

 

 

Here is the requirements:

 

Activity Status wil be High
If Opportunity.LAST_ACTIVITY_DATE  is greater than 14 days

Medium
If Opportunity.LAST_ACTIVITY_DATE  is in-between 7 and 14
days

Low

If Opportunity.LAST_ACTIVITY_DATE  is less than 7 days.

 

Chris

 

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Opportunity> at line 7 column 16


 

Public class OppPoliceController    {

       List<Opportunity> O = [SELECT LastActivityDate, Activity_Status__c FROM Opportunity]; {
       
        
            If (O.LastActivityDate > (today() - 14))    {
               O.Activity_Status__c = 'High';
            }
        
            else if (O.LastActivityDate >= (today() - 7))    {
               O.Activity_Status__c = 'Medium';    
            }
        
            else if (O.Last_Activity_Date < (today() - 14))    {
               O.Activity_Status__c = 'Medium';
            }
        
            else if (O.LastActivityDate > (today() - 7))
               O.Activity_Status__c = 'Low';
            
           update O;
           }
        
       }                
 

WizradWizrad

A few things to say about this:

 

1) You can do this with workflows and field updates.

2) You cannot create a loop with the following syntax:

 

 

    List<Opportunity> O = [SELECT LastActivityDate, Activity_Status__c FROM Opportunity]; {

 You mean to do

 

 

 

for(Opportunity o : [SELECT LastActivityDate, Activity_Status__c FROM Opportunity]) {

3) It's never a good idea to query for all of something without a where clause.  You really need ALL opportunities?  When you have more than 50,000 Oppty records this code will break.

 

4) Never put DML inside of loops.  Update your list of opportunities after you have changed the Activity_Status__c.

 

Dchris222Dchris222

Question, can I include Chatter in the workflows and field updates? I also would like to implement for example:

 

High
If Opportunity.LAST_ACTIVITY_DATE and Opportunity.Chatter.Last_Post_Date are greater than 14 days

Med
If Opportunity.LAST_ACTIVITY_DATE and Opportunity.Chatter.Last_Post_Date are in between 7 and 14
days

Low
If Opportunity.LAST_ACTIVITY_DATE and Opportunity.Chatter.Last_Post_Date are less than 7 days.

 

Chris