Sunday, 9 September 2018

with Graph and Abort

VFP:


<apex:page standardController="Automated_Test_Run__c" extensions="AutomatedTestSetup_Controller" Sidebar="False" id="pg" docType="html-5.0">
<script>
function fnChange(DT)
{
// first make sure 'DT ' is not null here
alert('Hello datetime'+DT);
DTPass(DT);
//alert(DT);
}
</script>
    <apex:form id="frm">
        <apex:pageBlock title="Job Schedule - Test Execution">
        <apex:pageMessages />
                <apex:pageBlockSection title="Job Schedule" columns="1">
                    <apex:outputPanel rendered="{!hasScheduledJobs}">
                            <!-- Automated Test execution <a href="/08e">scheduled</a> {!DtValue}-->
                            Automated Test execution scheduled {!showdatetime} 

                            <!--Sheduled Jobs Showing Block-Start
                            <div>
                            <apex:pageBlockTable value="{!Schdjobs}" var="jobs">
                               <apex:column >
                                <apex:facet name="header">Scheduled Name</apex:facet>
                                <apex:outputText value="{!jobs.CronJobDetail.Name}"/>
                               </apex:column>                          
                               <apex:column >
                                <apex:facet name="header">Current State</apex:facet>
                                <apex:outputText value="{!jobs.State}"/>
                               </apex:column>
                                <apex:column >
                               <apex:facet name="header">Next Fire Time</apex:facet>
                                <apex:outputText value="{!jobs.NextFireTime}"/>
                               </apex:column>
                             </apex:pageBlockTable>
                             </div>
                            <!--Sheduled Jobs Showing Block-Start-->
                    </apex:outputPanel>

                    <apex:outputPanel rendered="{!NOT(hasScheduledJobs)}">
                    <apex:pageBlockSectionItem >
                        <div>
                            <p>Select the Date and Time to schedule the Automated Test execution</p>              
                        </div>
                    </apex:pageBlockSectionItem>
                    </apex:outputPanel>

                    <apex:pageBlockSectionItem id="ScheduleVal" rendered="{!NOT(hasScheduledJobs)}">
                    <apex:OutputLabel value="Date and Time"/> 
                        <!--<apex:input type="datetime" value="{!startdate}" required="true" /> -->                
                        <apex:InputField required="true" value="{!Automated_Test_Run__c.Schedule__c}" ID="Schdval"/>                      
                    </apex:pageBlockSectionItem>                                      

                    <!-- <apex:commandButton value="Submit" action="{!scheduleJobs}"/> -->
                    <!--<apex:commandButton value="Submit" Action="{!scheduleJobs}">
                    <apex:param name="DttValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>           
                    </apex:commandButton>-->

                    <!--Working BelowComment link-->
                    <apex:commandLink value="Submit" rendered="{!NOT(hasScheduledJobs)}" action="{!scheduleJobs}" styleClass="btn">
                    <apex:param name="DtValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>
                    </apex:commandLink>

                        <!--Working to resolve Pasing value Start
                    <apex:commandButton value="{!Automated_Test_Run__c.Schedule__c}" rendered="{!NOT(hasScheduledJobs)}" onclick="fnChange(this.value);" />
                    
                    <apex:actionFunction id="DTPass" name="DTPass" action="{!scheduleJobs}">
                    <apex:param name="param" assignTo="{!DtValue}" value="{!Automated_Test_Run__c.Schedule__c}" />
                    </apex:actionFunction>                  
                            <!--Working to resolve Pasing value end-->
                            
                    <apex:commandLink value="Cancel" rendered="{!hasScheduledJobs}" action="{!CancelJobs}" styleClass="btn">
                    </apex:commandLink> 
                                
                </apex:pageBlockSection>
                
                <!--Startstics Section -->
          
                <apex:pageBlockSection title="Test Class Execution Statistics" id="Piechartsec" columns="4"> 

                    <apex:pageBlockSectionItem id="ShowGraph">   
                        <apex:outputPanel id="Graph">
                                <apex:chart height="250" width="350" data="{!pieData}" id="Piechartsec2">
                                    <apex:pieSeries dataField="data" labelField="name"/>
                                    <apex:legend position="left"/>
                                </apex:chart>
                            </apex:outputPanel>                                                     
                    </apex:pageBlockSectionItem>                
                    <apex:pageBlockSectionItem id="Refresh">    
                        <apex:commandlink value=" Refresh " status="progress" action="{!Refresh}" styleClass="btn" reRender="Piechartsec2"/>
                        <!-- <apex:commandlink value=" Refresh " status="progress" onclick="window.location.reload();" styleClass="btn" reRender="Piechartsec2"/>-->
                    </apex:pageBlockSectionItem> 

                    <apex:pageBlockSectionItem id="Abort">
                        <apex:commandlink value="Abort" status="progress" action="{!AbortTestJobs}" rendered="{!(hasAbort)}" styleClass="btn" reRender="Piechartsec2"/>
                    </apex:pageBlockSectionItem> 

                        <!--<apex:pageBlockSectionItem id="ShowStaticstics" dataStyle="text-align:center; width: 10%; max-width: 20%;">                 
                    <h1> <p>{!Staticstics}</p> </h1>                    
                    </apex:pageBlockSectionItem> -->
                    
                </apex:pageBlockSection> 
        </apex:pageBlock>
    </apex:form>
