5

I'm working with an answer submitted here to be able to post to a group and automatically link to an Opportunity. I'm going to use the VF page as a custom Publisher action.

This post was very helpful, but I'm trying to figure out a couple things. 1) is there a way I could hard code the group that the post is pointed to (versus asking user to input the ID). Its always going to be same for this use case. 2) Is there a way to have the opportunity be dynamically be pulled in based on the opportunity record the post is generated from (versus typing in ID). I'm thinking no since the controller used is Chatter? 3) If it is able for the Opp to be pulled in - can I also pull in standard fields to the opportunity (Like Account Name/Amount).

(I'm a Visualforce newbie so be kind).

VISUALFORCE CODE:

<apex:page controller="ChatterPostController">
  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockButtons >
        <apex:commandButton value="Post" action="{!doPost}" />
      </apex:pageBlockButtons>
      <apex:pageBlockSection columns="1">
         <apex:outputLabel value="Path to Win"/>
         <apex:inputText value="{!detail}" style="width: 255px !important;" />
         <apex:outputLabel value="Opportunity ID"/>
         <apex:inputText value="{!OppId}" />
         <apex:outputLabel value="Group ID"/>
         <apex:inputText value="{!groupId}" />
      </apex:pageBlockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page>

CONTROLLER:

public with sharing class ChatterPostController {

    public String detail {get; set;}
   public Id oppId {get; set;}
    public Id groupId  {get; set;}

    public PageReference doPost()
    {
        FeedItem fitem=new FeedItem();
        fItem.parentId=groupId;

        fItem.body=detail + ' Opportunity: ' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + oppId;

        insert fItem;

        return null;
    }
}

1 Answer 1

7

Yes. You can do all of that. Take a look at the help page on creating VF pages for custom actions for some good examples.

You would need to make a Controller Extension to be able to access the Opportunity in context. The controller you posted is a Custom Controller. Once you have the Controller Extension and page written you can then add it as a Object-Specific Action. See the docs on Customer Controllers and Controller Extensions for more information.

You may want to associate the post with your Opportunity rather than the Group. I've included Visualforce pages for both scenarios. If you do want to do the group you can avoid asking for the ID by storing the Group Id in a Custom Setting. Technically, you could just hardcode it in your source, but that's not as maintainable.

Controller Code

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 = Your_Custom_Setting__c.getInstance().Group_Id__c;
    }

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

        // alternatively, if you want this to show up in the
        // Opportunity's feed then change to set parent to OppId
        // fItem.parentId = opp.Id;

        fItem.parentId = groupId;

        fItem.body = detail + ' Opportunity: ' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.Id;

        insert fItem;

        return null;
    }
}

Visualforce Code to use if posting to Group Feed (no refresh needed)

<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="Path to Win"/>
         <apex:inputText value="{!detail}" style="width: 255px !important;" />
      </apex:pageBlockSection>
   </apex:pageBlock>
 </apex:form>

</apex:page>

Visualforce Code to use if posting to Opportunity Feed (refresh needed)

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

  <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/> 
  <script> 
        function refreshFeed() { 
            Sfdc.canvas.publisher.publish({name : 'publisher.refresh', payload : {feed:true}}); 
        }
  </script>   

  <apex:form >
    <apex:pageBlock >
      <apex:pageBlockButtons >
        <apex:commandButton value="Post" action="{!doPost}" oncomplete="refreshFeed();"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection columns="1">
         <apex:outputLabel value="Path to Win"/>
         <apex:inputText value="{!detail}" style="width: 255px !important;" />
      </apex:pageBlockSection>
   </apex:pageBlock>
 </apex:form>

</apex:page>

The second Visualforce page refreshes the feed on the Opportunity in the onComplete function of the apex:commandButton, after the FeedItem is inserted. If that is left out the feed doesn't get updated and the user won't see the FeedItem just created. In the first page you'd be posting to the Group's feed so there's no need to update the Opportunity's feed. That's the only difference between the two Visualforce pages.

Also, make sure that all profiles that need to use the action have access to the Visualforce Page.

3
  • This is great - thanks -- everything works perfectly, only issue now I have is that the Publisher Action (that I added to the Opportunity layout, that all profiles use) is only visible to me, and not other users. I'm assuming its a user permission of some sort? Or related the VF page? I've already looked here, no help. help.salesforce.com/… Dec 19, 2013 at 20:00
  • If you haven't, you might need to give access to the VF page and Apex class to the various profiles: help.salesforce.com/…. Dec 19, 2013 at 20:06
  • that was it - oops rookie move Dec 19, 2013 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .