+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Please help me to reach higher levels of test coverage. Not sure how to test for certain things.

Hi There,

I have an extension and a test class. I have always managed to just scrape by with my tests, despite not always reaching 100%. This extension I have created, I recently added the ability to add an event in a different timezone, however upon testing it, it only passes at 54%. I have tested all the major page references, but I think it is failing as I am not sure how to set the strings and all the gets. This is just hypothesis. Any help would be much appreciated in completing my test class.

 

static testMethod void TestExtSaveEveButton(){


Profile Profile1 = [select id from profile where name='System Administrator'];
Account acc = new Account(Name = 'Test Eve Account');
insert acc;

User use = new User(alias = 'ceo', email='admin@testorg.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', 
        timezonesidkey='America/Los_Angeles', username='adminEve@testorg.com', profileid = Profile1.Id);
insert use;

//Now lets create Destiny Products record that will be reference for the Standard Account
        Event EveTest = new Event (WhatId = acc.id, OwnerId = use.id, subject = 'call', StartDateTime = System.now(), TimeZone_Different__c = False, EndDateTime = System.now()+3);
        Event entTest = new Event (WhatId = acc.id, OwnerId = use.id, subject = 'call', TimeZone_Different__c = True, Contact_TimeZone__c = 'Australia/Brisbane', StartDateTime = System.now(), EndDateTime = System.now()+3);
        
        //call the apepages stad controller
        Apexpages.Standardcontroller stdEve = new Apexpages.Standardcontroller(EveTest);
        ApexPages.currentPage().getParameters().put('clone', '1');

//now call the class and reference the standardcontroller in the class.
        ExtSaveEveButton extEve = new ExtSaveEveButton(stdEve);

//call the pageReference in the class.
        extEve.eve.IsReminderSet = true;
        string hours = '11';
        
        extEve.getTimes();
        extEve.getHours();
        extEve.getMins();
        extEve.getAMPM();
        
        extEve.getStartTime();
        extEve.getStartHours();
        extEve.getStartMins();
        extEve.getStartAMPM();
        extEve.getStartDate();
        extEve.getEndTime();
        extEve.getEndHours();
        extEve.getEndMins();
        extEve.getEndAMPM();
        extEve.getEndDate();
        extEve.getReminderTime();
        extEve.getReminderHours();
        extEve.getReminderMins();
        extEve.getReminderAMPM();
        extEve.getReminderDate();
        extEve.saveDestinyEvent();
        extEve.cancelDestinyEvent();
        extEve.saveSendFollowUpConfirmEmail();
        extEve.saveSendAppConfirmEmail();
        extEve.saveSendAppConfirmEmailVirtual();
        extEve.saveSerDecisionYesEPE();
        extEve.saveSendServiceDecisionNoEmail();
         extEve.saveNewDestinyEvent();
         extEve.saveSendEmail();
    

       
        
        Apexpages.Standardcontroller stdent = new Apexpages.Standardcontroller(entTest);
         extSaveEveButton extent = new extSaveEveButton (stdent);
         extent.eve.IsReminderSet = true;
                 extEve.getTimes();
        extent.getHours();
        extent.getMins();
        extent.getAMPM();
        extent.getStartTime();
        extent.getStartHours();
        extent.getStartMins();
        extent.getStartAMPM();
        extent.getStartDate();
        extent.getEndTime();
        extent.getEndHours();
        extent.getEndMins();
        extent.getEndAMPM();
        extent.getEndDate();
        extent.getReminderTime();
        extent.getReminderHours();
        extent.getReminderMins();
        extent.getReminderAMPM();
        extent.getReminderDate();
        extent.saveDestinyEvent();
        extent.cancelDestinyEvent();
         extent.saveNewDestinyEvent();
         extent.saveSendEmail();
         extent.saveSendAppConfirmEmail();
         extent.saveSendAppConfirmEmailVirtual();
         extent.saveSendFollowUpConfirmEmail();
          extent.saveSendServiceDecisionNoEmail();
          extent.saveSerDecisionYesEPE();
     
}