</apex:page>
 

****************************************
public with sharing class AutomatedTestSetup_Controller {

    public boolean hasScheduledJobs { get; set; }
    public boolean hasTestRun { get; set; }
    public boolean hasAbort { get; set; }
    public boolean isRunning { get; set; }
    public boolean isEmailing { get; set; }
    public AutomatedTestSetup_Controller ATS { get; set; }
    public String DtValue {get; set; }
    public boolean showMessage {get;set;}
    public String showdatetime{get;set;}
    public String Staticstics{get;set;}
    public List<CronTrigger> Schdjobs{get;set;}
    public List<Shdwrapper> cronjbs{get;set;}
    //public List<Shdwrapper> cronjbs=New List<Shdwrapper>(){get;set;}
    public AutomatedTestSetup_Controller(ApexPages.StandardController controller) {
        showMessage=false;
        refreshHasScheduledJobs();
        refreshHasTestRun();
        ShowAbortbutton();      
    }
    Public class Shdwrapper {
          Public string Name{ get; set; }
          Public Datetime CreatedDate{ get; set; }
          Public Datetime NextFireTime{ get; set; }
          Public string State{ get; set; }
    }
    public void refreshHasScheduledJobs() {
        //integer jobCount = [select count() from CronJobDetail where Name like '%TestRunner%' and JobType = '7'];
        //integer jobCount = [SELECT count() FROM CronTrigger  where CronJobDetail.Name like '%TestRunner%' and State in('WAITING','ACQUIRED','EXECUTING')];
        integer jobCount = [SELECT count() FROM CronTrigger  where CronJobDetail.Name like '%TestRunner%'];
        Schdjobs=[SELECT CronJobDetail.Name,CreatedDate,NextFireTime,State FROM CronTrigger where CronJobDetail.Name like '%TestRunner%' and State in('WAITING','ACQUIRED','EXECUTING')];
        hasScheduledJobs = jobCount !=0; 
        if(hasScheduledJobs==true){
            Shdjobslist(Schdjobs);
            }                    
    }
    Public Void Shdjobslist(list<CronTrigger> Schdjobs){
        //Showing Shedule Jobs on VFp   
         List<Shdwrapper> cronjbs=New List<Shdwrapper>(); 
         if(Schdjobs.size()>0){
                 for(CronTrigger oa : Schdjobs) {
                    //String timeZone = UserInfo.getTimeZone().getID();
                    //Datetime NextDateTime=oa.NextFireTime;
                    
                    Shdwrapper w = new Shdwrapper();
                     w.Name = oa.CronJobDetail.Name;
                     //w.CreatedDate = Datetime.valueOf(CreatedDate);
                     w.NextFireTime = oa.NextFireTime;
                     w.State = oa.State;
                    cronjbs.add(w); 
                }   
         }  
    }
  
    public void refreshHasTestRun() {
        integer runCount = [select count() from Automated_Test_Run__c limit 1];
        hasTestRun = runCount > 0;    
    }
    public void ShowAbortbutton(){
        integer runCount = [select count() from Automated_Test_Run__c limit 1];
        Test_Runner_Job__c RunningJobnm =[Select Name,Automated_Test_Run__c,CreatedDate From Test_Runner_Job__c order by CreatedDate desc limit 1];
         integer TestexecrunCount = [select count() from ApexTestQueueItem WHERE Status IN ('Holding','Queued','Preparing','Processing') and ParentJobId=:RunningJobnm.Name];
        hasAbort = TestexecrunCount > 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);
        showMessage=false;
  
        if(DtValue!='null' && DtValue!='' && DtValue!=null ){
             //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
            deleteCompletedJobs();
            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 DTforSchdle = 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]));
            //DTforSchdle ***1-->2018-09-01 14:13:00
            System.debug('DTforSchdle ***'+DTforSchdle);
            //Getting Locle Timezone
             Datetime gmt = DTforSchdle;
             String timeZone = UserInfo.getTimeZone().getID();
             Datetime GivenDateTime=DTforSchdle;// here you can user your dates e.g. createddate
              Datetime local=Datetime.valueOf(GivenDateTime);
              //showdatetime = string.valueOfGmt(local);
              showdatetime=local.format();
              System.debug('@@@@@@@@@@@'+DtValue);
            integer AutomatedEmailScheduleHours=integer.ValueOf(Label.AutomatedEmailScheduleHours);
            DateTime mailschedule= local.addHours(AutomatedEmailScheduleHours);  
            //Datetime mailschedule =DTforSchdle + (AutomatedEmailScheduleHours/24);
            System.debug('mailschedule ***'+mailschedule);         
            //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);
            AutoTestRunner.setup(strSchedule,strEmailSchedule);
            refreshHasScheduledJobs();
           //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm, 'Jobs scheduled'));    
        }
        /* else{
             showMessage = true;
             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 = new PageReference('/apex/AutomatedTestSetupVFP');
                    pageRef.setRedirect(true);
                    return pageRef;
    }
    
    public PageReference AbortTestJobs() {     
        Test_Runner_Job__c RunningJobnm =[Select Name,Automated_Test_Run__c,CreatedDate From Test_Runner_Job__c order by CreatedDate desc limit 1];
        ApexTestQueueItem[] items = [SELECT Id FROM ApexTestQueueItem WHERE Status IN ('Holding','Queued','Preparing','Processing') and ParentJobId=:RunningJobnm.Name];
            for(ApexTestQueueItem item: items) {
                item.Status = 'Aborted';
            }
            update items;
            
        PageReference pageRef = new PageReference('/apex/AutomatedTestSetupVFP');
        pageRef.setRedirect(true);
        return pageRef;
    }
 
    public void deleteCompletedJobs(){
        List<CronTrigger> jobsToAbort = [select Id,State from CronTrigger where CronJobDetail.JobType = '7' and CronJobDetail.Name like '%TestRunner%' and State='DELETED'];
              for (CronTrigger job : jobsToAbort) {
               System.abortJob(job.Id);
               }       
    }
    
    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 part
    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];
      //SELECT AsyncApexJobId, Outcome, ApexClass.Name, MethodName, Message, StackTrace FROM ApexTestResult WHERE AsyncApexJobId='7074D00000OEnAAQA1'
      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 ApexTestResult WHERE Outcome = 'Pass' and AsyncApexJobId=:RunningJob.Name];
      integer Failed       = [select count() FROM ApexTestResult WHERE Outcome = 'Fail' and AsyncApexJobId=:RunningJob.Name];
      integer Queuedjobs   = [select count() FROM ApexTestQueueItem where Status='Queued' and ParentJobId=:RunningJob.Name];
      integer TotalJobs   =  [select count() FROM ApexTestQueueItem where ParentJobId=:RunningJob.Name];
      //Staticstics='Total Testclasses '+TotalJobs+'  '+'Pending Testclasses '+Queuedjobs;
     Staticstics= 'Total Testclasses '+TotalJobs+'\n';
    // Staticstics+= '\n'+'Pending Testclasses '+Queuedjobs+'\n';
     Staticstics+= '\n'+'Test Execution Started on  '+RunningJob.CreatedDate;
            List<PieWedgeData> data = new List<PieWedgeData>();
            data.add(new PieWedgeData('In Progress'+' '+Queuedjobs, +Queuedjobs));
            data.add(new PieWedgeData('Passed'+' '+Cmpltd, +Cmpltd));
            data.add(new PieWedgeData('Fail'+' '+Failed, +Failed));
            
        return data;
    }

    // Wrapper class
    public class PieWedgeData {
       public String name { get; set; }
       public Integer data { get; set; }
       public Integer Cmpltd{ get; set; }
       public Integer Failed{ get; set; }
       public Integer Queuedjobs{ get; set; }
           public PieWedgeData(String name, Integer data) {
                this.name = name;
                this.data = data;
            }
    }
    
    public PageReference Refresh(){ 
         PageReference pageRef = new PageReference('/apex/AutomatedTestSetupVFP');
            pageRef.setRedirect(true);
            return pageRef;
    }
}




Monday, 3 September 2018

after Schedule fix


//https://developer.salesforce.com/forums/?id=906F0000000MNmuIAG
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;
/*integer offset=userinfo.getTimezone().getoffset(gmt);
Datetime local = gmt.addSeconds(offset/1000);
System.Debug('GMT Time: ' + gmt);
System.Debug('Local Time: ' + local);*/
//Contact c=new Contact(LastName='Test1contact',New_Datetime__c=local); insert c;


