0

I've got a visualforce page with a controller, and I've been looking for a way to run a javascript function from the controller if there's a new case.

So far I have the following code:

VisualForce Page:

<apex:form >
<apex:inputHidden value="{!caseList1stLinePrevious}" />
<apex:inputHidden value="{!caseList2ndLinePrevious}" />
<apex:inputHidden value="{!caseList3rdLinePrevious}" />
</apex:form>

VisualForce Controller:

public list<case> caseList1stLine{get;set;}
public list<case> caseList2ndLine{get;set;}
public list<case> caseList3rdLine{get;set;}


public case caseList1stLinePrevious{get;set;}
public case caseList2ndLinePrevious{get;set;}
public case caseList3rdLinePrevious{get;set;}

 public Controller()
{
       caseList1stLine = new list<case>();
       caseList1stLine=[SELECT CaseNumber, SuppliedEmail,    Day_Created__c,Days_Open__c, Subject FROM Case WHERE Owner.Type = 'Queue' AND Owner.Name = 'IT First Line' ORDER BY Days_Open__c ASC];
       caseList2ndLine = new list<case>();
       caseList2ndLine=[SELECT CaseNumber, SuppliedEmail, Day_Created__c,Days_Open__c, Subject FROM Case WHERE Owner.Type = 'Queue' AND Owner.Name = 'IT Second Line' ORDER BY Days_Open__c ASC];
       caseList3rdLine = new list<case>();
       caseList3rdLine=[SELECT CaseNumber, SuppliedEmail, Day_Created__c,Days_Open__c, Subject FROM Case WHERE Owner.Type = 'Queue' AND Owner.Name = 'IT Third Line' ORDER BY Days_Open__c ASC];



       checkUpdated();

       // Save most recent cases to var
       caseList1stLinePrevious = caseList1stLine[0]; 
       caseList2ndLinePrevious = caseList2ndLine[0];
       caseList3rdLinePrevious = caseList3rdLine[0];


}


public void checkUpdated()
{
    if(caseList1stLine[0] == caseList1stLinePrevious || caseList2ndLine[0] == caseList2ndLinePrevious || caseList3rdLine[0] == caseList3rdLinePrevious)
    {

        calljavascript_cls();

    }

}

public void calljavascript_cls()
{
    callfunc='<script> playSound(); </script>';
}

The Javascript function calling works fine when debugging, so I think the most recent cases aren't getting stored between page refreshes. Does anybody have any idea/input on this matter?

Thanks,

Sam

1

You generally shouldn't try to insert Javascript with your controller like this.

Instead, you could change checkUpdated() method to set a boolean that's accessible to the page:

public Boolean isUpdated { get; private set; }

public void checkUpdated()
{
    isUpdated = false;

    if(caseList1stLine[0] == caseList1stLinePrevious || caseList2ndLine[0] == caseList2ndLinePrevious || caseList3rdLine[0] == caseList3rdLinePrevious)
    {

        isUpdated = true;

    }

}

Then in your Visualforce page, call your JS method if the boolean is true.

<script> 
  if({!isUpdated}) {
    playSound(); 
  }
</script>
  • Thanks Rob, I've implemented this now. I still can't seem to get the isUpdated to update to true when there's a new case. Am I using the hidden fields correctly? – user24634 Sep 17 '15 at 14:27
  • It's hard to tell from the information above. What do you mean when you say you want to save the information "between page refreshes"? When would the page refresh? From the code you posted above, it looks like you're only calling checkUpdate() from your constructor - so it will only run when the page first loads. If you want to make the check again based on a user action, you're going to need to call it again. – Rob Sep 18 '15 at 7:17
  • Thanks Rob. I switched to using actionPoller instead and I no longer need to do a full page refresh. Thanks for your help. – user24634 Sep 18 '15 at 8:50
  • Great, glad to hear you got it working! – Rob Sep 18 '15 at 9:15

Your Answer

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