and my extension in the next post
 
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Public class extSaveEveButton {


public Event Eve {get;set;}

//TimeZone
TimeZone UserTz = UserInfo.getTimeZone();
String TimeZoneString = string.valueof(UserTz);

Datetime mydate = System.now();

String S = mydate.format('dd/MM/yyyy');
String T = mydate.format(' hh:mm aa');
String H = T.substring(1, 3);
String M = T.substring(4, 6);
String AMPM = T.substring(7, 9);


//Introduce strings and set format
//Start
String st = T;
String sh = H;
String sm = M;
String sAMPM = AMPM;
String sd = S;
//End
String et = T;
String eh = H;
String em = M;
String eAMPM = AMPM;
String ed = S;
//Reminder
String rt = T;
String rh = H;
String rm = M;
String rAMPM = AMPM;
String rd = S;
//Follow-Up
String ft = T;
String fh = H;
String fm = M;
String fAMPM = AMPM;
String fd = S;

//Sets Standard Controller
public extSaveEveButton (ApexPages.StandardController controller) {
this.Eve = (Event)controller.getRecord();
Eve.Contact_TimeZone__c = TimeZoneString;


}

//Retrieve instance of strings within VF page
//Start
//Time
//Hours
public String getStartHours(){return sh;}
public void setStartHours(String sh) {this.sh = sh;}
//Mins
public String getStartMins(){return sm;}
public void setStartMins(String sm) {this.sm = sm;}
//AMPM
public String getStartAMPM(){return sAMPM;}
public void setStartAMPM(String sAMPM) {this.sAMPM = sAMPM;}
//Time
public String getStartTime(){return st;}
public void setStartTime(String st) {this.st = st;}
//Date
public String getStartDate() {return sd;}
public void setStartDate(String sd) {this.sd = sd;}

//End
//Hours
public String getEndHours(){return eh;}
public void setEndHours(String eh) {this.eh = eh;}
//Mins
public String getEndMins(){return em;}
public void setEndMins(String em) {this.em = em;}
//AMPM
public String getEndAMPM(){return eAMPM;}
public void setEndAMPM(String eAMPM) {this.eAMPM = eAMPM;}
//Time
public String getEndTime(){return et;}
public void setEndTime(String et) {this.et = et;}
//Date
public String getEndDate() {return ed;}
public void setEndDate(String ed) {this.ed = ed;}

//Reminder
//Hours
public String getReminderHours(){return rh;}
public void setReminderHours(String rh) {this.rh = rh;}
//Mins
public String getReminderMins(){return rm;}
public void setReminderMins(String rm) {this.rm = rm;}
//AMPM
public String getReminderAMPM(){return rAMPM;}
public void setReminderAMPM(String rAMPM) {this.rAMPM = rAMPM;}
//Time
public String getReminderTime(){return rt;}
public void setReminderTime(String rt) {this.rt = rt;}
//Date
public String getReminderDate() {return rd;}
public void setReminderDate(String rd) {this.rd = rd;}

//Follow Up
//Hours
public String getFollowHours(){return fh;}
public void setFollowHours(String fh) {this.fh = fh;}
//Mins
public String getFollowMins(){return fm;}
public void setFollowMins(String fm) {this.fm = fm;}
//AMPM
public String getFollowAMPM(){return fAMPM;}
public void setFollowAMPM(String fAMPM) {this.fAMPM = fAMPM;}
//Time
public String getFollowTime(){return ft;}
public void setFollowTime(String ft) {this.ft = ft;}
//Date
public String getFollowDate() {return fd;}
public void setFollowDate(String fd) {this.fd = fd;}



//Save Button
Public PageReference saveDestinyEvent(){

//Clone Situation
if(System.currentPageReference().getParameters().get('clone') == '1'){
//Set record id to null
Eve.Id = null;
}


//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}
//TimeZone
if(Eve.TimeZone_Different__c == true && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){

//Find TimeZones and get offsets
string ClientTimeZone = String.valueof(Eve.Contact_TimeZone__c);
Timezone ClientTz = Timezone.getTimeZone(ClientTimeZone);
Integer ClientOffset = ClientTz.getOffset(Eve.StartDateTime);
ClientOffset = ClientOffset/1000;
Integer UserOffset = UserTz.getOffset(Eve.StartDateTime);
UserOffset = UserOffset/1000;

//Set Client Start Date and End Date


DateTime ClientNewStartDatetime = Eve.StartDateTime;
Datetime ClientNewEndDatetime = Eve.EndDateTime;
Eve.Client_TimeZone_StartDateTime__c = ClientNewStartDatetime;
Eve.Client_TimeZone_EndDateTime__c = ClientNewEndDatetime;

//Convert Event start and End time to Users timezone.
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(-(ClientOffset));
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(-(ClientOffset));
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(UserOffset);
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(UserOffset);
Eve.StartDateTime = ClientNewStartDatetime;
Eve.EndDateTime = ClientNewEndDatetime;
if(Eve.Description == null){
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c);
}else{
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c)+'\n'+Eve.Description;
}


}else if(Eve.TimeZone_Different__c == false  && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){
Eve.Client_TimeZone_StartDateTime__c = Eve.StartDateTime;
Eve.Client_TimeZone_EndDateTime__c = Eve.EndDateTime;
}