String timeZone = UserInfo.getTimeZone().getID();
Datetime dateGMT=DTforEmail;// here you can user your dates e.g. createddate
Datetime local=Datetime.valueOf(dateGMT);
string DtValue=local.format();
System.debug('@@@@@@@@@@@'+DtValue);
Contact c=new Contact(LastName=DtValue,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;
    }

}

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;
    }

}

Sunday, 26 August 2018

Automated_WIP

*******Page

<apex:page standardController="Automated_Test_Run__c" extensions="AutomatedTestSetup_Controller" Sidebar="False" id="pg" docType="html-5.0">
    <apex:form id="frm">
        <apex:pageBlock title="Job Schedule - Test Execution">
        <apex:pageMessages />
            <apex:pageBlockSection title="Job Schedule" columns="1">
                    <apex:outputPanel rendered="{!hasScheduledJobs}">
                    Automated Test execution <a href="/08e">scheduled</a>
                    </apex:outputPanel>
           
                    <apex:outputPanel rendered="{!NOT(hasScheduledJobs)}">
                        <apex:pageBlockSectionItem >
                            <div>
                                <p>Select the Date and Time to schedule the Automated Test execution</p>             
                            </div>
                        </apex:pageBlockSectionItem>
                    </apex:outputPanel>
                   
                    <apex:pageBlockSectionItem id="ScheduleVal" rendered="{!NOT(hasScheduledJobs)}">
                        <apex:OutputLabel value="Date and Time"/> 
                        <!--<apex:input type="datetime" value="{!startdate}" required="true" /> -->               
                        <apex:InputField required="true" value="{!Automated_Test_Run__c.Schedule__c}" ID="Schdval"/>                     
                    </apex:pageBlockSectionItem>                                     
                       
                    <!-- <apex:commandButton value="Submit" action="{!scheduleJobs}"/> -->
                    <!--<apex:commandButton value="Submit" Action="{!scheduleJobs}">
                     <apex:param name="DttValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>           
                    </apex:commandButton>-->
               
                    <apex:commandLink value="Submit" rendered="{!NOT(hasScheduledJobs)}" action="{!scheduleJobs}" styleClass="btn">
                        <apex:param name="DtValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>
                    </apex:commandLink>
                   
                    <apex:commandLink value="Cancel" rendered="{!hasScheduledJobs}" action="{!CancelJobs}" styleClass="btn">
                    </apex:commandLink>                   
            </apex:pageBlockSection>
           
            <apex:pageBlockSection title="Statistics" id="Piechartsec" columns="2">
                <div>
                    <p>Percentage completion of test execution:</p>             
                </div>
                <!--<apex:commandButton value="Refresh" Action="{!getPieData}" reRender="Piechartsec2"/>
                <apex:commandlink value=" refresh" status="progress" onclick="window.location.href='/apex/AutomatedTestSetupVFP" />-->
              <apex:commandlink value=" Refresh " status="progress" onclick="window.location.href='/apex/AutomatedTestSetupVFP" styleClass="btn"/>
                <apex:chart height="250" width="350" data="{!pieData}" id="Piechartsec2">
                <apex:pieSeries dataField="data" labelField="name"/>
                <apex:legend position="right"/>
                </apex:chart>

         
           
           </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
               


