function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DesaiDesai 

Update in Apex class

Hi,

We have written Apex class which updates the event record when custom VF refresh button is clicked but the record is not updated immediately. When we have to refresh the browser with the record id, updated data shown is.

public void Test()
   {    
       Event E = [SELECT Id, TOA_Appointment_ID__c,TOA_Appointment_Status__c FROM EVENT WHERE Id = '00Uj000000AMapq'] ;
       List<String> returnvalue = new List<String>();
       toaGetApptUpdates TU = new toaGetApptUpdates ();  // call class
       returnvalue = TU.GetAptInfo(E.TOA_Appointment_ID__c);  // invoke the class method

       String vStat = returnvalue.get(0);  // Store the response values
       String vTrvlTime = returnvalue.get(1);
       String vResId = returnvalue.get(2);
       String vResName = returnvalue.get(3);
      
           E.TOA_Appointment_Status__c = vStat;   // assign the response values
           E.TOA_Traveling_Time__c = vTrvlTime;
           
           E.TOA_Resource_Name__c = vResName;
           
          
            update E;  // update the event record.
          }

VF page for event detail record :

<apex:page tabStyle="Appointment__tab" standardController="Event" extensions="cController"  sidebar="true" >
<script  type="text/javascript">
    function Testmy()
    {
        alert("Hello");
        callt();
    
    }
    </script>

    <apex:form >
     <apex:actionFunction name="callt" action="{!Test}"  />

    <style type="text/css">
         h { font-weight: bold; position: relative; left:8px; font-size:11px }
         
         p { font-weight: bold; position: relative; left:36px; bottom:10px; font-size:13px }
         
        .pbHeader
        {
          font-size:10px  ;
        }
        
        .  ext-webkit ext-chrome
        {
            background:#ececec;
        }
        
         h3
         {
             color: #222;
          }
       
    } 
    </style>
   
    <img id="theImage" src="/resource/home28" style="position:relative; top:10px; width:26px; "></img>
    <h>Appointment</h>
    <p>{!event.Subject}</p>
     <apex:pageBlock title="Appointment Detail" >
     <apex:pageBlockButtons location="top">
            <apex:commandButton value="Edit" action="{!edit}" style="font-size:11px"/>
            <apex:commandButton value="Delete" action="{!delete}" style="font-size:11px"/>
            <apex:commandButton value="Refresh"   onclick="Testmy()" style="font-size:11px" reRender="pgsec" />
    </apex:pageBlockButtons>
        
        <apex:pageBlockSection title="Calendar Details" columns="2" id="pgsec">
           <apex:pageBlockSectionItem >
                Assigned To
                 <apex:outputLink value="{!URLFOR($Action.User.View,Event.Owner)}}">
                     <apex:outputfield value="{!event.Owner.Name}" />
                </apex:outputLink>
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                Related To
                <apex:outputlink onclick="window.location='/{!event.What}';"  value="#">{!event.What.Name}</apex:outputlink>
            </apex:pageBlockSectionItem>
           <apex:outputText value="{!event.Subject}"></apex:outputText>
            
            <apex:pageBlockSectionItem >
                Name
                 <apex:outputLink onclick="window.location='/{!event.Who}';" rendered="{!NOT(ISNULL(event.Who))}" value="#">{!event.Who.Name}</apex:outputLink>
            </apex:pageBlockSectionItem>
            <apex:outputText label="Start" value="{0,date,MM/dd/yy hh:mm a}"> <apex:param value="{!event.StartDateTime}" /></apex:outputText>
            <apex:outputText value="{!event.TOA_Work_Type__c}"></apex:outputText>
            <apex:outputText value="{0,date,MM/dd/yy hh:mm a}" label="End"> <apex:param value="{!event.EndDateTime}" /></apex:outputText>
             <apex:outputText value="{!event.TOA_Time_Slot__c}"></apex:outputText>
           <apex:outputText id="TOAID" value="{!event.TOA_Appointment_ID__c}" ></apex:outputText>
           <apex:outputText value="{!event.TOA_Resource_Name__c}"></apex:outputText>
           <apex:outputText value="{!event.TOA_Appointment_Status__c}"></apex:outputText>
           <apex:outputText value="{!event.TOA_Traveling_Time__c}"></apex:outputText>
           
            
         </apex:pageBlockSection>
         
        <apex:pageBlockSection title="Appointments">
         
         <iframe src=".../VikTableEffects?id={!event.Id}" style="width: 786px; height: 200px; border:0px"/>
        
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Thanks,
Pallavi
Best Answer chosen by Desai
Veenesh VikramVeenesh Vikram
Hi Pallavi,

Update your Apex method like below:
public pagereference Test()
   {    
       Event E = [SELECT Id, TOA_Appointment_ID__c,TOA_Appointment_Status__c FROM EVENT WHERE Id = '00Uj000000AMapq'] ;
       List<String> returnvalue = new List<String>();
       toaGetApptUpdates TU = new toaGetApptUpdates ();  // call class
       returnvalue = TU.GetAptInfo(E.TOA_Appointment_ID__c);  // invoke the class method

       String vStat = returnvalue.get(0);  // Store the response values
       String vTrvlTime = returnvalue.get(1);
       String vResId = returnvalue.get(2);
       String vResName = returnvalue.get(3);
      
           E.TOA_Appointment_Status__c = vStat;   // assign the response values
           E.TOA_Traveling_Time__c = vTrvlTime;
           
           E.TOA_Resource_Name__c = vResName;
           
          
            update E;  // update the event record.
            pagereference p = new pagereference('/apex/YOUR_PAGE_NAME');        //This will refresh page with updated value
            p.setRedirect(true);
            return p;
          }

Thanks and Regards
Veenesh

All Answers

Veenesh VikramVeenesh Vikram
Hi Pallavi,

Update your Apex method like below:
public pagereference Test()
   {    
       Event E = [SELECT Id, TOA_Appointment_ID__c,TOA_Appointment_Status__c FROM EVENT WHERE Id = '00Uj000000AMapq'] ;
       List<String> returnvalue = new List<String>();
       toaGetApptUpdates TU = new toaGetApptUpdates ();  // call class
       returnvalue = TU.GetAptInfo(E.TOA_Appointment_ID__c);  // invoke the class method

       String vStat = returnvalue.get(0);  // Store the response values
       String vTrvlTime = returnvalue.get(1);
       String vResId = returnvalue.get(2);
       String vResName = returnvalue.get(3);
      
           E.TOA_Appointment_Status__c = vStat;   // assign the response values
           E.TOA_Traveling_Time__c = vTrvlTime;
           
           E.TOA_Resource_Name__c = vResName;
           
          
            update E;  // update the event record.
            pagereference p = new pagereference('/apex/YOUR_PAGE_NAME');        //This will refresh page with updated value
            p.setRedirect(true);
            return p;
          }

Thanks and Regards
Veenesh
This was selected as the best answer
DesaiDesai
Hi Veenesh,

Thanks for the update. With the above code it was still not showing the updated data. So i modified the above code by passing the id of the record and it worked.

pagereference p = new pagereference('/apex/EventDetail');
       p.setRedirect(true);
       p.getParameters().put('id', sIdev );
       return p;

Thanks,
Pallavi