try {     
Upsert Eve;
// Send the user to the detail page for the new account.
PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+Eve.whatid+'&Sfdc.override=1');
pageRef.getParameters().put('tab','Activities');
return pageRef;
 } catch (Exception e) {     
      
             ApexPages.addMessages(e);      
        }
        return null;

}

/
//Service Decision Yes
  Public PageReference saveSendEmail(){

//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}
//TimeZone
if(Eve.TimeZone_Different__c == true && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){

//Find TimeZones and get offsets
string ClientTimeZone = String.valueof(Eve.Contact_TimeZone__c);
Timezone ClientTz = Timezone.getTimeZone(ClientTimeZone);
Integer ClientOffset = ClientTz.getOffset(Eve.StartDateTime);
ClientOffset = ClientOffset/1000;
Integer UserOffset = UserTz.getOffset(Eve.StartDateTime);
UserOffset = UserOffset/1000;

//Set Client Start Date and End Date
DateTime ClientNewStartDatetime = Eve.StartDateTime;
Datetime ClientNewEndDatetime = Eve.EndDateTime;
Eve.Client_TimeZone_StartDateTime__c = ClientNewStartDatetime;
Eve.Client_TimeZone_EndDateTime__c = ClientNewEndDatetime;

//Convert Event start and End time to Users timezone.
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(-(ClientOffset));
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(-(ClientOffset));
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(UserOffset);
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(UserOffset);
Eve.StartDateTime = ClientNewStartDatetime;
Eve.EndDateTime = ClientNewEndDatetime;
if(Eve.Description == null){
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c);
}else{
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c)+'\n'+Eve.Description;
}


}else if(Eve.TimeZone_Different__c == false  && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){
Eve.Client_TimeZone_StartDateTime__c = Eve.StartDateTime;
Eve.Client_TimeZone_EndDateTime__c = Eve.EndDateTime;
}


List <Event_Task_Email__c> ETEmailList = new List <Event_Task_Email__c> ();
List <Id> AccIds = new List <Id> ();
Datetime NameDate = Datetime.now();
String StringNameDate = NameDate.format('yyyy-MM-dd');
String RefName = 'Event-Email#'+StringNameDate;
   
   if (Eve.Whatid != null ) { 

        Event_Task_Email__c ETEmail = new Event_Task_Email__c (); //instantiate the object to put values for future record       
        // now map Service fields to new vehicle object that is being created with this Trigger
        
        ETEmail.Associated_Account__c = Eve.Whatid; // and so on so forth untill you map all the fields. 
      

        
        AccIds.add(Eve.Whatid);
        ETEmailList.add(ETEmail);
     
        
       } 
        

    try {
        insert ETEmailList;
  
        
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
ETEmailList = [select id,Associated_Account__c,Event_Service__c from Event_Task_Email__c where Associated_Account__c in : AccIds order by CreatedDate];


 for (Event_Task_Email__c Elist : ETEmailList ) {
 

  if(Elist.Event_Service__c == Eve.Service_Name__c && Elist.Associated_Account__c == Eve.Whatid){
    Eve.Email_Link__c = Elist.Id;
    
 }  
 }
  

    try {     
        upsert Eve;
        // Send the user to the detail page for the new account.
      PageReference pageRef= new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+Eve.Email_Link__c+'&p2_lkid='+Eve.whoid+'&template_id=00X90000001Fu7B&retURL=/apex/DestinyAccount?id='+Eve.whatid+'&cancelURL={!$CurrentPage.Url}');

        return pageRef;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;
    
    }
    
    
    
    
    ///////////////////////////////Service Decision Yes EPE\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    Public PageReference saveSerDecisionYesEPE(){