**************

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 'TestRunner%' 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
       
        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);
        //String DaTime=ApexPages.currentPage().getParameters().get('{$Automated_Test_Run__c.Schedule__c}');
        //System.debug('Date & Time is'+one);
        TestRunner.setup(strSchedule);
        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 'TestRunner%' 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() {
        TestRunner.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];
//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;
        }
    }

}


*************

global class TestRunner implements Schedulable {
    // derived from http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_unit_tests_running.htm#APISectionTitle

    public static void setup(String strSchedule) {
        try {
String cronexpression=strSchedule;
            system.schedule('TestRunner',cronexpression, new TestRunner());
           // system.schedule('TestRunnerResults', '0 30 23 ? * SUN-THU', new TestRunnerResults());
        } catch (Asyncexception ex) {
            system.debug('Job already scheduled');
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Please Select Future Date Time');
              ApexPages.addMessage(myMsg);
        }
    }
   
   global void execute(SchedulableContext SC) {
      enqueueTests();
   }
   
    // Enqueue all unit test classes as spec'd by Automated_Test_Run__c record(s)
    public static list<ID> enqueueTests() {
        list<Id> jobIds = new list<Id>();
   
        for (Automated_Test_Run__c run : [select Query__c, Excluded_Classes__c,
        Email_on_Pass__c, Exclude_Managed_Packages__c
        from Automated_Test_Run__c where Active__c = true]) {
            string whereExcluded = '';
            if (run.Excluded_Classes__c != null) {
                for (string c : run.Excluded_Classes__c.split('[,\\s]+')) {
                    whereExcluded += 'and Name != \'' + c + '\'';
                }
            }
            if (run.Exclude_Managed_Packages__c == True) {
               whereExcluded += ' AND NameSpacePrefix = null';
            }
           
            string query = 'SELECT Id FROM ApexClass WHERE Name LIKE \'' + run.Query__c + '\' ' + whereExcluded;
            ApexClass[] testClasses = Database.query(query);
               
            if (testClasses.size() > 0) {
                ApexTestQueueItem[] queueItems = new List<ApexTestQueueItem>();
                for (ApexClass cls : testClasses) {
                    queueItems.add(new ApexTestQueueItem(ApexClassId=cls.Id));
                }
   
                if (!Test.isRunningTest()) {
                    insert queueItems;
       
                    // Get the job ID of the first queue item returned.       
                    ApexTestQueueItem item =
                       [SELECT ParentJobId FROM ApexTestQueueItem
                        WHERE Id=:queueItems[0].Id LIMIT 1];
                       
                    insert new Test_Runner_Job__c(Name = item.parentjobid, Automated_Test_Run__c = run.Id);
                   
                    jobIds.add(item.parentjobid);
                }
            }
        }
       
        return jobIds;
    }

    public static list<Messaging.SingleEmailMessage> emailMethodStatus(list<Test_Runner_Job__c> jobs) {
        set<string> jobNames = new set<string>();
        for (Test_Runner_Job__c job : jobs) {
            jobNames.add(job.Name);
        }
       
        ApexTestResult[] results =
           [SELECT AsyncApexJobId, Outcome, ApexClass.Name, MethodName, Message, StackTrace
            FROM ApexTestResult
            WHERE AsyncApexJobId in :jobNames];
           
        return emailMethodStatus(jobs, results);
    }
       
    public static list<Messaging.SingleEmailMessage> emailMethodStatus(list<Test_Runner_Job__c> jobs, ApexTestResult[] results) {
        list<Messaging.SingleEmailMessage> messages = new list<Messaging.SingleEmailMessage>();
       
        for (Test_Runner_Job__c job : jobs) {
            system.debug('Test Runner Job: Id ' + job.Id + ' Name ' + job.Name);
            integer passed = 0;
            integer total = 0;
           
            string failTable = '<table border="1" cellpadding="5px">';
            string className = '';
               
            for (ApexTestResult atr : results) {
                system.debug('Apex Test Result: Id '  + atr.AsyncApexJobId);
                if ((string)atr.AsyncApexJobId == job.Name || (Test.isRunningTest() && jobs.size() == 1)) {
                    if (atr.Outcome == 'Pass') {
                        passed++;
                    } else {
                        if (atr.ApexClass.Name != className) {
                            className = atr.ApexClass.Name;
                            failTable += '<tr style="background: #ddd; font-weight: bold;"><td colspan="2">' + className + '</td></tr>\n';
                        }
                        failTable += '<tr><td>' + atr.MethodName + '</td><td> ' + atr.message + '</td></tr>\n';
                    }
                    total++;
                }
            }
           
            failTable += '</table>';
           
            string body = '<p>' + passed + '/' + total + ' tests passed</p>\n';
            boolean allPassed = passed == total;
           
            if (!allPassed) {
                body += '<p>Test Failures:</p>\n' + failTable;
            }
           
            if ((allPassed && job.Automated_Test_Run__r.Email_on_Pass__c) || !allPassed) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(job.Automated_Test_Run__r.Emails__c.split('[,\\s]+'));
                mail.setSubject(job.Automated_Test_Run__r.Name + ' Automated Test Results: ' + (passed < total ? 'Fail' : 'Pass'));
                mail.setHtmlBody(body);
                System.debug(mail);
                messages.add(mail);
            }
        }     

        if (!Test.isRunningTest()) {
            Messaging.sendEmail(messages);
        }
       
        return messages;
    }
 }


