0

I tried a lot to resolving this issue but not able to achieving the requirement.Please help me on this

After Clicking a button in Panel 1,It navigates to Panel 2 and Initially it displays panel 2 as empty.after few seconds If i refresh panel 2 It displays data.

Anyone suggest how can I display data immediately after clicking a button without refreshing a page again.

The below code works perfectly.No errors and all.But only the issue is data isn't visible immediately after clicking a button.

page:

            --------------------------------------------Panel-1--------------------------------------------------

            <apex:outputPanel rendered="{!flag1}">               
             <center>
                <apex:commandButton value="Assign Vouchers and Send Email" action="{!process}"   reRender="pg1" styleClass="slds-button slds-button_brand" />

             </center>               
             <center>
                 <b>Generate and Assign Voucher</b>&nbsp;&nbsp;&nbsp;&nbsp;
                  <apex:inputCheckBox value="{!checkBoxval}"></apex:inputCheckBox>
             </center>                       
          </apex:outputPanel>                                 
            --------------------------------------------Panel-2--------------------------------------------------
            <apex:outputPanel id="pg1" >
            <apex:outputPanel rendered="{!flag}" >

            <table class="slds-table slds-table_bordered slds-table_cell-buffer slds-no-row-hover" style="table-layout:fixed; width:100%">
             <thead>
                                <tr class="slds-text-title_caps slds-line-height_reset">
                                    <th scope="col">
                                        <div class="slds-truncate" title="Name">Name</div>
                                    </th>
                                    <th scope="col">
                                        <div class="slds-truncate" title="Email">Email</div>
                                    </th>                               
                                </tr>
                            </thead>
                            <tbody>
                                <apex:repeat value="{!MyResult}" var="c" id="table"> 

                                    <tr>
                                        <td data-label="Name">
                                            <div class="slds-truncate" >{!c.Name}  </div>
                                        </td>
                                        <td scope="row" data-label="Email">
                                            <div class="slds-truncate" >{!c.Login__c}</div>
                                        </td>

                                    </tr>
                                </apex:repeat>
                            </tbody>
                        </table>
                    </div>
                </div>

            Controller :
            --------------
            public with sharing class VoucherController{
            public PageReference process(){ 
                     if(checkBoxVal==true){
                       VoucherGenerate_AssignBatch b = new VoucherGenerate_AssignBatch();
                       Database.executeBatch(b);               
                       flag = true;  
                       flag1 = false ; 

                    }
                }
            }

            Batch Class:
            -------------
            global with sharing class VoucherGenerate_AssignBatch implements Database.Batchable<SObject>, Database.Stateful {   
                global Database.QueryLocator start(Database.BatchableContext bc) {

                    String query = 'SELECT Id,Login__c,Name,Priority__c,Opportunity__c,Opportunity__r.CloseDate, Attandance_Status__c,  WebinarContact__c,WebinarContact__r.Name,WebinarContact__r.Email,'+
                                   'Voucher_Number__c,Email_Sent__c  FROM Webinar_Attendees__c WHERE Opportunity__c != Null  AND Attandance_Status__c = \'Attended\' '; 
                    system.debug('query :: '+query);
                    return Database.getQueryLocator(query);         
                }

                global void execute (Database.BatchableContext bc, List<Webinar_Attendees__c> scope) {

                    integer limitCount;           
                    List<Webinar_Attendees__c> updateWebinarAttendees = new List<Webinar_Attendees__c>();    
                    Map<Id,List<Webinar_Attendees__c>> myl = new Map<Id,List<Webinar_Attendees__c>>(); 
                    Set<Id> oppIds = new Set<Id>();

                    Adobe_Connect__c c = [select id,Name,VouchersPerOpportunity__c,Days__c from Adobe_Connect__c];  
                    system.debug('VouchersPerOpportunity__c custom settings value :: ' +c.VouchersPerOpportunity__c);
                    system.debug('Days__c custom settings value::'+c.Days__c); 
                    if(c.VouchersPerOpportunity__c != Null){
                       limitCount = (Integer)c.VouchersPerOpportunity__c ;      
                    }else{ limitCount = 3; }
                    system.debug('limitCount value :: ' +limitCount) ;   

                    //Changed by pramineni: Fetching webinar attendees ordering by priority instead of createddate and making Priority with null attendees last
                    for(Webinar_Attendees__c wc : scope) {           
                        List<Webinar_Attendees__c> w = [SELECT Id,Login__c,Name,Opportunity__c,Priority__c,Attandance_Status__c,Opportunity__r.CloseDate,Opportunity__r.OppVoucherCount__c,  Webinar__r.IsDeleted__c,
                                                        Webinar__r.IsRestored__c,Webinar__r.Start_Time__c,Webinar__r.Name,WebinarContact__c,WebinarContact__r.Name,WebinarContact__r.Email,Voucher_Number__c,Email_Sent__c
                                                        FROM Webinar_Attendees__c WHERE Opportunity__c = : wc.Opportunity__c  AND Attandance_Status__c = 'Attended'   AND Voucher_Number__c = Null AND Email_Sent__c = false
                                                        ORDER BY Priority__c ASC NULLS LAST];               
                        myl.put(wc.Opportunity__c,w);     
                        system.debug('myl :: ' +myl) ;
                    }

                    for(Id oppId : myl.keySet()){

                        system.debug('oppId from Map :: '+oppId);
                        List<Webinar_Attendees__c> wlst = myl.get(oppId);
                        system.debug('wlst size from Map :: '+wlst.size());
                        system.debug('wlst from Map :: '+wlst);

                        if(wlst.size() > 0){ 

                            Opportunity opp = [SELECT Name,OppVoucherCount__c,StageName,CloseDate FROM Opportunity WHERE Id = : oppId];
                            Integer OppV_sum;
                            if(opp.OppVoucherCount__c == Null){
                                OppV_sum = 0;
                            }else{ OppV_sum = Integer.valueOf(opp.OppVoucherCount__c); }           

                            for(Webinar_Attendees__c wa : wlst){

                                system.debug('attendees webinar startDate'+wa.Webinar__r.Start_Time__c);
                                system.debug('opp close date'+opp.CloseDate);

                                //Changed by pramineni:: logic to check if the fair start date is less than 10 days to the webinar date then the attendee is not allowed a voucher
                                Integer i = wa.Webinar__r.Start_Time__c.date().daysBetween(opp.CloseDate);

                                system.debug('Difference:'+ i);
                                system.debug('OppV_sum value before :: '+OppV_sum);

                                if(i > c.Days__c) {                       
                                if((wa.Webinar__r.IsDeleted__c == false && wa.Webinar__r.IsRestored__c == true) || (wa.Webinar__r.IsDeleted__c == false && wa.Webinar__r.IsRestored__c == false )){

                                    if(OppV_sum < limitCount ){

                                        // if(wc.Voucher_Number__c == Null && wc.Email_Sent__c == false){  condition already present in query                   
                                        try{    
                                             Voucher_Repository__c v1 = new Voucher_Repository__c();
                                             v1.Voucher_Type__c = 'Online';
                                             DateTime dt = Datetime.now();
                                             v1.Voucher_StartDate__c = dt ;
                                             v1.Voucher_EndDate__c = dt.addDays(10);
                                             v1.Voucher_Value__c = 25 ;
                                             Insert v1;    

                                             Voucher_Repository__c v = [SELECT Id,Name,CheckAvailable__c,redeemed_On__c,redeemed_To__c,Voucher_EndDate__c,Voucher_StartDate__c,Voucher_Type__c,
                                                                        Voucher_Validity__c,Voucher_Value__c FROM Voucher_Repository__c WHERE Id = : v1.id];                                

                                             system.debug('New Voucher ::'+v);    
                                             String emailid = wa.Login__c;      //wa.WebinarContact__r.Email ;
                                             system.debug(' email is :: ' +emailid); 

                                            if(v != Null && emailid != Null){

                                                OppV_sum = OppV_sum + 1;
                                                system.debug('OppV_sum value After :: '+OppV_sum);   
                                                v.redeemed_To__c = wa.Id;  
                                                v.redeemed_On__c = wa.Opportunity__r.CloseDate;                                                                
                                                v.CheckAvailable__c = true ;                                            

                                                // Email processing
                                                EmailTemplate template = [select Id,Body,HtmlValue,Name,Subject FROM EmailTemplate WHERE Name =  'MyTemplate2'];    // 'AlocateVoucherTemplate'];                                       
                                                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                               
                                                String[] toAddresses = new String[] {emailid};   
                                                mail.setToAddresses(toAddresses);  
                                                mail.setTemplateId(template.Id);
                                                mail.setSubject('Thanks for attending the '+wa.Webinar__r.Name+' Webinar');   
                                                mail.setReplyTo('XYZ@Salesforce.com'); //Set who the email is sent from
                                                mail.setSenderDisplayName('Salesforce team');                                                               
                                                mail.setTargetObjectId(wa.WebinarContact__c);                               
                                                mail.setUseSignature(false);
                                                mail.setBccSender(false);
                                                mail.setSaveAsActivity(false);                                
                                                String plainBody = template.HtmlValue;   //htmlBody
                                                String ContactName;
                                                if(wa.WebinarContact__r.Name != Null ){
                                                   ContactName = wa.WebinarContact__r.Name;
                                                }else{
                                                   ContactName = wa.Name;
                                                }                                   
                                                plainBody = plainBody.replace('{!Contact.LastName}', ContactName);                               
                                                plainBody = plainBody.replace('{!Voucher_Name}', String.valueof(v.Name) );                                
                                                plainBody = plainBody.replace('{!Voucher_Value}', String.valueof(v.Voucher_Value__c) );                                
                                                Datetime d = v.redeemed_On__c ;                                                                      
                                                plainBody = plainBody.replace('{!redeemed_OnDate}', String.valueof(d.format('MM/dd/yyyy')));                             
                                                plainBody = plainBody.replace('{!Voucher_Validity}', String.valueof(v.Voucher_Validity__c));                               
                                                plainBody = plainBody.replace('{!Voucher_QrCode}',String.valueof(emailid+v.Name+'$'+v.Voucher_Value__c));                                       
                                                //mail.setPlainTextBody(plainBody);
                                                mail.setHTMLBody(plainBody);
                                                system.debug('email body :: ' + plainBody );   

                                                Messaging.SendEmailResult[] results = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail}); 
                                                if (results[0].success) {
                                                    system.debug('The email was sent successfully.');
                                                    wa.Voucher_Number__c = v.Id;
                                                    wa.Email_Sent__c = true ;                                          
                                                }else{ system.debug('The email failed to send:'+ results[0].errors[0].message); }

                                                update v;
                                                updateWebinarAttendees.add(wa);

                                                if(OppV_sum > 0){
                                                    opp.OppVoucherCount__c = OppV_sum;
                                                    update opp;
                                                    system.debug('final value of opp.OppVoucherCount__c :: ' +opp.OppVoucherCount__c);
                                                }                                                                                           
                                            }else{                                                                  
                                               if(emailid == Null){ system.debug('** email value is blank **');  }                               
                                            }

                                        }catch(Exception e){  system.debug('Exception raised :: ' +e);   }                           

                                    }else { system.debug(' Opp voucher count is greater then Limit count '); }                                

                                }else { system.debug(' Webinar IsDeleted && Webinar IsRestored condition not met '); }                                      

                            }else { system.debug('The fair start date is less than 10 days to the webinar date');}
                          }                
                        }            
                    }  

                    update updateWebinarAttendees;  
                    system.debug('WebinarAttendees List updated :: ' + updateWebinarAttendees); 
                    system.debug('WebinarAttendees List updated size :: ' + updateWebinarAttendees.size());                       
                }
                global void finish(Database.BatchableContext batchableContext) {

                }   
            }

