3

I have a tricky requirement that I am not sure is entirely possible.

I have a custom "Skip" button on the Case page layout. This button allows you to skip to the next Case in an list, if the status is equal to a certain value.

I am able to move to the next position in the list once the button has been clicked, BUT once the page redirects to the next Case (position 1 in the array), there is no way to increment the position.

I need to increment the position after the button click, and the page refresh, so that the next time I click the button, I am able to hit position 2 of the array, and I click the button, the page refreshes, I get position 3 etc...until the end of the array which I will then redirect the page back to the initial landing page.

I have thought of looping through the array and increment the value there but this does not work because the page refreshes once you redirect the page which then resets the incremented value back to 1.

I have thought of saving the incremented value into a object in Salesforce but I can't do this because I want to start fresh, at position 1, when ever a user hits the initial landing page.

Any advice on how to increment the position in the array so I am able to show the next Case in the list?

BUTTON:

<apex:page id="skipCase" standardController="Case" extensions="ACHBounceController" action="{!skipCase}">

APEX:

public with sharing class ACHBounceController {

public List<Case> openCases {get;set;}
public static Integer i;

public ACHBounceController(ApexPages.StandardController controller){
    openCases = [SELECT Id, Status, CaseNumber FROM Case WHERE (RecordType.Name = 'ACH Bounce' AND (Status = 'Sent to Accounting' OR Status ='On Hold')) ORDER BY Last_Stage_Change_Date__c DESC];
}
public PageReference skipCase() {
    if((openCases[0].Status == 'Sent to Accounting') || (openCases[0].Status == 'On Hold')){
            i = 1;
            PageReference ReturnPage = new PageReference('/' + openCases[i].Id);
            ReturnPage.setRedirect(true);
            return ReturnPage;
            i = i + 1;
    }
    return null;
}
}

        //for(Integer i = 1; i >= openCases.size(); i++){
        //    PageReference ReturnPage = new PageReference('/' + openCases[i].Id);
        //    ReturnPage.setRedirect(true);
        //    return ReturnPage;
        //    break;
        //}
5
  • Instead of using an integer index, you could loop through your queried list until you find the Case you just redirected from, and then use the next one in the list after that. You just need to make sure you pass the originating Case Id to your VF page. – Jeremy Nottingham Apr 13 '17 at 21:27
  • How about using a cookie to remember where you were last? – sfdcfox Apr 13 '17 at 21:31
  • Set redirect to false and remove static from the integer. This of course is only based on code presented so with additional context it may not be valid – Eric Apr 13 '17 at 21:36
  • @JeremyNottingham once I located the ID that matches the Case I am redirecting from, how would I find the next location in the list? As far as I know there is no way to find the index position in a list with apex – Olivia Apr 14 '17 at 14:56
  • @Olivia, start a counter before you query for the list, and increment as you go through the list. When you find your Case, check the counter. Then return openCases[counter + 1] – Jeremy Nottingham Apr 16 '17 at 19:18
4

I will suggest you to leverage Session Cache. Refer Store and Retrieve Values from the Session Cache.

To store counter in cache use:

Cache.Session.put(UserInfo.getUserId(), counter);

To retrieve counter from cache:

if (Cache.Session.contains(UserInfo.getUserId()))
{
    Integer counter = (Integer)Cache.Session.get(UserInfo.getUserId());
}

To remove entries from cache:

Cache.Session.remove(UserInfo.getUserId());

In those above example I have shown the userId as key. You can put necessary key for your purpose.

1
  • good suggestion, I would really like to try and avoid this if possible because I will have to write something to clear out the cookie once the user goes to any object other then the Case object. But it is still an option! Thank you for your feedback – Olivia Apr 14 '17 at 15:00
0

The above answers are correct but I wanted to avoid using cache so I found a simple way to get this to work.

I sorted my SOQL list in ASC order by last modified date. I put a hidden checkbox on the object and ran some logic that set the checkbox to TRUE if the case was skipped. This allowed a change to take place, which changed the last modified date and put the case at the bottom of the list. I filtered my query to not include the cases that had a value of TRUE for the checkbox. This allowed me to end the list once all cases had been skipped.

Once there were no more Cases in the list, I redirected the user back to the start page that has a 'Start' button on it. Once the user clicks the start button, all Case checkbox values are set back to FALSE. This allows the user to go through the list again and have the ability to skip the case again if needed.

The point of this functionality is to show a user a list of Cases that needed the field status to be updated. With the ability to skip a case they didn't want to update, the user is able to stay on the Case page and be redirected to the next Case in the list without having to leave the page.

This was a good work around for me! I am going to mark Santanu's answer correct because this is definitely one great way to complete my original question.

3
  • How did you solve for the next time someone goes through your Cases are you setting them to false every time the page loads? So is that flag you build always going to be true the first time someone goes through them – EricSSH Apr 17 '17 at 19:05
  • @EricSSH I update my answer with more details. There is a "start" button on the landing page that sets the values back to false when the user wants to start again. So the flag will always be set to false when the user starts going through the list again. – Olivia Apr 17 '17 at 19:56
  • What if multiple users are using this and they click start and now they are resetting each other, just something to think about.. This may not be a concern, but you may run into some context issues – EricSSH Apr 17 '17 at 19:59

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.