You need to sign in to do that
Don't have an account?

Route ONLY after 10:30am?? - Please help!
Hi Guys,
Apex class posted below is part of a system that routes calls to our users. It is all working perfectly but we recently needed to add the function where it only returned records that:
1) Any state but WA/Western Australia, any time
2) If in the state of WA/Western Australia (Field-wise this is "Inferred State" or "State"); AND only after 10:30am USER (callers) time as they are 2 hours behind us
Everything works well, but I can't get (2) above to work! Please help! For our WA opportunties I simply want to only route these Opps ONLY after 10:30am User/Caller (not customer) time and not before for our WA/Western Australia opportunities!
I only have VERY limited Apex knowledge (in fact I am not a coder - this ws inherited from a past developer that is no longer in business), so PLEASE let me know what and where to post a change to make this work. Thank you soooo much in advance!
Code is below. Code I am trying to get to work isin BOLD text. Everything else but this time routing is working perfectly.
public with sharing class OpportunityFinder {
public PageReference GoToNextOpportunity(){
Id op_id = findNextOpportunity();
if( op_id == null ) return new Pagereference('/apex/No_Further_Opportunities');
PageReference p = new PageReference('/apex/OpportunityCallView?id=' + op_id);
p.setRedirect(true);
return p;
}
public static Id findNextOpportunity(){
Opportunity[] found;
Id user_id = UserInfo.getUserId();
// My Open opportunities where StageCount = 0
found = [SELECT Id FROM Opportunity WHERE IsClosed = false AND (datetime.now() > datetime.newInstance(Date.today().year(), Date.today().month(), Date.today().day(), 10, 30, 0)) AND (State__c = 'WA' OR Inferred_State__c = 'Western Australia')) AND (State__c != 'WA' OR Inferred_State__c != 'Western Australia')) AND (StageName != 'Following - No Phone - Email Only')) AND (Stage_Count__c = 0 OR Stage_Count__C = null) AND Call_Back_Date__c = null AND Last_Call__c != :Date.today() AND In_Queue__c = false AND OwnerId = :user_id ORDER BY Lead_Score_From_Contact__c DESC LIMIT 1];
if( found.size() > 0 ) return found[0].Id;
// My Open opportunities where Callback Date = today
found = [SELECT Id FROM Opportunity WHERE IsClosed = false AND (datetime.now() > datetime.newInstance(Date.today().year(), Date.today().month(), Date.today().day(), 10, 30, 0)) AND (State__c = 'WA' OR Inferred_State__c = 'Western Australia')) AND (State__c != 'WA' OR Inferred_State__c != 'Western Australia')) AND (StageName != 'Following - No Phone - Email Only') AND Call_Back_Date__c = :Date.today() AND Last_Call__c != :Date.today() AND In_Queue__c = false AND OwnerId = :user_id ORDER BY LastModifiedDate DESC LIMIT 1];
if( found.size() > 0 ) return found[0].Id;
// My Open opportunities where StageCount = 1 AND Callback <= today
found = [SELECT Id FROM Opportunity WHERE IsClosed = false AND (datetime.now() > datetime.newInstance(Date.today().year(), Date.today().month(), Date.today().day(), 10, 30, 0)) AND (State__c = 'WA' OR Inferred_State__c = 'Western Australia')) AND (State__c != 'WA' OR Inferred_State__c != 'Western Australia')) AND (StageName != 'Following - No Phone - Email Only') AND Stage_Count__c = 1 AND Call_Back_Date__c <= :Date.today() AND Last_Call__c != :Date.today() AND In_Queue__c = false AND OwnerId = :user_id ORDER BY Lead_Score_From_Contact__c DESC LIMIT 1];
if( found.size() > 0 ) return found[0].Id;
return null;
}
}
try to initiate the date instance in GMT time zone:
datetime myDate = datetime.newInstanceGmt(Date.today().year(), Date.today().month(), Date.today().day(), 10, 30, 0);
hope this helps ;)