*******************



SELECT Id, StartTime, LogUserId, LogLength, Location FROM ApexLog

https://developer.salesforce.com/forums/?id=9060G000000XhvVQAS

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm


DateTime myDateTime = DateTime.newInstance(1997, 1, 31, 7, 8, 16);
DateTime newDateTime = myDateTime.addHours(3);
DateTime expected = DateTime.newInstance(1997, 1, 31, 10, 8, 16);
System.assertEquals(expected, newDateTime);


********

Automated-done

*************
<apex:page standardController="Automated_Test_Run__c" extensions="AutomatedTestSetup_Controller" Sidebar="False" id="pg" docType="html-5.0">
    <apex:form id="frm">
        <apex:pageBlock title="Job Schedule - Test Execution">
        <apex:pageMessages />
            <apex:pageBlockSection title="Job Schedule" columns="1">
                    <apex:outputPanel rendered="{!hasScheduledJobs}">
                    Automated Test execution <a href="/08e">scheduled</a>
                    </apex:outputPanel>
           
                    <apex:outputPanel rendered="{!NOT(hasScheduledJobs)}">
                        <apex:pageBlockSectionItem >
                            <div>
                                <p>Select the Date and Time to schedule the Automated Test execution</p>             
                            </div>
                        </apex:pageBlockSectionItem>
                    </apex:outputPanel>
                   
                    <apex:pageBlockSectionItem id="ScheduleVal" rendered="{!NOT(hasScheduledJobs)}">
                        <apex:OutputLabel value="Date and Time"/> 
                        <!--<apex:input type="datetime" value="{!startdate}" required="true" /> -->               
                        <apex:InputField required="true" value="{!Automated_Test_Run__c.Schedule__c}" ID="Schdval"/>                     
                    </apex:pageBlockSectionItem>                                     
                       
                    <!-- <apex:commandButton value="Submit" action="{!scheduleJobs}"/> -->
                    <!--<apex:commandButton value="Submit" Action="{!scheduleJobs}">
                     <apex:param name="DttValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>           
                    </apex:commandButton>-->
               
                    <apex:commandLink value="Submit" rendered="{!NOT(hasScheduledJobs)}" action="{!scheduleJobs}" styleClass="btn">
                        <apex:param name="DtValue" value="{!Automated_Test_Run__c.Schedule__c}" assignTo="{!DtValue}"/>
                    </apex:commandLink>
                   
                    <apex:commandLink value="Cancel" rendered="{!hasScheduledJobs}" action="{!CancelJobs}" styleClass="btn">
                    </apex:commandLink>                   
            </apex:pageBlockSection>
           
            <apex:pageBlockSection title="Statistics" id="Piechartsec" columns="2">
                <div>
                    <p>Percentage completion of test execution:</p>             
                </div>
                <!--<apex:commandButton value="Refresh" Action="{!getPieData}" reRender="Piechartsec2"/>
                <apex:commandlink value=" refresh" status="progress" onclick="window.location.href='/apex/AutomatedTestSetupVFP" />-->
              <apex:commandlink value=" Refresh " status="progress" onclick="window.location.href='/apex/AutomatedTestSetupVFP" styleClass="btn"/>
                <apex:chart height="250" width="350" data="{!pieData}" id="Piechartsec2">
                <apex:pieSeries dataField="data" labelField="name"/>
                <apex:legend position="right"/>
                </apex:chart>

         
           
           </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
               