enter image description here

0
<apex:commandButton value="Assign Vouchers and Send Email" action="{!process}"  styleClass="slds-button slds-button_brand" />
<apex:actionSupport event="onclick" reRender="pg1" />

Here, you are rerendering pg1 immediately on click. You should rerender after completing action="{!process}". So, you should use reRender directly on apex:commandButton.

<apex:commandButton reRender="pg1" value="Assign Vouchers and Send Email" action="{!process}"  styleClass="slds-button slds-button_brand" />

<apex:outputPanel id="pg2" >
    <apex:outputPanel rendered="{!flag1}">               
         <center>
            <apex:commandButton value="Assign Vouchers and Send Email" action="{!process}"   reRender="pg1" styleClass="slds-button slds-button_brand" />

         </center>               
         <center>
             <b>Generate and Assign Voucher</b>&nbsp;&nbsp;&nbsp;&nbsp;
              <apex:inputCheckBox value="{!checkBoxval}"></apex:inputCheckBox>
         </center>                       
      </apex:outputPanel> 
  </apex:outputPanel>   

then rerender both: reRender="pg1,pg2"

Above will solve prob 1.

Now, for prob 2:

change apex method:

public PageReference process(){ 
    if(checkBoxVal==true){
        VoucherGenerate_AssignBatch b = new VoucherGenerate_AssignBatch();
        Database.executeBatch(b);               
        flag = true;  
        flag1 = false ; 

        String query = 'SELECT Id,Login__c,Name,Priority__c,Opportunity__c,Opportunity__r.CloseDate, Attandance_Status__c,  WebinarContact__c,WebinarContact__r.Name,WebinarContact__r.Email,'+
            'Voucher_Number__c,Email_Sent__c  FROM Webinar_Attendees__c WHERE Opportunity__c != Null  AND Attandance_Status__c = \'Attended\' '; 
        system.debug('query :: '+query);

        Myresult = Database.query(query);

    }
}

batch apex is async so those records will not be immediately available. You need to return direct soql query results

  • Comments are not for extended discussion; this conversation has been moved to chat. – Adrian Larson Aug 14 at 13:33
  • Stop posting comments here. Please use the chat for any continued discussion. – Adrian Larson Aug 14 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.