//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}
//TimeZone
if(Eve.TimeZone_Different__c == true && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){

//Find TimeZones and get offsets
string ClientTimeZone = String.valueof(Eve.Contact_TimeZone__c);
Timezone ClientTz = Timezone.getTimeZone(ClientTimeZone);
Integer ClientOffset = ClientTz.getOffset(Eve.StartDateTime);
ClientOffset = ClientOffset/1000;
Integer UserOffset = UserTz.getOffset(Eve.StartDateTime);
UserOffset = UserOffset/1000;

//Set Client Start Date and End Date
DateTime ClientNewStartDatetime = Eve.StartDateTime;
Datetime ClientNewEndDatetime = Eve.EndDateTime;
Eve.Client_TimeZone_StartDateTime__c = ClientNewStartDatetime;
Eve.Client_TimeZone_EndDateTime__c = ClientNewEndDatetime;

//Convert Event start and End time to Users timezone.
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(-(ClientOffset));
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(-(ClientOffset));
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(UserOffset);
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(UserOffset);
Eve.StartDateTime = ClientNewStartDatetime;
Eve.EndDateTime = ClientNewEndDatetime;
if(Eve.Description == null){
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c);
}else{
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c)+'\n'+Eve.Description;
}


}else if(Eve.TimeZone_Different__c == false  && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){
Eve.Client_TimeZone_StartDateTime__c = Eve.StartDateTime;
Eve.Client_TimeZone_EndDateTime__c = Eve.EndDateTime;
}


List <Event_Task_Email__c> ETEmailList = new List <Event_Task_Email__c> ();
List <Id> AccIds = new List <Id> ();
Datetime NameDate = Datetime.now();
String StringNameDate = NameDate.format('yyyy-MM-dd');
String RefName = 'Event-Email#'+StringNameDate;
   
   if (Eve.Whatid != null ) { 

        Event_Task_Email__c ETEmail = new Event_Task_Email__c (); //instantiate the object to put values for future record       
        // now map Service fields to new vehicle object that is being created with this Trigger
        
        ETEmail.Associated_Account__c = Eve.Whatid; // and so on so forth untill you map all the fields. 
        ETEmail.Event_Service__c = Eve.Service_Name__c;

        
        AccIds.add(Eve.Whatid);
        ETEmailList.add(ETEmail);
     
        
       } 
        

    try {
        insert ETEmailList;
  
        
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
ETEmailList = [select id,Associated_Account__c,Event_Service__c from Event_Task_Email__c where Associated_Account__c in : AccIds order by CreatedDate];


 for (Event_Task_Email__c Elist : ETEmailList ) {
 

  if(Elist.Event_Service__c == Eve.Service_Name__c && Elist.Associated_Account__c == Eve.Whatid){
    Eve.Email_Link__c = Elist.Id;
    
 }  
 }
  

    try {     
        upsert Eve;
        // Send the user to the detail page for the new account.
      PageReference pageRef= new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+Eve.Email_Link__c+'&p2_lkid='+Eve.whoid+'&template_id=00X90000001FuZK&retURL=/apex/DestinyAccount?id='+Eve.whatid+'&cancelURL={!$CurrentPage.Url}');

        return pageRef;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;
    
    }
    
