Chatter Publisher Action redirecting to page within Action Window

I just moved to production a custom VF page and Apex Class, that allows me to create from an Opportunity record, a Chatter post to a Chatter Group, as a custom publisher action. (received much help here for this).

Everything worked great in the Sandbox, and looks good in production except for one thing — once the post is submitted, I want the user’s screen to be completely redirected to the group page.
Instead — only the post window is redirected to the group page (not the entire browser window). (see screenshot)
enter image description here

The strange thing is it works correctly in the sandbox (completely redirecting to group page).

Here is the code:

VISUALFORCE PAGE

<apex:page standardController="Opportunity" extensions="OpportunityChatterPostController" showHeader="false">

  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockButtons >
        <apex:commandButton value="Post" action="{!doPost}"/>

      </apex:pageBlockButtons>

      <apex:pageBlockSection columns="1">
         <apex:outputLabel value="Type Main Contact & Path to Win for {!Opportunity.Name}, Bookings: ${!Opportunity.Amount}" />
         <apex:inputTextArea value="{!detail}" cols="60" rows="20" style="width: 475px !important;" />
      </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>

</apex:page>

APEX CLASS

public with sharing class OpportunityChatterPostController {
    public String detail { get; set;}
    public Opportunity opp { get; set; }
    public String groupId { get; set; }
    public String message { get; set; }

    public OpportunityChatterPostController(ApexPages.StandardController controller) {
        opp = (Opportunity) controller.getRecord();

        // get this value from a Custom Setting
        groupId = Win__c.getInstance().WAGroupID__c;
    }

    public PageReference doPost() {
        FeedItem fItem = new FeedItem();


        fItem.parentId = groupId;

        fItem.body =  'WIN! ' + opp.Name + ' $' + opp.Amount + '. ' +  detail + '. Opportunity: ' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.Id;

        insert fItem;


        PageReference pageRef = new PageReference('/' + groupID)

        ;
        return pageRef;



    }
}

Answer

I’m surprised that it works in the sandbox, as the publisher action is presented in an iframe and so any refresh/redirect that takes place should be entirely contained in the iframe.

The way that I handle this is to set a property in the controller to indicate that the post was successful, then have some JavaScript that is rendered based on the property which refreshes the entire page using window.top.location. It does mean that the page gets refreshed prior to the redirect, so I sometimes put out a message that the post was successful so that the user knows what is going on.

Controller:

public with sharing class OpportunityChatterPostController {
    public String detail { get; set;}
    public Opportunity opp { get; set; }
    public String groupId { get; set; }
    public String message { get; set; }

    // indicator that the post is successful
    public Boolean posted {get; set;}

    public OpportunityChatterPostController(ApexPages.StandardController controller) {
        opp = (Opportunity) controller.getRecord();

        // get this value from a Custom Setting
        groupId = Win__c.getInstance().WAGroupID__c;

        posted=false;
    }

    public PageReference doPost() {
        FeedItem fItem = new FeedItem();


        fItem.parentId = groupId;

        fItem.body =  'WIN! ' + opp.Name + ' $' + opp.Amount + '. ' +  detail + '. Opportunity: ' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.Id;

        insert fItem;

        posted=true;

        return null;  // refresh the page
    }

Page:

<apex:page standardController="Opportunity" extensions="OpportunityChatterPostController" showHeader="false">

  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockButtons >
        <apex:commandButton value="Post" action="{!doPost}"/>

      </apex:pageBlockButtons>

      <apex:pageBlockSection columns="1">
         <apex:outputLabel value="Type Main Contact & Path to Win for {!Opportunity.Name}, Bookings: ${!Opportunity.Amount}" />
         <apex:inputTextArea value="{!detail}" cols="60" rows="20" style="width: 475px !important;" />
      </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>

  <!-- redirect to the group page -->
  <apex:outputPanel rendered="{!posted}">
   <script>
      window.top.location='/{!groupId}';
   </script>
 </apex:outputPanel>

</apex:page>

Its basically the same concept as refreshing a record detail page from an embedded Visualforce page – I’ve explained that scenario in more detail in a blog post at:

http://bobbuzzard.blogspot.co.uk/2011/05/refreshing-record-detail-from-embedded.html

Attribution
Source : Link , Question Author : OfficeGnome , Answer Author : Bob Buzzard

Leave a Comment