Sunday, 2 September 2018

Date Time conversion


https://developer.salesforce.com/forums/?id=906F0000000BCLdIAO
https://salesforce.stackexchange.com/questions/15098/how-to-convert-a-text-to-datetime

Datetime myDate = datetime.valueOf(stringDate);


Map <String, Integer> monthNames = new Map <String, Integer> {'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12};
List <String> stringParts = 'Wed Aug 07 04:30:00 GMT 2013'.split(' ');
List <String> timeParts = stringParts[3].split(':');

DateTime yourDateVariable = DateTime.newInstanceGmt(Integer.valueOf(stringParts[5]), monthNames.get(stringParts[1]), Integer.valueOf(stringParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));
System.debug('yourDateVariable is'+yourDateVariable);
DateTime yourDateVariable2= yourDateVariable.addHours(24);
System.debug('yourDateVariable2 is'+yourDateVariable2);

++++++++++
https://developer.salesforce.com/forums/?id=906F00000008ywsIAA

++++++++++

public with sharing class AutomatedTestSetup_Controller {

    public AutomatedTestSetup_Controller(ApexPages.StandardController controller) {
        refreshHasScheduledJobs();
        refreshHasTestRun();
    }
 
    public boolean hasScheduledJobs { get; set; }
    public boolean hasTestRun { get; set; }
    public boolean isRunning { get; set; }
    public boolean isEmailing { get; set; }
    public AutomatedTestSetup_Controller ATS { get; set; }
    public String DtValue {get; set; }
    public String Staticstics;
 
    public void refreshHasScheduledJobs() {
        integer jobCount = [select count() from CronJobDetail where Name like 'AutoTestRunner%' and JobType = '7'];
        hasScheduledJobs = jobCount == 1;     
    }
 
    public void refreshHasTestRun() {
        integer runCount = [select count() from Automated_Test_Run__c limit 1];
        hasTestRun = runCount > 0;   
    }
 
    public string getTestRunLink() {
        return '/' + Automated_Test_Run__c.SObjectType.getDescribe().getKeyPrefix();
    }
 
    public AutomatedTestSetup_Controller() {
        refreshHasScheduledJobs();
        refreshHasTestRun();
    }
 
    public PageReference scheduleJobs() {
    //String InputDtValue = ApexPages.currentPage().getParameters().get('DtValue');
   // System.debug('Date & Time is given***'+InputDtValue);
    System.debug('Date & Time is given***1'+DtValue);
 
 
    if(DtValue!='null' && DtValue!='' ){
     //DateTime Schdtime = (DateTime)Json.deserialize('"'+DtValue+'"', DateTime.class);
     //DtValue Format: Sat Aug 25 17:12:00 GMT 2018
     //0 12 17 25 Aug 2018 Sat
    // 0 12 17 25 AUG ? 2017
    //  Seconds Minutes Hours   Day Of Month    Month   Day Of Week  Year
    //  0        12         17  25              AUG       ?          2017
     
        Map <String, Integer> monthNames = new Map <String, Integer> {'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12};
        List <String> stringParts = DtValue.split(' ');
        List <String> timeParts = stringParts[3].split(':');

        DateTime DTforEmail = DateTime.newInstanceGmt(Integer.valueOf(stringParts[5]), monthNames.get(stringParts[1]), Integer.valueOf(stringParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));
        //DTforEmail ***1-->2018-09-01 14:13:00
        System.debug('DTforEmail ***'+DTforEmail);

//Getting Locle Timezone
Datetime gmt = DTforEmail;
Datetime local = getLocalDateTime(gmt);
System.Debug('GMT Time: ' + gmt);
System.Debug('Local Time: ' + local);
Contact c=new Contact(LastName='Test1contact',New_Datetime__c=local); insert c;


        integer AutomatedEmailScheduleHours=integer.ValueOf(Label.AutomatedEmailScheduleHours);
        DateTime mailschedule= local.addHours(AutomatedEmailScheduleHours);

Contact c2=new Contact(LastName='Test2contact',New_Datetime__c=mailschedule); insert c2;
        //Datetime mailschedule =DTforEmail + (AutomatedEmailScheduleHours/24);
        System.debug('mailschedule ***'+mailschedule);
       
/***************
String[] splited = DtValue.split(' ');
        String day = splited[2];
        String month = splited[1]; 
        String HMS = splited[3];
        String[] splitedHMS = HMS.split(':');
        String hour = splitedHMS[0];
        String minute = splitedHMS[1];
        String second = splitedHMS[2];
        String year =splited[5];
        //String minute = string.valueOf(Schdtime.minute() + 1);
        //String year = string.valueOf(Schdtime.year());
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.debug('***strSchedule***'+strSchedule);

**************/
        //Creating email job Cronexpression
        Map <Integer,String> monthNumbers = new Map <Integer,String> {1=>'Jan', 2=>'Feb',3=>'Mar',4=>'Apr', 5=>'May', 6=>'Jun',7=>'Jul',8=>'Aug', 9=>'Sep', 10=>'Oct', 11=>'Nov', 12=>'Dec'};
         String TestShdmonth=monthNumbers.get(local.month());
String strSchedule = '0 ' + local.minute() + ' ' +local.hour()+ ' ' + local.day()+ ' ' + TestShdmonth+ ' ? ' + ' ' + local.year();
        System.debug('***strSchedule***'+strSchedule);


String emailmonth=monthNumbers.get(mailschedule.month());
       
        String strEmailSchedule = '0 ' + mailschedule.minute() + ' ' +mailschedule.hour()+ ' ' + mailschedule.day()+ ' ' + emailmonth+ ' ? ' + ' ' + mailschedule.year();
        System.debug('***strEmailSchedule***'+strEmailSchedule);
        //String DaTime=ApexPages.currentPage().getParameters().get('{$Automated_Test_Run__c.Schedule__c}');
        //System.debug('Date & Time is'+one);
        AutoTestRunner.setup(strSchedule,strEmailSchedule);
        refreshHasScheduledJobs();
       //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Jobs scheduled'));
     
    }
     else{
         ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please Select Date Time Value and then Select Submit Button to Schedule job');
              ApexPages.addMessage(myMsg);
     }
    RETURN NULL;
    }
    public PageReference CancelJobs() {
  //List<CronTrigger> jobsToAbort = [select id,Name from CronJobDetail where Name like 'AutoTestRunner%' and JobType = '7'];
         List<CronTrigger> jobsToAbort = [select Id from CronTrigger where CronJobDetail.JobType = '7' and CronJobDetail.Name like '%TestRunner%'];
  for (CronTrigger job : jobsToAbort) {
   System.abortJob(job.Id);
   }
   //PageReference pageRef = '/apex/AutomatedTestSetupVFP';
   PageReference pageRef = new PageReference('/apex/AutomatedTestSetupVFP');
        pageRef.setRedirect(true);
        return pageRef;
 }
    public void createTestRun() {
        User currentUser = [select Email from User where Id = :UserInfo.getUserId()];
        insert new Automated_Test_Run__c(Name = 'All Tests', Emails__c = currentUser.Email);
        refreshHasTestRun();
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Automated Test Run created'));
    }
 
    public void run() {
        AutoTestRunner.enqueueTests();
        isRunning = true;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Tests queued'));
    }
 
    public void email() {
        TestRunnerResults.emailUnprocessedJobStatus();
        isEmailing = true;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Results processed and emailed'));
    }
 
//Statistics

 public List<PieWedgeData> getPieData() {
  //Select id,Status from ApexTestQueueItem where ParentJobId='7070K00008HsU7EQAV'
  //Test_Runner_Job__c RunningJob =[Select Name,Automated_Test_Run__c,CreatedDate From Test_Runner_Job__c where Processed__c = false order by CreatedDate desc limit 1];
  Test_Runner_Job__c RunningJob =[Select Name,Automated_Test_Run__c,CreatedDate From Test_Runner_Job__c order by CreatedDate desc limit 1];
  //integer Cmpltd       = [select count() FROM ApexTestResult WHERE Outcome = 'Pass' and AsyncApexJobId=:RunningJob.Name];
  integer Cmpltd       = [select count() FROM ApexTestQueueItem where Status='Completed' and ParentJobId=:RunningJob.Name];
  integer Queuedjobs   = [select count() FROM ApexTestQueueItem where Status='Queued' and ParentJobId=:RunningJob.Name];
  Integer Tot=Cmpltd+Queuedjobs;
  Integer Pending=Tot-Queuedjobs;
  Staticstics='Total Testclasses='+Tot+'Pending Testclasses='+Pending;
        List<PieWedgeData> data = new List<PieWedgeData>();
        data.add(new PieWedgeData('Completed'+' '+Cmpltd, +Cmpltd));
        data.add(new PieWedgeData('In Progress'+' '+Queuedjobs, +Queuedjobs));
        return data;
    }

    // Wrapper class
    public class PieWedgeData {

        public String name { get; set; }
        public Integer data { get; set; }
  public Integer Cmpltd{ get; set; }
  public Integer Queuedjobs{ get; set; }
        public PieWedgeData(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }

public static Datetime getLocalDateTime(Datetime z)
    {   
        Datetime l = z.Date();
        l = l.addHours(z.hour());
        l = l.addMinutes(z.minute());
        l = l.addSeconds(z.second());
       
        return l;
    }

}

No comments:

Post a Comment