Public PageReference saveSendAppConfirmEmail(){

//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}
//TimeZone
if(Eve.TimeZone_Different__c == true && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){

//Find TimeZones and get offsets
string ClientTimeZone = String.valueof(Eve.Contact_TimeZone__c);
Timezone ClientTz = Timezone.getTimeZone(ClientTimeZone);
Integer ClientOffset = ClientTz.getOffset(Eve.StartDateTime);
ClientOffset = ClientOffset/1000;
Integer UserOffset = UserTz.getOffset(Eve.StartDateTime);
UserOffset = UserOffset/1000;

//Set Client Start Date and End Date
DateTime ClientNewStartDatetime = Eve.StartDateTime;
Datetime ClientNewEndDatetime = Eve.EndDateTime;
Eve.Client_TimeZone_StartDateTime__c = ClientNewStartDatetime;
Eve.Client_TimeZone_EndDateTime__c = ClientNewEndDatetime;

//Convert Event start and End time to Users timezone.
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(-(ClientOffset));
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(-(ClientOffset));
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(UserOffset);
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(UserOffset);
Eve.StartDateTime = ClientNewStartDatetime;
Eve.EndDateTime = ClientNewEndDatetime;
if(Eve.Description == null){
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c);
}else{
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c)+'\n'+Eve.Description;
}


}else if(Eve.TimeZone_Different__c == false  && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){
Eve.Client_TimeZone_StartDateTime__c = Eve.StartDateTime;
Eve.Client_TimeZone_EndDateTime__c = Eve.EndDateTime;
}


List <Event_Task_Email__c> ETEmailList = new List <Event_Task_Email__c> ();
List <Id> AccIds = new List <Id> ();
Datetime NameDate = Datetime.now();
String StringNameDate = NameDate.format('yyyy-MM-dd');
String RefName = 'Event-Email#'+StringNameDate;

