Sunday 29 May 2011

Chatter Autofollow Users

The subject of today's blog post is auto-following users.  This can be useful if you have people who are important with your organization (or think they are and you have no choice but to agree with them) and you want to ensure that all new users follow them in chatter. Regular readers of this blog will no doubt be surprised to see that there is no Visualforce aspect to this!

The first part of the solution is to create a public group containing the Users to be followed - I've chosen to call mine "Auto Follow Users" and the only important user is me!

Next up is an after insert trigger on the user object, as shown below:

trigger ai_user on User (after insert) 
{
   ChatterAutofollow.AutofollowUsers(trigger.newMap.keySet());
}

Not much to the trigger as I'm handing off to a utility class method to actually carry out the work. Usually this is a matter of personal preference, but in this case it has to be done that way for reasons which will be explained later.

The utility class is shown below:

public class ChatterAutofollow
{
   // constant group name
   public static final String AUTO_FOLLOW_GROUP='Auto Follow Users';
 
 
   // method has to be @future as cannot mix setup/non-setup DML in the same transaction      
   @future
   public static void AutofollowUsers(Set<Id> userIds)
   {
      // set up the users that should automatically be followed
      List<User> usersToFollow=new List<User>();
 
      Group autoFollowGroup=[Select Id From Group Where Name = :AUTO_FOLLOW_GROUP];
    
      List<GroupMember> members=[Select Id, UserOrGroupId From GroupMember Where GroupId = :autoFollowGroup.id];
      List<EntitySubscription> entSubs=new List<EntitySubscription>();
    
      // loop the users that have been created
      for (Id userId : userIds)
      {
         // Loop through all members in a group
      for (GroupMember member : members)
      {
     EntitySubscription entSub = new EntitySubscription (parentId = member.UserOrGroupId,
                                     subscriberid = userId);
       entSubs.add(entSub);
       }
      }
      insert entSubs;
   }
}

The method has the @future annotation, and this is the reason why the work has to be done by a utility class. Apex doesn't allow setup (i.e. User) DML to be mixed with non-etup (i.e. chatter EntitySubscription) in the same transaction. While I'm not carrying out any setup DML in my code, as this trigger is invoked from the insertion of a User, another part of the platform certainly has done this. Thus the insertion of the EntitySubscriptions takes place asynchronously in a separate transaction.

The method itself is pretty straightforward. First the group details are retrieved:

Group autoFollowGroup=[Select Id From Group Where Name = :AUTO_FOLLOW_GROUP];
    
List<GroupMember> members=[Select Id, UserOrGroupId From GroupMember Where GroupId = :autoFollowGroup.id];

Next the subscription objects are created, by iterating the inserted user ids and for each of these, iterating the auto follow group members and creating an EntitySubscription to their feed. In accordance with best practice and being able to handle bulk mode triggers, the subscriptions are added to a list and inserted en-masse at the end of the processing.



for (Id userId : userIds)
// loop the users that have been created
{
   // Loop through all members in a group
   for (GroupMember member : members)
   {
     EntitySubscription entSub = new EntitySubscription (parentId = member.UserOrGroupId,
                                     subscriberid = userId);
     entSubs.add(entSub);
   }
}
insert entSubs;

To check everything works as expected,  I create a new user called Fred Blogger and receive a confirmation email that he is now following me:

Saturday 21 May 2011

Onload Handling

There's been a few posts around page onload handling on the Visualforce discussion board in the last couple of weeks, specifically around breaking existing Salesforce behaviour when adding a custom onload handler.

An example of this is adding a custom onload handler to a Visualforce page that contains a Date input field.

Here's my original page with no custom onload handler:

<apex:page standardController="Opportunity">
   <apex:form >
      <apex:pageBlock mode="detail">
         <apex:pageBlockSection columns="2">
            <apex:inputField value="{!Opportunity.Name}" />
            <apex:inputField value="{!Opportunity.AccountId}" />
            <apex:inputField value="{!Opportunity.CloseDate}" />
            <apex:inputField value="{!Opportunity.Amount}" />
         </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
</apex:page>

As expected, when I click into the Close Date field, the date picker appears and I can select the appropriate date:




However, if I add the following javascript to the bottom of the page, specifying my custom onload handler, things go awry:


