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 

Opportunity Chatter Feed Created Date

When I query the Policy class below I expect to retrieve the latest post date on  the Opportunity feed for a particular Opportunity, but it appears that It is not returning what I expected. My end goal is to evaluate the date as low, medium or high based on the criteria in the if statements. Everything else works as expected, I am just running into problems with the last Chatter post date. Any help would be appreciated.

 

 

global class OppPoliceController {

public void Opportunity1() {

List<Opportunity> Opptys2 = [SELECT o.LastActivityDate, o.Activity_Status__c, o.StageName, (SELECT CreatedDate from Feeds), (SELECT Status FROM Tasks) FROM Opportunity o WHERE o.StageName != 'Closed Won' AND o.StageName != 'Closed Lost' ORDER BY o.LastActivityDate DESC];

for(Opportunity Op : Opptys2){

datetime LastActivityDate = Op.LastActivityDate; //Last Activity Date in datetimeformat
datetime CreatedDate = null;
datetime CreatedDate1 = Op.feeds[0].CreatedDate; //Created Date in datetime format
date d = Date.today(); //Current Date Formated

string Status = '';
Status = Op.tasks[0].Status;
System.debug('This is the created date: ' + CreatedDate1);
System.debug('This is the status: ' + Status);
System.debug('This is todays date: ' + d);

if(LastActivityDate == Null) {

if(Status == 'In Progress' || Status == 'Waiting on someone else' || Status == 'Deferred') {

Op.Activity_Status__c = 'Low';


}
else if(Status == 'Not Started') {

Op.Activity_Status__c = 'High';


}
}

else if(d > LastActivityDate.addDays(14) && d > CreatedDate1.addDays(14)) {

Op.Activity_Status__c = 'High';

}

else if((d >= CreatedDate1.addDays(7) && d < CreatedDate1.addDays(14))) {

Op.Activity_Status__c = 'Medium';

}
else if(d >= LastActivityDate.addDays(7) && d < LastActivityDate.addDays(14)) {

Op.Activity_Status__c = 'Medium';

}



else if(d < LastActivityDate.addDays(7) || d < CreatedDate1.addDays(7) || Status == 'In Progress' || Status == 'Waiting on someone else' || Status == 'Deferred') {

Op.Activity_Status__c = 'Low';

}

else {

Op.Activity_Status__c = 'Error';

}
}
update Opptys2;
}
}

 

 

cloudcodercloudcoder

you probably want to return your results ORDER BY CreatedDate DESC, Id DESC

 

This should give you the order you are looking for. Don't forget to LIMIT the results too to avoid hitting any limits.

VPrakashVPrakash

Hi,

 

Use 

 

SELECT CreatedDate from OpportunityFeed)

 

instead of 

 

(SELECT CreatedDate from Feeds)