try {     
upsert Eve;
// Send the user to the detail page for the new account. New template Id Added. HTML CHanged
} catch (Exception e) {     
ApexPages.addMessages(e);         
}


   if (Eve.Whatid != null ) { 

        Event_Task_Email__c ETEmail = new Event_Task_Email__c (); //instantiate the object to put values for future record       
        // now map Service fields to new vehicle object that is being created with this Trigger
        
        ETEmail.Associated_Account__c = Eve.Whatid; // and so on so forth untill you map all the fields. 
        ETEmail.Event_StartDateTime__c = Eve.StartDateTime;
        ETEmail.Event_Agent_Name__c = Eve.Agent_Name__c;
        
        AccIds.add(Eve.Whatid);
        ETEmailList.add(ETEmail);
      }  

    try {
        insert ETEmailList;
  
        
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
ETEmailList = [select id,Associated_Account__c,Event_StartDateTime__c from Event_Task_Email__c where Associated_Account__c in : AccIds order by CreatedDate];


 for (Event_Task_Email__c Elist : ETEmailList ) {
 

  if(Elist.Event_StartDateTime__c == Eve.startDateTime && Elist.Associated_Account__c == Eve.Whatid){
    Eve.Email_Link__c = Elist.Id;
    
 }  
 }
  

    try {     
        upsert Eve;
        // Send the user to the detail page for the new account. New template Id Added. HTML CHanged
      PageReference pageRef= new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+Eve.Email_Link__c+'&p2_lkid='+Eve.whoid+'&template_id=00X90000001Fu7Q&retURL=/apex/DestinyAccount?id='+Eve.whatid+'&cancelURL={!$CurrentPage.Url}');
        return pageRef;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;
    
    } 
    
    
    //////////////////Appointment confirmation Email Virtual\\\\\\\\\\\\\\\\\\\\\\\\\\\
    Public PageReference saveSendAppConfirmEmailVirtual(){

//Set Start and End Datetime
if(Eve.StartDateTime == null){
String S = sd+' '+this.sh+':'+this.sm+' '+this.sAMPM;
Eve.StartDateTime = DateTime.parse(S);
}
if(Eve.EndDateTime == null){
String EdEt = ed+' '+this.eh+':'+this.em+' '+this.eAMPM;
Eve.EndDateTime = DateTime.parse(EdEt);
}
if((Eve.IsReminderSet == true)&&(Eve.ReminderDateTime == null)){
String R = this.rd+' '+this.rh+':'+this.rm+' '+this.rAMPM;
Eve.ReminderDateTime= DateTime.parse(R);
}
if(Eve.Destiny_Service_Decision__c=='Follow Up'){
String F = this.fd+' '+this.fh+':'+this.fm+' '+this.fAMPM;
String FoD = this.fd;
Eve.Follow_Up_DateTime__c = DateTime.parse(F);
Eve.Follow_Up_Date__c = Date.parse(FoD);
Eve.Follow_Up_Time__c = this.ft;
}
//TimeZone
if(Eve.TimeZone_Different__c == true && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){

//Find TimeZones and get offsets
string ClientTimeZone = String.valueof(Eve.Contact_TimeZone__c);
Timezone ClientTz = Timezone.getTimeZone(ClientTimeZone);
Integer ClientOffset = ClientTz.getOffset(Eve.StartDateTime);
ClientOffset = ClientOffset/1000;
Integer UserOffset = UserTz.getOffset(Eve.StartDateTime);
UserOffset = UserOffset/1000;

//Set Client Start Date and End Date
DateTime ClientNewStartDatetime = Eve.StartDateTime;
Datetime ClientNewEndDatetime = Eve.EndDateTime;
Eve.Client_TimeZone_StartDateTime__c = ClientNewStartDatetime;
Eve.Client_TimeZone_EndDateTime__c = ClientNewEndDatetime;

//Convert Event start and End time to Users timezone.
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(-(ClientOffset));
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(-(ClientOffset));
ClientNewStartDatetime = ClientNewStartDatetime.addSeconds(UserOffset);
ClientNewEndDatetime = ClientNewEndDatetime.addSeconds(UserOffset);
Eve.StartDateTime = ClientNewStartDatetime;
Eve.EndDateTime = ClientNewEndDatetime;
if(Eve.Description == null){
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c);
}else{
Eve.Description = 'Local Start Time: '+String.valueof(Eve.Client_TimeZone_StartDateTime__c)+'\n'+'Local End Time: '+string.valueof(Eve.Client_TimeZone_EndDateTime__c)+'\n'+Eve.Description;
}


}else if(Eve.TimeZone_Different__c == false  && Eve.Client_TimeZone_StartDateTime__c == null && Eve.Client_TimeZone_EndDateTime__c == null){
Eve.Client_TimeZone_StartDateTime__c = Eve.StartDateTime;
Eve.Client_TimeZone_EndDateTime__c = Eve.EndDateTime;
}


List <Event_Task_Email__c> ETEmailList = new List <Event_Task_Email__c> ();
List <Id> AccIds = new List <Id> ();
Datetime NameDate = Datetime.now();
String StringNameDate = NameDate.format('yyyy-MM-dd');
String RefName = 'Event-Email#'+StringNameDate;

try {     
upsert Eve;
// Send the user to the detail page for the new account. New template Id Added. HTML CHanged
} catch (Exception e) {     
ApexPages.addMessages(e);         
}
    
 

   if (Eve.Whatid != null ) { 

        Event_Task_Email__c ETEmail = new Event_Task_Email__c (); //instantiate the object to put values for future record       
        // now map Service fields to new vehicle object that is being created with this Trigger
        
        ETEmail.Associated_Account__c = Eve.Whatid; // and so on so forth untill you map all the fields. 
        ETEmail.Event_StartDateTime__c = Eve.StartDateTime;
        ETEmail.Event_Agent_Name__c = Eve.Agent_Name__c;
        
        AccIds.add(Eve.Whatid);
        ETEmailList.add(ETEmail);
      }  

    try {
        insert ETEmailList;
  
        
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
ETEmailList = [select id,Associated_Account__c,Event_StartDateTime__c from Event_Task_Email__c where Associated_Account__c in : AccIds order by CreatedDate];


 for (Event_Task_Email__c Elist : ETEmailList ) {
 

  if(Elist.Event_StartDateTime__c == Eve.startDateTime && Elist.Associated_Account__c == Eve.Whatid){
    Eve.Email_Link__c = Elist.Id;
    
 }  
 }
  

    try {     
        upsert Eve;
        // Send the user to the detail page for the new account. New template Id Added. HTML CHanged
      PageReference pageRef= new PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+Eve.Email_Link__c+'&p2_lkid='+Eve.whoid+'&template_id=00X90000001FuZZ&retURL=/apex/DestinyAccount?id='+Eve.whatid+'&cancelURL={!$CurrentPage.Url}');
        return pageRef;
        
        } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;
    
    } 
   