****************

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 'TestRunner%' 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
       
        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);
        //String DaTime=ApexPages.currentPage().getParameters().get('{$Automated_Test_Run__c.Schedule__c}');
        //System.debug('Date & Time is'+one);
        TestRunner.setup(strSchedule);
        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 'TestRunner%' 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() {
        TestRunner.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];
//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;
        }
    }

}

************

global class TestRunner implements Schedulable {
    // derived from http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_unit_tests_running.htm#APISectionTitle

    public static void setup(String strSchedule) {
        try {
String cronexpression=strSchedule;
            system.schedule('TestRunner',cronexpression, new TestRunner());
           // system.schedule('TestRunnerResults', '0 30 23 ? * SUN-THU', new TestRunnerResults());
        } catch (Asyncexception ex) {
            system.debug('Job already scheduled');
        }
    }
   
   global void execute(SchedulableContext SC) {
      enqueueTests();
   }
   
    // Enqueue all unit test classes as spec'd by Automated_Test_Run__c record(s)
    public static list<ID> enqueueTests() {
        list<Id> jobIds = new list<Id>();
   
        for (Automated_Test_Run__c run : [select Query__c, Excluded_Classes__c,
        Email_on_Pass__c, Exclude_Managed_Packages__c
        from Automated_Test_Run__c where Active__c = true]) {
            string whereExcluded = '';
            if (run.Excluded_Classes__c != null) {
                for (string c : run.Excluded_Classes__c.split('[,\\s]+')) {
                    whereExcluded += 'and Name != \'' + c + '\'';
                }
            }
            if (run.Exclude_Managed_Packages__c == True) {
               whereExcluded += ' AND NameSpacePrefix = null';
            }
           
            string query = 'SELECT Id FROM ApexClass WHERE Name LIKE \'' + run.Query__c + '\' ' + whereExcluded;
            ApexClass[] testClasses = Database.query(query);
               
            if (testClasses.size() > 0) {
                ApexTestQueueItem[] queueItems = new List<ApexTestQueueItem>();
                for (ApexClass cls : testClasses) {
                    queueItems.add(new ApexTestQueueItem(ApexClassId=cls.Id));
                }
   
                if (!Test.isRunningTest()) {
                    insert queueItems;
       
                    // Get the job ID of the first queue item returned.       
                    ApexTestQueueItem item =
                       [SELECT ParentJobId FROM ApexTestQueueItem
                        WHERE Id=:queueItems[0].Id LIMIT 1];
                       
                    insert new Test_Runner_Job__c(Name = item.parentjobid, Automated_Test_Run__c = run.Id);
                   
                    jobIds.add(item.parentjobid);
                }
            }
        }
       
        return jobIds;
    }

    public static list<Messaging.SingleEmailMessage> emailMethodStatus(list<Test_Runner_Job__c> jobs) {
        set<string> jobNames = new set<string>();
        for (Test_Runner_Job__c job : jobs) {
            jobNames.add(job.Name);
        }
       
        ApexTestResult[] results =
           [SELECT AsyncApexJobId, Outcome, ApexClass.Name, MethodName, Message, StackTrace
            FROM ApexTestResult
            WHERE AsyncApexJobId in :jobNames];
           
        return emailMethodStatus(jobs, results);
    }
       
    public static list<Messaging.SingleEmailMessage> emailMethodStatus(list<Test_Runner_Job__c> jobs, ApexTestResult[] results) {
        list<Messaging.SingleEmailMessage> messages = new list<Messaging.SingleEmailMessage>();
       
        for (Test_Runner_Job__c job : jobs) {
            system.debug('Test Runner Job: Id ' + job.Id + ' Name ' + job.Name);
            integer passed = 0;
            integer total = 0;
           
            string failTable = '<table border="1" cellpadding="5px">';
            string className = '';
               
            for (ApexTestResult atr : results) {
                system.debug('Apex Test Result: Id '  + atr.AsyncApexJobId);
                if ((string)atr.AsyncApexJobId == job.Name || (Test.isRunningTest() && jobs.size() == 1)) {
                    if (atr.Outcome == 'Pass') {
                        passed++;
                    } else {
                        if (atr.ApexClass.Name != className) {
                            className = atr.ApexClass.Name;
                            failTable += '<tr style="background: #ddd; font-weight: bold;"><td colspan="2">' + className + '</td></tr>\n';
                        }
                        failTable += '<tr><td>' + atr.MethodName + '</td><td> ' + atr.message + '</td></tr>\n';
                    }
                    total++;
                }
            }
           
            failTable += '</table>';
           
            string body = '<p>' + passed + '/' + total + ' tests passed</p>\n';
            boolean allPassed = passed == total;
           
            if (!allPassed) {
                body += '<p>Test Failures:</p>\n' + failTable;
            }
           
            if ((allPassed && job.Automated_Test_Run__r.Email_on_Pass__c) || !allPassed) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(job.Automated_Test_Run__r.Emails__c.split('[,\\s]+'));
                mail.setSubject(job.Automated_Test_Run__r.Name + ' Automated Test Results: ' + (passed < total ? 'Fail' : 'Pass'));
                mail.setHtmlBody(body);
                System.debug(mail);
                messages.add(mail);
            }
        }     

        if (!Test.isRunningTest()) {
            Messaging.sendEmail(messages);
        }
       
        return messages;
    }
 }


**************

Sunday, 1 April 2018

Small Queue check snippet

if((result(new value,oldvalue)))

public class boolean result(newval, oldval){
boolean resbool=false;
allquenames we are getting in allquenames
if(allquenames.contains(newval) && (newval!=oldval)){
for(type qm:allquenames){
if(qm=newvale){
resbool=true
}

}

}
return resbool;
}