<script>
      window.onload=function()
      {
         alert('My onload handler');
      };
</script>



The alert displays as expected:


However, once I've okayed the alert, I find that clicking into the Close Date field no longer displays a date picker.  It looks from this that the date picker is added through a javascript onload handler, which I have  overwritten with my onload function.

The solution is to check for an existing onload function before specifying my own, and if there is one already defined, append my function to that.

Here's the javascript to do exactly that:

function addLoadEvent(func) 
{ 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') 
  { 
     window.onload = func; 
  } 
  else 
  { 
      window.onload = function()  
      { 
        if (oldonload) 
        { 
           oldonload(); 
        } 
        func(); 
      } 
   } 
} 
  
addLoadEvent(function()  
{ 
   alert('My onload handler');
});

The addLoadEvent function checks if there is an onload handler specified, and if there isn't simply sets it to my function.  If there is one, a new function is created that invokes the existing onload handler followed my function and this is added as the onload handler.

Thus I end up with a chain of functions being executed when the page is loaded, starting with that defined by Salesforce.

Saturday 14 May 2011

Updating Attributes in Component Controller

A while ago I was working on a  component that would allow a user to select/input a number of data values, and return these as a semi-colon separated String to the controller of the parent page.

So I created my component, taking the String as an attribute and assigning it to a variable in the component's controller.

Component:

<apex:component controller="StringEntryController">
   <apex:attribute type="String" name="theString" description="The string to update" assignTo="{!stringVal}"/>
   <apex:outputLabel value="Select the string components"/>
   <apex:selectList value="{!chosenVals}" multiSelect="true" size="4">
     <apex:selectOption itemValue="UK" itemLabel="UK"/>
     <apex:selectOption itemValue="USA" itemLabel="USA"/>
     <apex:selectOption itemValue="Canada" itemLabel="Canada"/>
     <apex:selectOption itemValue="France" itemLabel="France"/>
     <apex:selectOption itemValue="Japan" itemLabel="Japan"/>
   </apex:selectList> 
</apex:component>

controller:


public class StringEntryController {
 public String stringVal {get; set;}
 public String[] chosenVals {get; 
                         set {
                                stringVal='';
                                for (String val : value)
                                {
                                 stringVal+=':' + val;
                                }
                           
                             stringVal=stringVal.substring(1);
                             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, ' Set string val to ' + stringVal));
                             }
                         }
 
 
}

I've added a message to the page to confirm that the String has been updated with the user's selections.

Here's the page that accesses the component.

<apex:page controller="PrimitiveCarrierController">
  <apex:form >
   <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockSection columns="1">
        <apex:pageBlockSectionItem >
        <apex:outputText value="The string = {!enteredString}"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
        <c:StringEntry theString="{!enteredString}"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
        <apex:commandButton value="Update"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
   </apex:pageBlock>
  </apex:form>
</apex:page>

And finally the page controller. Note that enteredString is initialised to 'This is the default from the controller' so that I can check that it has changed.

public class PrimitiveCarrierController 
{
 public String enteredString {get; set;}
 
 public PrimitiveCarrierController()
 {
  enteredString='This is the default from the controller';
 }
}

I then access the page, select some values from the component and click the Update button. The
output is as follows:


As you can see, while the value of the String has changed in the component, as is shown by the value in the page message, the value in the page's controller hasn't.

Reading the Apex Developer's Guide indicates that this is because Strings are passed by value rather than by reference. This means that the String that my component controller is working on is different to the one in the page controller, so it doesn't matter what my component controller does, the String from the page controller is unaffected.

The solution is to put the String inside an object, as these are passed by reference rather than by value, so changing the value of the contained String in the component controller affects the actual object instance that the page controller is managing.

Here is the class that contains the String:

public class StringCarrier {

    public String value {get; set;}
}

The revised component:

<apex:component controller="StringEntryControllerV1">
   <apex:attribute type="StringCarrier" name="theString" description="The string to update" assignTo="{!stringVal}"/>
   <apex:outputLabel value="Select the string components"/>
   <apex:selectList value="{!chosenVals}" multiSelect="true" size="4">
     <apex:selectOption itemValue="UK" itemLabel="UK"/>
     <apex:selectOption itemValue="USA" itemLabel="USA"/>
     <apex:selectOption itemValue="Canada" itemLabel="Canada"/>
     <apex:selectOption itemValue="France" itemLabel="France"/>
     <apex:selectOption itemValue="Japan" itemLabel="Japan"/>
   </apex:selectList> 