///////////////////////////////////////////////////Time tool\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Time SelectList
public list<selectoption> getTimes()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(T,T));
      options.add(new SelectOption('12:00 PM','12:00 PM'));
      options.add(new SelectOption('1:00 PM','1:00 PM'));
      options.add(new SelectOption('2:00 PM','2:00 PM'));
      options.add(new SelectOption('3:00 PM','3:00 PM'));
      options.add(new SelectOption('4:00 PM','4:00 PM'));
      options.add(new SelectOption('5:00 PM','5:00 PM'));
      options.add(new SelectOption('6:00 PM','6:00 PM'));
      options.add(new SelectOption('7:00 PM','7:00 PM'));
      options.add(new SelectOption('8:00 PM','8:00 PM'));
      options.add(new SelectOption('9:00 PM','9:00 PM'));
      options.add(new SelectOption('10:00 PM','10:00 PM'));
      options.add(new SelectOption('11:00 PM','11:00 PM'));
      options.add(new SelectOption('12:00 AM','12:00 AM'));
      options.add(new SelectOption('1:00 AM','1:00 AM'));
      options.add(new SelectOption('2:00 AM','2:00 AM'));
      options.add(new SelectOption('3:00 AM','3:00 AM'));
      options.add(new SelectOption('4:00 AM','4:00 AM'));
      options.add(new SelectOption('5:00 AM','5:00 AM'));
      options.add(new SelectOption('6:00 AM','6:00 AM'));
      options.add(new SelectOption('7:00 AM','7:00 AM'));
      options.add(new SelectOption('8:00 AM','8:00 AM'));
      options.add(new SelectOption('9:00 AM','9:00 AM'));
      options.add(new SelectOption('10:00 AM','10:00 AM'));
      options.add(new SelectOption('11:00 AM','11:00 AM'));
      options.add(new SelectOption('12:00 AM','12:00 AM'));

   return options;
 }
 
 public list<selectoption> getHours()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption(H,H));
      options.add(new SelectOption('12','12'));
      options.add(new SelectOption('1','1'));
      options.add(new SelectOption('2','2'));
      options.add(new SelectOption('3','3'));
      options.add(new SelectOption('4','4'));
      options.add(new SelectOption('5','5'));
      options.add(new SelectOption('6','6'));
      options.add(new SelectOption('7','7'));
      options.add(new SelectOption('8','8'));
      options.add(new SelectOption('9','9'));
      options.add(new SelectOption('10','10'));
      options.add(new SelectOption('11','11'));
   return options;
 }  
 
 public list<selectoption> getMins()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('00','00'));
      options.add(new SelectOption('00','00'));
      options.add(new SelectOption('05','05'));
      options.add(new SelectOption('10','10'));
      options.add(new SelectOption('15','15'));
      options.add(new SelectOption('20','20'));
      options.add(new SelectOption('25','25'));
      options.add(new SelectOption('30','30'));
      options.add(new SelectOption('30','30'));
      options.add(new SelectOption('35','35'));
      options.add(new SelectOption('40','40'));
      options.add(new SelectOption('45','45'));
      options.add(new SelectOption('50','50'));
      options.add(new SelectOption('55','55'));
   
   return options;
 }    
 
  public list<selectoption> getAMPM()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('PM','PM'));
      options.add(new SelectOption('AM','AM')); 
   return options;
 } 
///////////////////////////////////////////////////Time tool\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 

}