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
mTroyanomTroyano 

Next Case button

How can I create "next button" in a case to show me next case? Depending on whether the state field is equal to one will be displayed or hidden the button.
Best Answer chosen by mTroyano
mTroyanomTroyano
I answer myself...

Situation - I have one custom object with name Offer (Offer__C) and I create two custom field (Import__c, Status__c) The propouse of this code is create page navigation between records depending of status. 

STEPS
Step 1 - I create one VisualForce Class "OfferClass.apxc"
Step 2 - I declare variables
 
public with sharing class OfferClass {
    Public Integer size{get;set;}
    private Offer__c currentRecord;
    public String idOffer {get; set;}
    private ApexPages.StandardController stdController;
    
    public OfferClass(ApexPages.StandardController controller) {
        stdController = controller;    
        idOffer = ApexPages.currentPage().getParameters().get('id');
    }
//continue here
}

Step 3 - I create a public boolean for check if exist next or previuos record and for check if we are viewing the first o last record.

public boolean getCheckNext() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved'];
        if (OfferNext.size() > 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

public boolean getCheckPrevious() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferPrevious = [SELECT Id FROM Offer__c WHERE Id < :idOffer AND Status__c = :'Approved'];
        if (OfferPrevious.size() > 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

public boolean getCheckLast() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved'];
        if (OfferNext.size() == 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

Step 4 - I create "public pagereference" for view the next, previous or the first record.
public PageReference doNext(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }

public PageReference doPrevious(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Id < :idOffer AND Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }

public PageReference doFirst(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }
 
Step 5 - I create VisualForce Page "OfferPage.vfp"
<apex:page standardController="Offer__c" extensions="OfferClass">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:panelGrid columns="7">
             <apex:commandButton value="First" action="{!doFirst}" rendered="{!CheckLast}"/>
             <apex:commandButton value="Next" action="{!doNext}" rendered="{!CheckNext}"/>
             <apex:commandButton value="Previous" action="{!doPrevious}" rendered="{!CheckPrevious}"/>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="DetailOffer">    
        <apex:detail subject="{!$CurrentPage.parameters.id}" relatedList="true"  relatedListHover="false"/>
        <chatter:feed entityId="{!$CurrentPage.parameters.id}"/>
    </apex:outputPanel>  
</apex:page>
 
Step 6 - I modified the action View

action view

RESULT

User-added image
User-added image
User-added image

Regards!
 
 

All Answers

Rahul_kumar123Rahul_kumar123
Hi Miguel,
I hope it will be helpful.

Best Regards
RahulKumar
mTroyanomTroyano
I answer myself...

Situation - I have one custom object with name Offer (Offer__C) and I create two custom field (Import__c, Status__c) The propouse of this code is create page navigation between records depending of status. 

STEPS
Step 1 - I create one VisualForce Class "OfferClass.apxc"
Step 2 - I declare variables
 
public with sharing class OfferClass {
    Public Integer size{get;set;}
    private Offer__c currentRecord;
    public String idOffer {get; set;}
    private ApexPages.StandardController stdController;
    
    public OfferClass(ApexPages.StandardController controller) {
        stdController = controller;    
        idOffer = ApexPages.currentPage().getParameters().get('id');
    }
//continue here
}

Step 3 - I create a public boolean for check if exist next or previuos record and for check if we are viewing the first o last record.

public boolean getCheckNext() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved'];
        if (OfferNext.size() > 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

public boolean getCheckPrevious() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferPrevious = [SELECT Id FROM Offer__c WHERE Id < :idOffer AND Status__c = :'Approved'];
        if (OfferPrevious.size() > 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

public boolean getCheckLast() {
        Offer__c RTActual = [SELECT Name, Status__c FROM Offer__c WHERE Id = :idOffer LIMIT 1];
        List<Offer__c> OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved'];
        if (OfferNext.size() == 0 && RTActual.Status__c == 'Approved'){
            return true;
        }
        return false;
    }

Step 4 - I create "public pagereference" for view the next, previous or the first record.
public PageReference doNext(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Id > :idOffer AND Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }

public PageReference doPrevious(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Id < :idOffer AND Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }

public PageReference doFirst(){
        Offer__c OfferNext = [SELECT Id FROM Offer__c WHERE Status__c = :'Approved' ORDER BY Id ASC LIMIT 1];
  
        PageReference pageRef = new PageReference('/apex/OfferPage?id=' + OfferNext.Id);
        pageRef.setRedirect(true);
        return pageRef; 
    }
 
Step 5 - I create VisualForce Page "OfferPage.vfp"
<apex:page standardController="Offer__c" extensions="OfferClass">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:panelGrid columns="7">
             <apex:commandButton value="First" action="{!doFirst}" rendered="{!CheckLast}"/>
             <apex:commandButton value="Next" action="{!doNext}" rendered="{!CheckNext}"/>
             <apex:commandButton value="Previous" action="{!doPrevious}" rendered="{!CheckPrevious}"/>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="DetailOffer">    
        <apex:detail subject="{!$CurrentPage.parameters.id}" relatedList="true"  relatedListHover="false"/>
        <chatter:feed entityId="{!$CurrentPage.parameters.id}"/>
    </apex:outputPanel>  
</apex:page>
 
Step 6 - I modified the action View

action view

RESULT

User-added image
User-added image
User-added image

Regards!
 
 
This was selected as the best answer
Shohrat MuhamovShohrat Muhamov
Hey,

How you can do it Lightning Component?

Best Regards,
Shohrat