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
riffindusriffindus 

scheduled job based on created date past 7 days

Hi,

 

I have to write a scheduled job to find out the opportunity created past 7 days and assign it to another set of owners.

 

when i tried the below code, i am getting some issues with createddate on SOQL.

 

below is my code,

 

Set<Opportunity> setopp = new Set<Opportunity>();
Map<ID,ID> mapFinalOutputOpp = new Map<ID,ID>();
Map<Id,String[]> mapOppInputs = new Map<ID,String[]>();
String AgentIds = Label.Agent_name_for_rerouting;
List<String> StrAgentIDLst= AgentIds.split(',', 5);
system.debug('agent lst'+StrAgentIDLst);
List<String> AgentIDLst = new list<String>();
// getting the strings from the label
for(String s:StrAgentIDLst){
system.debug('individual agent lst'+s);
//String AgentID = Id.valueOf(s);
AgentIDLst.add(s);
}
//fetching the opportunity
date d = system.today().addDays(-7);
system.debug('system date - 7'+d);
date d2 = system.today().addDays(-8);
system.debug('system date - 7'+d2);
for (String Ids:AgentIDLst){
List<Opportunity> lstopp = [Select ID, Inquiry_School__c,LeadSource,CampaignId,Inquiry_Degree__c from Opportunity where Owner.name =:Ids and CreatedDate >: d2 and CreatedDate <: d and StageName = 'Lead'];
setopp.addAll(lstopp);
lstopp=null;
}
system.debug('setopp size'+setopp.size());
system.debug('lstopp'+setopp );

 

Please let me know.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Hseth-frondeHseth-fronde

I tried executing the query you have mentioned and it worked fien. No isses with the createdate field. The portantial issue I can see in your code is SOQL in a for loop which might result in you hitting the governor limitis if hte loop list contains alot of records. 

Having said that I suggest you run the query in anonymous execution block and verify your self that there is no issue with the query as such. 

All Answers

Hseth-frondeHseth-fronde

I tried executing the query you have mentioned and it worked fien. No isses with the createdate field. The portantial issue I can see in your code is SOQL in a for loop which might result in you hitting the governor limitis if hte loop list contains alot of records. 

Having said that I suggest you run the query in anonymous execution block and verify your self that there is no issue with the query as such. 

This was selected as the best answer
riffindusriffindus

Hi,

 

thanks for the reply.

 

When i run the code in anonymous mode i don't get any opportunity in the list. wherein i know there are opportunities available with same created date.

 

the issue if i run in query editor is, 

 

the system date is return as 2013-11-27 00:00:00 (got it from the system debug) but Query accepts the format of 2013-11-27T00:00:00Z

 

This is where the query is not retrieving any records.

 

 

 

 

riffindusriffindus

Hi,

 

you are correct. the query is working fine. 

 

I made a mistake with dates. Instead of adding -6, i added -7 to system date, so the query didnt give me any opportunity created with that date.

 

Thanks.

Hseth-frondeHseth-fronde

The issue with dateformat which you mentioned will only be there in case you are using dynamic queries. If in future you encounter such an issue, I suggest you to look at my blog. 

 

http://himanshooseth1.blogspot.co.nz/2013/12/handling-datetime-field-in-dynamic.html