5

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;



    }
}
  • 1
    @BobBuzzard has an elegant solution. If you were using the development mode footer to develop, the "core.apexpages.devmode.url=1" parameter being part of the form submission url might have been causing it to be allowed in your sandbox. – Peter Knolle Jan 9 '14 at 9:25
3

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

  • that worked - I though maybe it was iframe related but couldn't figure out why it was working in the sandbox. Strange! Helpful link - and you inspired the original iteration of the code too! Thanks a lot. – OfficeGnome Jan 9 '14 at 9:25

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.