Thursday, 7 June 2018

salesforce - Refresh Lightning Page with sforce.one.back(true);

<apex:page standardController="Time_Service__c" extensions="TimerController">
 
<apex:sectionHeader title="Track Activity"/>
 <apex:includeScript value="{!URLFOR($Resource.Jquery1_10)}"  />

    <style> 
        @font-face {
        font-family: 'digital';
        src: url("{!URLFOR($Resource.DigitalFonts,'digitalfont.ttf')}");
        src: url("{!URLFOR($Resource.DigitalFonts,'digitalfont.ttf')}") format('truetype');
        }
     
        .container {
            padding: 200px;
            text-align: center;
        }

        .timer {
            padding: 10px;
            background: linear-gradient(top, #222, #444);
            display: inline-block;
            border: 7px solid #efefef;
            border-radius: 5px;
            position: relative;
         
            box-shadow:
                inset 0 -2px 10px 1px rgba(0, 0, 0, 0.75),
                0 5px 20px -10px rgba(0, 0, 0, 1);
        }
     
        .cell {
            /*Should only display 1 digit. Hence height = line height of .numbers
            and width = width of .numbers*/
            width: 0.8em;
            height: 40px;
            font-size: 37px;
            font-family: 'digital';
            position: relative;
            margin: 0 auto;
          //  color: #707070;
        }

        .numbers {
            width: 0.6em;
            line-height: 40px;
            font-family: digital, arial, verdana;
            text-align: center;
            color: #fff;
         
            position: absolute;
            top: 0;
            left: 0;
         
            /*Glow to the text*/
            text-shadow: 0 0 5px rgba(255, 255, 255, 1);
        }
     
        .cellName {
        font-family: "Lucida Console", "Lucida Sans Typewriter", Monaco, "Bitstream Vera Sans Mono", monospace;
        margin: 0 auto;
       // color: #707070;
        }
     
        .cellBlock {
            float: left;
            -webkit-box-shadow: 10px 0px 5px -7px #888 ;
        }
     
        .buttonBlock {
            float: right;
            margin-left: 20px;
        }
     
        .carvedhr {
             clear: both;
             float: none;
             width: 98%;
             height: 2px;
             margin:5px 0px 5px 1px;
             border: none;
             background: #ddd;
             background-image: -webkit-gradient(
                 linear,
                 left top,
                 left bottom,
                 color-stop(0.5, rgb(221,221,221)),
                 color-stop(0.5, rgb(255,255,255))
             );
             background-image: -moz-linear-gradient(
                 center top,
                 rgb(221,221,221) 50%,
                 rgb(255,255,255) 50%
             );       
        }
     
        .logoDiv {
            height: 14px;
            float: right;
            padding-right:2%;
            padding-top:1.5%;
            margin-bottom:1.2%     
        }             
    </style> 
<script src="/soap/ajax/29.0/connection.js"></script>
<script type='text/javascript' src='/canvas/sdk/js/publisher.js'/>
<script src="https://mobile1.t.salesforce.com/soap/ajax/30.0/connection.js" type="text/javascript" />
 
    <script type="text/javascript">   
        var time;
        var secVal;
        var minVal;
        var houVal;
        var isStarted = new Boolean(0);
        var stopTimer = new Boolean({!stopTimer});
        var confirmedStop = new Boolean(0); // used at oncomplete stop
        var isAutoSave = new Boolean(0); // use this boolean to toggle auto save
        var autoSaveCounter = 0;
        var autoSaveTime = 300; // default auto save time 300 seconds

        function start_timer(btnToChange) {
            //  var btnToChange = document.getElementById( id );        this is not required
            if (stopTimer == false) {
                if (isStarted == 0) {
                    isStarted = 1;
                    btnToChange.value = 'Pause Timer';
                    timer = setInterval("increaseCounter()", 1000);               
                    time = secVal + '#' + minVal + '#' + houVal + '#' + stopTimer;
               //     startClockAct(time);
                 
                    autoSaveCounter = 0;
                } else {
                    isStarted = 0;
                    btnToChange.value = 'Start Timer';
                    window.clearTimeout(timer);
                    secVal = secVal + 1;
                    time = secVal + '#' + minVal + '#' + houVal + '#' + stopTimer;
                    stopClockAct(time);
                }
            }
        }

        function stop_timer() {
           if (stopTimer == false) {
              //  if (confirmStop() == true) {
                    if (typeof timer != 'undefined') { // user clicking directly stop button
                        window.clearTimeout(timer);
                        if (isStarted == 1) { // don't adjust time if timer is not started
                            secVal = secVal + 1;
                        }
                    } else {
                        secVal = parseInt($('#counterSec').html(), 10)
                        minVal = parseInt($('#counterMin').html(), 10)
                        houVal = parseInt($('#counterHour').html(), 10)
                    }
                    isStarted = 0;
                    var btnToChange = document.getElementById('j_id0:j_id4:PBBlock:startButton');
                    btnToChange.value= 'Start Timer' ;
                   // stopTimer = true; // this stops the timer
                    time = secVal + '#' + minVal + '#' + houVal + '#' + stopTimer;
                   stopClockAct(time);
                 // saveClockAct(time);
               // }
            }
        }
     
        function Save_timer() {
            //if (stopTimer == false) {
              //  if (confirmStop() == true) {
                    if (typeof timer != 'undefined') { // user clicking directly stop button
                        window.clearTimeout(timer);
                        if (isStarted == 1) { // don't adjust time if timer is not started
                            secVal = secVal + 1;
                        }
                    } else {
                        secVal = parseInt($('#counterSec').html(), 10)
                        minVal = parseInt($('#counterMin').html(), 10)
                        houVal = parseInt($('#counterHour').html(), 10)
                    }
                    isStarted = 0;
                    //stopTimer = true; // this stops the timer
                    time = secVal + '#' + minVal + '#' + houVal + '#' + stopTimer;
                   // stopClockAct(time);Commented by Arjun
                  // alert('Calling');
                   saveButtonrec(time);
                //}
           // }
        }

        function increaseCounter() {
            secVal = parseInt($('#counterSec').html(), 10)
            minVal = parseInt($('#counterMin').html(), 10)
            houVal = parseInt($('#counterHour').html(), 10)
            if (secVal != 59)
                $('#counterSec').html((secVal + 1));
            else {
                if (minVal != 59) {
                    $('#counterMin').html((minVal + 1));
                } else {
                    $('#counterHour').html((parseInt($('#counterHour').html(), 10) + 1));
                    $('#counterMin').html(0);
                }
                $('#counterSec').html(0);
            }

            // auto save
            if (isAutoSave == true) {
                autoSaveCounter++;
                if (autoSaveCounter == autoSaveTime) {
                    var saveTime = secVal + '#' + minVal + '#' + houVal + '#' + stopTimer;
                    saveClockAct(saveTime);
                 
                    autoSaveCounter = 0;
                }
            }
        }

        function confirmStop() {
            confirmedStop = confirm("Do you really want to stop the timer!");
            if (confirmedStop == true) {
                return true;
            } else {
                return false;
            }
        }

        function onCompleteStop() {
            if (confirmedStop == true) {
               window.top.location = '/{!caseId}';
            }
        }         
    </script>
 
 
   
         
<apex:pageMessages  id="msgId" />
        <apex:form >
        <apex:outputPanel id="hiddenblock"> 
     
        <apex:outputPanel id="panel1">
     <apex:pageBlock id="PBBlock">
    <div class ="clockBlock">
            <div id="innerDiv" style="width:400px;">
                <br/>
                    <div class="timer">
                        <div class="cellBlock" >
                            <div class="cell">
                                <div id="counterHour">
                                    {!hourTime}
                                </div>
                             </div>                     
                             <div class="cellName">
                                &nbsp; Hours &nbsp;
                             </div>
                        </div>
                        <div class="cellBlock" >
                            <div class="cell">
                                <div id="counterMin" >
                                    {!minTime}
                                </div>
                             </div>                     
                             <div class="cellName">
                                    &nbsp; Minutes &nbsp;
                            </div>                           
                        </div>
                        <div class="cellBlock" >
                            <div class="cell">
                                <div id="counterSec" >
                                    {!secTime}
                                </div>               
                            </div>
                             <div class="cellName">
                                 &nbsp; Seconds &nbsp;
                            </div>
                        </div>
                    <div class="buttonBlock" >
                        <div >   
                             <apex:commandButton value="Start Timer" id="startButton" onclick="start_timer(this)" rerender="none" disabled="{!stopTimer}"/>
                        </div>
                        <div style="margin-top:4%;">
                            <apex:commandButton value="Save Timer" id="stopButton" onclick="stop_timer(this)" rerender="none" disabled="{!stopTimer}"  oncomplete="onCompleteStop()"/>
                        </div>
                    </div>
                    <div style="padding-top:19.5%;">
                        <hr class = "carvedhr" />
                    </div>
                  <!--  <div class="logoDiv" >
                            <apex:image url="{!$Resource.ArxxusCTv1__clockLogo}" height="100%"/>
                    </div> -->                 
              </div>             
                     <apex:actionFunction name="startClockAct" action="{!startClock}" rerender="none" >   
                       <apex:param name="time" assignTo="{!timeBundle}" value="" /> 
                    </apex:actionFunction>
                 
                   <apex:actionFunction name="stopClockAct" action="{!stopClock}" rerender="outpanelId">   
                       <apex:param name="time" assignTo="{!timeBundle}" value="" /> 
                    </apex:actionFunction>
                 
                     <apex:actionFunction name="saveClockAct" action="{!saveTime}" rerender="pbBlck">   
                       <apex:param name="time" assignTo="{!timeBundle}" value="" /> 
                    </apex:actionFunction>
                 
                    <apex:actionFunction name="saveButtonrec" action="{!saveRecord}" rerender="hiddenblock">   
                       <apex:param name="time" assignTo="{!timeBundle}" value="" /> 
                    </apex:actionFunction> 

                         
            </div>
         
     
    </div>
    </apex:pageBlock>
    </apex:outputPanel>
 
</apex:outputPanel>   
<!--</apex:form>
 <apex:form >-->
 <apex:outputPanel id="outpanelId">
     <apex:pageBlock id="pbBlck" >
 
         <apex:pageBlockSection title="Task Information">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Activity Description</apex:outputLabel>
                    <apex:outputPanel >
                        <div class="requiredInput">
                            <div class="requiredBlock"></div>
                            <apex:inputField  required="true" value="{!Time_Service__c.Activity_Description__c}"/>
                        </div>
                     </apex:outputPanel> 
            </apex:pageBlockSectionItem>
               <apex:outputPanel style="margin-left:30%">
                    <apex:outputLabel rendered="{!lead_render}"> Lead: &nbsp; </apex:outputLabel>
                    <apex:outputText value="{!LeadObject.Lastname}" rendered="{!lead_render}" />
                    <apex:outputLabel rendered="{!opportunity_render}"> Opportunity:  &nbsp; </apex:outputLabel>
                    <apex:outputText value="{!opportunityObject.Name}" rendered="{!opportunity_render}"/>
                    <apex:outputLabel rendered="{!case_render}"> Case:  &nbsp;</apex:outputLabel>
                    <apex:outputText value="{!caseObject.CaseNumber}" rendered="{!case_render}" />
                </apex:outputPanel>
           
                 <apex:pageBlockSectionItem >
                    <apex:outputLabel > Status</apex:outputLabel>
                    <apex:outputText value="Completed"/>
                 </apex:pageBlockSectionItem>
               
                 <apex:pageBlockSectionItem >
                 <apex:outputLabel >Type</apex:outputLabel>
                 <apex:inputField value="{!Time_Service__c.Type__c}"/>
                 </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
         <apex:pageBlockSection columns="1">
             <apex:pageBlockSectionItem >
             <apex:outputLabel >Comments</apex:outputLabel>
                 <apex:outputPanel >
                    <div class="requiredInput">
                                    <div class="requiredBlock"></div>
                             <apex:inputField  required="true" style="height : 100px; width: 500px" value="{!Time_Service__c.Comments__c}"/>
                     </div>
                   </apex:outputPanel>
             </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
       
         <apex:pageBlockSection title="Timer Information">
             <apex:pageBlockSectionItem >
                <apex:outputLabel >Task Hours</apex:outputLabel>
                <apex:outputText value="{!hourTime}"/>
            </apex:pageBlockSectionItem>
         
             <apex:pageBlockSectionItem >
                <apex:outputLabel >Task Mins</apex:outputLabel>
                <apex:outputText value="{!minTime}"/>
             </apex:pageBlockSectionItem>
           
             <apex:pageBlockSectionItem >
             <apex:outputLabel >Task Second</apex:outputLabel>
             <apex:outputText value="{!secTime}"/>
             </apex:pageBlockSectionItem>
           
             <apex:pageBlockSectionItem >
             <apex:outputLabel >Total time in Seconds</apex:outputLabel>
             <apex:outputText value="{!totalsecond}"/>       
             </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
   
     <apex:pageBlockButtons >
         <apex:commandButton id="saveButton" value="Save" onclick="Save_timer()" oncomplete="return doRefresh()" reRender="script-block,msgId" />
         
         <apex:commandButton action="{!cancel}" value="Cancel"/>
     </apex:pageBlockButtons>
     </apex:pageBlock>
     <script type="text/javascript">
             function doRefresh(){
                 // alert('hasMessages: '+hasMessages);
                 if(hasMessages == 'false') {
                   
                 if ((typeof sforce != 'undefined') && sforce && (!!sforce.one)) {
                     // Manage navigation in Lightning Experience & Salesforce1
                     sforce.one.back(true);                   
                     // alert('---doRefresh---');
                     Sfdc.canvas.publisher.publish({name: "publisher.refresh", payload:{ refresh:"true"}});

                  }
                     }
             
             }
             </script>
     <apex:outPutPanel layout="block" id="script-block">
         <script>
         var hasMessages = '{!hasError}';
         </script>
     </apex:outPutPanel>
 </apex:outputPanel>
 </apex:form>
</apex:page>







public class TimerController {

    public Boolean stopTimer{get;set;}
    public string timeBundle{get;set;}
    public decimal hourTime{get;set;}
    public decimal minTime{get;set;}
    public decimal secTime{get;set;}
    public decimal totalsecond{get;set;}
    public string caseId{get;set;}
    public case CaseObject {get;set;}
    public string leadId{get;set;}
    public Lead LeadObject {get;set;}
    public string opportunityId{get;set;}
    public Opportunity OpportunityObject {get;set;}
    public ApexPages.StandardController stController {get; set;}
    public boolean lead_render{get;set;}
    public boolean opportunity_render{get;set;}
    public boolean case_render{get;set;}
    public Boolean hasError {
        get {
            return ApexPages.hasMessages();
        }
    }
    //public Time_Service__c timeObjectRecord{get;set;}
 
    public TimerController(ApexPages.StandardController Controller){
 
        stopTimer = false;
     
        case_render=false;
        lead_render=false;
        opportunity_render=false;
     
        hourTime = 0;
        minTime = 0;
        secTime = 0;
        stController = Controller;
     
         caseId = ApexPages.currentPage().getParameters().get('caseId');
         leadId = ApexPages.currentPage().getParameters().get('leadId');
         opportunityId = ApexPages.currentPage().getParameters().get('opportunityId');
     
        try{
         
        if(caseId != null && caseId.length()>0){
             caseObject = [Select Id, CaseNumber from Case where Id= : caseId];
            case_render=true;
        }
        if(leadId != null && leadId.length()>0){
             LeadObject = [Select Id,lastname from Lead where Id= : leadId];
            lead_render=true;
        }
        if(opportunityId != null && opportunityId.length()>0){
             OpportunityObject = [Select Id,name from Opportunity where Id= : opportunityId];
            opportunity_render=true;
        }
       
        }catch(Exception ex){
            System.debug('Exception: '+ex);
        }
}
 
    public void startClock(){
     
    }
 
    public pageReference stopClock(){ 
        System.debug('inside Stop clocl');
        System.debug('timeBundle=='+timeBundle);
        list<String> parts = timeBundle.split('#');
     
     
     
        if(parts[0] != 'undefined'){
            System.debug('parts0=='+parts[0]);
            System.debug('parts1=='+parts[1]);
            System.debug('parts2=='+parts[2]);
            System.debug('parts3=='+parts[3]);
         
            System.debug('secTime=='+secTime);
            secTime = integer.valueOf(parts[0]);
            minTime = integer.valueOf(parts[1]);
           hourTime = integer.valueOf(parts[2]);
         
            totalsecond = (3600*hourTime + 60*minTime + secTime);
        }
        return null;
    }
 
    public pageReference saveTime(){
 
        System.debug('Inside Save Method');
        System.debug('timeBundle=='+timeBundle);
     
        list<String> parts = timeBundle.split('#');
             
        secTime = integer.valueOf(parts[0]);
        minTime = integer.valueOf(parts[1]);
        hourTime = integer.valueOf(parts[2]);
     
        Time_Service__c timeRecord;
       /* if(timeObjectRecord!=null)
            timeRecord=timeObjectRecord;
        else*/
            timeRecord = (Time_Service__c)stController.getRecord();
        totalsecond = (3600*hourTime + 60*minTime + secTime);
     
         System.debug('caseId: '+caseId+' length: '+caseId.length());
         System.debug('leadId: '+leadId+' length: '+leadId.length());
         System.debug('opportunityId: '+opportunityId+' length: '+opportunityId.length());
     
        if(caseId!=null && caseId.length()>0)
        timeRecord.Case__c = caseId;
        if(leadId!=null && leadId.length()>0)
        timeRecord.Lead__c=  leadId;
        if(opportunityId!=null && opportunityId.length()>0)
        timeRecord.Opportunity__c = opportunityId;
        timeRecord.Task_Hours__c = hourTime ;
        timeRecord.Task_Minutes__c = minTime ;
        timeRecord.Task_Second__c = secTime ;
        timeRecord.Total_Time_In_Seconds__c = totalsecond ;
        timeRecord.Activity_Type__c = 'Track Activity';
     
     
        System.debug('stController==='+stController);
        upsert timeRecord;
     
        return null;
    }
 
    public PageReference saveRecord(){
 
        System.debug('Inside Record Save');
     
        try{
         System.debug('caseId: '+caseId+' length: '+caseId.length());
         System.debug('leadId: '+leadId+' length: '+leadId.length());
         System.debug('opportunityId: '+opportunityId+' length: '+opportunityId.length());
        }catch(Exception ex){
            System.debug('Exception: '+ex);
        }
        Time_Service__c timeObjectRecordst;
       /* if(timeObjectRecord!=null)
            timeObjectRecordst=timeObjectRecord;
        else*/
          timeObjectRecordst = (Time_Service__c)stController.getrecord();
     
        list<String> parts = timeBundle.split('#');
     
        secTime = integer.valueOf(parts[0]);
        minTime = integer.valueOf(parts[1]);
        hourTime = integer.valueOf(parts[2]);     
        totalsecond = (3600*hourTime + 60*minTime + secTime);
     
     
        if(timeObjectRecordst.Activity_Description__c == null
           || timeObjectRecordst.Activity_Description__c.equals('')
           ||  timeObjectRecordst.Comments__c == null
           || timeObjectRecordst.Comments__c.equals('')){
                System.debug('Inside Error Message');
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.warning,'Please fill the required fields before saving the record');
            ApexPages.addMessage(myMsg);
           return null ;
     
        }

  Time_Service__c timeRec;
       /* if(timeObjectRecord!=null)
            timeRec=timeObjectRecord;
else */
        timeRec = (Time_Service__c)stController.getRecord();
       if(caseId!=null && caseId.length()>0)
        timeRec.case__c = caseId;
        if(leadId!=null && leadId.length()>0)
        timeRec.Lead__c=leadId;
        if(opportunityId!=null && opportunityId.length()>0)
        timeRec.Opportunity__c=opportunityId;
       // timeRec.Activity_Description__c= caseId;
        timeRec.status__c= 'Completed';
        timeRec.Task_Hours__c= hourTime;
        timeRec.Task_Minutes__c= minTime ;
        timeRec.Task_Second__c= secTime ;
        timeRec.Total_Time_In_Seconds__c= totalsecond;
        timeRec.Activity_Type__c = 'Track Activity';
     
        System.debug('stController====='+stController);
        upsert timeRec;
     
     
       // pageReference p = new pageReference('/apex/CaseView?id='+caseId);
       // return p;
       return null;
    }
 
    public pageReference cancel(){   
        pageReference p1;
        if(caseId!=null && caseId.length()>0)
            p1 = new pageReference('/'+caseId);
        else if(leadId!=null && leadId.length()>0)
            p1 = new pageReference('/'+leadId);
        else if(opportunityId!=null && opportunityId.length()>0)
            p1 = new pageReference('/'+opportunityId);     
        return p1; 
    }
  /*  public void setupTimeService(){
        if(leadId!=null){
            //create new record for lead
            timeObjectRecord=new Time_Service__c();
            timeObjectRecord.Lead__c=leadId;
            insert timeObjectRecord;
        }
    }*/
}



No comments:

Post a Comment