</apex:component>

The revised component controller:

public class StringEntryControllerV1 {
 public StringCarrier stringVal {get; set;}
 public String[] chosenVals {get; 
                         set {
                                stringVal.value='';
                                for (String val : value)
                                {
                                 stringVal.value+=':' + val;
                                }
                           
                             stringVal.value=stringVal.value.substring(1);
                             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, ' Set string val to ' + stringVal.value));
                             }
                         }
 
 
}

The revised page.

<apex:page controller="PrimitiveCarrierControllerV1">
  <apex:form >
   <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockSection columns="1">
        <apex:pageBlockSectionItem >
        <apex:outputText value="The string = {!enteredString.value}"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
        <c:StringEntryV1 theString="{!enteredString}"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
        <apex:commandButton value="Update"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
   </apex:pageBlock>
  </apex:form>
</apex:page>

And the revised page controller. Note that enteredString is initialised in the same way as before. Note also that the StringCarrier property must be requires instantiation unlike a String primitive.

public class PrimitiveCarrierControllerV1 
{
 public StringCarrier enteredString {get; set;}
 
 public PrimitiveCarrierControllerV1()
 {
  enteredString=new StringCarrier();
  enteredString.value='This is the default from the controller';
 }
}

And as is shown below, the changes applied by the component propagate to the page:

Saturday 7 May 2011

Refreshing Record Detail from Embedded Visualforce Page

Strange how things go in cycles.  This week there's been several questions on the discussion boards, asking how to refresh the standard record view page from an Visualforce page.  An example of an embedded page is shown below:


The purpose of the embedded page allows the user to choose a country ISO code and when the save button is clicked, populate the account billing country field with the full name of the account.

The controller method to carry out the save:

public PageReference save()
{
 Account acc=(Account) stdCtrl.getRecord();
 acc.BillingCountry=chosenCountry;
  
 return stdCtrl.save();
}


As this is an extension controller, the account is retrieved from the standard controller to update the field. The return value is the result of the standard controller save method, which is the standard record view page.

Unfortunately, as the Visualforce page is embedded in the standard view via an iframe, this simply puts the full record view page inside the existing record view page:


Not exactly what I had in mind!

Unfortunately there's no way to return a page reference to the browser that tells it to refresh the entire page, rather than just the iframe.   It is possible, however, to use javascript to carry out a client side redirect back to the record view.  Thus there needs to be some javascript that is conditionally rendered into the page after the save (and only after the save, otherwise the page will continually refresh) that executes the refresh.

The full revised controller is shown below - the new aspect is the refreshPage property, which is initialised to false in the constructor and set to true when the record is saved. I've also changed the controller to return null, as I need the embedded page to refresh and render the javascript.

public class EmbeddedController
{
 public ApexPages.StandardController stdCtrl {get; set;}
 public String chosenCountry {get; set;}
 public Boolean refreshPage {get; set;}
 
 public EmbeddedController(ApexPages.standardController std)
 {
  stdCtrl=std;
  refreshPage=false;
 }
 
 public PageReference save()
 {
  Account acc=(Account) stdCtrl.getRecord();
  acc.BillingCountry=chosenCountry;
  refreshPage=true;
  stdCtrl.save();
  
                return null;
 }
}


and the important new element in the page:

<apex:outputPanel rendered="{!refreshPage}">
   <script>
      window.top.location='/{!Account.id}';
   </script>
</apex:outputPanel>


Now when I click the save button in the embedded Visualforce page, the entire record view page refreshes and my chosen country appears in the billing address.

Update: 22/12/2012

A number of the comments for this post have asked about achieving this via the service cloud console.  This mechanism won't work I'm afraid, due to the browser same origin policy.  Looking at my javascript error console in chrome, I see 'Unsafe Javascript' exceptions when the refresh method fires.  I've also investigated converting the embedded page to a custom console component and using the integration toolkit to refresh the enclosing tab, but the same errors are thrown, event after whitelisting every domain involved.