1

I am trying to conditionally refresh my Case page depending on if the particular case has been updated or not. I use the below JavaScript and mark-up code to refresh my page

<apex:page controller="MyClass"  showHeader="True" sidebar="false">    
    <apex:includeScript value="/support/console/36.0/integration.js"/>
    <script type="text/javascript">
        function RefreshPrimaryTab()     
        {
            if (sforce.console.isInConsole())
            sforce.console.getFocusedPrimaryTabId(showTabId);
            else
            window.top.location.href = '/' + id;
        }
            var showTabId = function showTabId(result) 
            {
            debugger;
                var tabId = result.id;              
                if ("{!IsModified}" == 'true') {                    
                sforce.console.refreshPrimaryTabById(tabId , true);  
                }               
            };

    </script>
    <apex:form id="theForm">
        <apex:pageBlock id="thePage">
            <apex:outputPanel id="msgs">
                <apex:pageMessages /> 
            </apex:outputPanel>           
            <apex:pageBlockButtons location="bottom" >
                <apex:commandButton value="Parent Case" action="{!processSelected}" oncomplete="RefreshPrimaryTab(); return false" rerender="msgs"/>
            </apex:pageBlockButtons>...

And my apex controller is as follows

public with sharing class MyClass {    
    public List<string> isModified {get;set;}
...
    public PageReference processSelected() {
        String modified;
        isModified=new List<String>();
        try{    
        selectedCases = new List<String>(); 
        for(getRelatedCases selCaseObj : relatedcases) {
            if(selCaseObj.selected == true) {
                selectedCases.add(selCaseObj.cid.CaseNumber);
                String fields = 'parentid';
                String curcaseid = ApexPages.currentPage().getParameters().get('Id');
                Case curCase = [SELECT id FROM Case WHERE Id=:curcaseid];
                curCase.put (fields, selCaseObj.cid.id);
                update curCase;              
                modified='true';                
            }
        } 
        }
        catch(DmlException e){
            modified='false';
            ApexPages.Message  msg = new ApexPages.Message(ApexPages.Severity.ERROR, e.getdmlMessage(0));
            ApexPages.addMessage(msg);
            //return NULL;           
        }      
        isModified.add((String) modified);
        System.debug('Is updated?: ' + isModified);
        return NULL;
    }   
...

So basically my method should either update a case and then JavaScript reload the page or throw the exception message without reloading the page.

Everything works fine if I simply run sforce.console.refreshPrimaryTabById(tabId , true); without checking if a case has been updated or not. But my page is getting reloaded every time along with the exception message. I need to avoid this. I am new with SalesForce so any help will be highly appreciated.

  • You can do this? public List isModified {get;set;}? – EricSSH Oct 17 '16 at 20:43
  • This has been changed as follows public List<string> isModified {get;set;} – Andrew Oct 17 '16 at 20:58
0

Here is a simplified example, Class-

public class ComputePropertyOnRerender {

    public Boolean isSuccess {get;set;}

    public ComputePropertyOnRerender() {}

    public void processSelected() {

        isSuccess = false;

        try {
            // uncomment following line to see the isSuccess as false 
            // throw new CustomException('Adding custom error!');

            isSuccess = true;
        } catch(Exception objEx) {
            ApexPages.addMessages(objEx);
        }

    }

    public class CustomException extends Exception {}
}

Page:

<apex:page standardController="Account" extensions="ComputePropertyOnRerender">
    <apex:form id="the-form">
        <apex:pageMessages/>

        <apex:pageBlock>
            <apex:pageBlockButtons>

                <!-- here you do not need return false, and rerender form and pass isSuccess -->
                <apex:commandButton 
                    value="Save Account" 
                    action="{!processSelected}" 
                    oncomplete="alert('isSuccess: {!isSuccess}');" 
                    rerender="the-form"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                isSuccess: {!isSuccess}
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

You do not need return false in command button if you are using rerender.

If the section containing property is rerendered, it would take latest value from controller. Better solution is to pass the required property as a parameter to the function.

**** Updating answer based on your example:

function refreshPrimaryTab() { 
    if (sforce.console.isInConsole()) { 
        sforce.console.getFocusedPrimaryTabId(showTabId); 
    } else {
        window.top.location.href = '/' + id; 
    }

} 

var showTabId = function showTabId(result) { 
    var tabId = result.id; 
    sforce.console.refreshPrimaryTabById(tabId , true); 
};

<apex:commandButton value="Parent Case" action="{!processSelected}" 
  oncomplete="if({!isSuccess}) { refreshPrimaryTab(); }" rerender="the-Form"/>
| improve this answer | |
  • Thanks! Can you please give me an example how to pass the apex property to the JavaScript function. I am still stragling to get expected behaivour. – Andrew Oct 20 '16 at 11:14
  • function RefreshPrimaryTab(isSuccess) { if (sforce.console.isInConsole()) sforce.console.getFocusedPrimaryTabId(showTabId); else window.top.location.href = '/' + id; } var refreshSuccess=isSuccess; var showTabId = function showTabId(result) { var tabId = result.id; if (refreshSuccess==true) { sforce.console.refreshPrimaryTabById(tabId , true); } }; – Andrew Oct 20 '16 at 11:27
  • <apex:commandButton value="Parent Case" action="{!processSelected}" oncomplete="RefreshPrimaryTab({!isSuccess});" rerender="the-Form"/> – Andrew Oct 20 '16 at 11:28
  • I tried passing the property to the function as shown above. But this does not work. This function should refresh console primary tab when the property is True. – Andrew Oct 20 '16 at 11:30
  • try this in your oncomplete of command button: if({!isSuccess}) { RefreshPrimaryTab(); } – Raul Oct 20 '16 at 11: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.