You need to sign in to do that
Don't have an account?
pavan kumar 177
How to setup test class for history related objects
I have a requirement to set up a scheduled batch that will run every day & calculate the duration between the last date of opportunity stage changes to the current date.
Batch Class:
- Duration >=25 & Duration <30(Send Email & update opportunity fields)
- Duration >30 (Update Stage,Other fields)
Batch Class:
global class StageAge implements Database.Batchable<sObject> { public static List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); public static id owea = [select Id,address from OrgWideEmailAddress where Address = 'info@a.com' Limit 1].id; public Static Id Inboundtemplate = [select id from EmailTemplate where name ='Handle CloseDate' Limit 1].id; global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator('Select id from opportunity where isclosed=false AND Team__c=\'Bangalore Team\''+ 'AND StageName != \'Nurture\''); } global void execute(Database.BatchableContext bc, List<Opportunity> OpportunityList) { Map<Id,Opportunity> UserDetails = new Map<Id,Opportunity>(); If(OpportunityList <> null && !OpportunityList.isEmpty()) { for(opportunity opps:[Select id,ownerid,name,Owner.FirstName,Owner.LastName from opportunity where id in:OpportunityList]) { UserDetails.put(opps.id,opps); } } System.debug('Details Opportunity'+OpportunityList.size()); Map<Id,Integer> Opportunityids = new Map<Id,Integer>(); List<Opportunity> Nurtureupdate = new List<Opportunity>(); Map <Id,OpportunityHistory> LastStage =new Map <Id,OpportunityHistory>(); For(OpportunityHistory opps:[Select id,OpportunityId,CreatedDate from OpportunityHistory where OpportunityId IN:OpportunityList ORDER BY CreatedDate DESC NULLS First]) { If(!LastStage.containsKey(opps.OpportunityId)) { System.debug('Inside Loop'+opps.OpportunityId); System.debug('Create date '+opps.CreatedDate); LastStage.put(opps.OpportunityId,opps); Integer Duration=0; //Datetime MainCreateddate = opps.CreatedDate; Duration=opps.CreatedDate.Date().daysBetween(Date.today()); System.debug('Duration Details'+Duration); If(Duration >= 25 && Duration < 30) { System.debug('Inside Duration'); EmailDispatcher(opps.OpportunityId,UserDetails.get(opps.OpportunityId).ownerid,UserDetails.get(opps.OpportunityId).name ,UserDetails.get(opps.OpportunityId).Owner.FirstName,UserDetails.get(opps.OpportunityId).Owner.Lastname); Opportunityids.put(opps.OpportunityId,Duration); } If(Duration >30) { Opportunityids.put(opps.OpportunityId,Duration); } } } if(mails!=null && !mails.isEmpty()) { System.debug('Its working'); Messaging.sendEmail(mails); } If(Opportunityids <> null && !Opportunityids.isEmpty()) { For(Opportunity oppupdate:[Select id,stagename from opportunity where id in:Opportunityids.keyset()]) { System.debug('Opportunity_Duration_Before_Check'+Opportunityids.get(oppupdate.id)); If(Opportunityids.get(oppupdate.id) >30) { Opportunity Oppaddition = new Opportunity(); Oppaddition.id = oppupdate.id; Oppaddition.stagename = 'Nurture'; System.debug('******Opportunity>30'+Opportunityids.get(oppupdate.id)); Oppaddition.Stage_Age__c = Opportunityids.get(oppupdate.id); Nurtureupdate.add(oppaddition); } System.debug('**********Opportunity_Before_Check<30'+Opportunityids.get(oppupdate.id)); If(Opportunityids.get(oppupdate.id) >= 25 && Opportunityids.get(oppupdate.id) < 30) { Opportunity Oppaddition = new Opportunity(); Oppaddition.id = oppupdate.id; System.debug('**********Opportunity<30'+Opportunityids.get(oppupdate.id)); Oppaddition.Stage_Age__c = Opportunityids.get(oppupdate.id); Nurtureupdate.add(oppaddition); } } If(Nurtureupdate <> null && !Nurtureupdate.isEmpty()) { update Nurtureupdate; } } } Public Static void EmailDispatcher(Id OpportunityDetails,Id Target,String Opportunityname,String Firstname,String Lastname) { System.debug('Target details'+Target); System.debug('Inside Dispatcher'); System.debug('OpportunityDetails'+OpportunityDetails); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); //mail.settemplateid(Inboundtemplate); mail.setTargetObjectId(Target); mail.setTreatTargetObjectAsRecipient(true); //mail.setToAddresses(new String[] {'toml@a.com'}); mail.setOrgWideEmailAddressId(owea); mail.setUseSignature(false); mail.setSaveAsActivity(false); mail.setReplyTo('marketing@a.com'); mail.setsubject('Update CloseDate of Opportunity Name '+Opportunityname); String body = 'Hi '+Firstname+' '+Lastname+','+'<br/>'+'<br/>'; body += 'Opportunity Name '+Opportunityname+'<br/>'; body += 'Have we closed this one? Please update the Close date accordingly.'+'<br/>'; body += 'For More details'+'<a href="'+OpportunityDetails+'">'+'Click Here'+'</a>'+'<br/>'+'<br/>'; body += 'Thanks you again'+'<br/>'; body += 'Team Azuga'+'<br/>'+'<br/>'; mail.setHtmlBody(body); mails.add(mail); } global void finish(Database.BatchableContext bc) { } }
Hi Pavan,
Here's a good post that should help you - https://salesforce.stackexchange.com/questions/4007/is-it-possible-to-test-apex-that-relies-on-field-history-tracking
Best,
Vishal