Showing posts with label Visualforce Angular. Show all posts
Showing posts with label Visualforce Angular. Show all posts

Monday, June 22, 2015


So finally the Part 2 of the #NoController series , In this part we will try to update the records that were retrieved from the Salesforce using SObject Remote.

If you haven't gone through the Part 1 of this , you can have a look here


Updating Records


  • Will be using update method of sObject Remote
  • Records will be updated as soon as the input elements losses focus (blur)

After all this was not that complicated :-)

Friday, April 3, 2015


Finally back after long long time!

I have been working with AngularJs for a while now and I find Angular to be really helpful to create stateless interactive pages and best part is you hardly need any Apex Controller for the same. Pages are fast, fluid and Mobile friendly (Salesforce 1).

With time I have found the best ingredient to prepare your AngularJS page are

  • AngularJS ( of course )
  • SObject-Remote (thanks to Jason Venable)
  • Bootstrap (not required)

Lets start with a simple example.

Bringing data to the page


This would be a pretty easy example where we will be using SObject-Remote to bring query data from Salesforce Accounts



The Catch

  • The area ng-controller marks the area which can access the $scope variable of that particular controller.
  • You can access variable inside $scope by using {{}}. If you have a variable $scope.accounts you can directly access that in page by doing {{accounts}}, just like regular merge field but a bit different syntax.
  • We have used $scope.$apply() , which generally tells angular to redraw the UI  / View. Since remoting works async we need to notify the angular to redraw once we have the data by calling this method.

End Result

Finally a stateless fluid page without any controllers.


Monday, April 7, 2014


With the rise of the Web and the HTML5, libraries like Angular , Knockout, Backbone started becoming popular. Among them somehow Angular stood out of the queue and looked more promising than others. It was also because of the fact that it is maintained by Google!

With VF pages I somehow couldn't find a good way to implement Angular, I have been tinkering with the library to make it work with Visualforce. I somehow wanted to marry Angular Factories with Visualforce Remoting and ended up doing the same using promise.


Here is small example how to use Visualforce Remoting with Angular.

Visualforce Page


<apex:page controller="AngularRemoting_Con">  
   <vs:importvisualstrap />  
   <vs:visualstrapblock >  
   <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"/>  
   <!--Remoting Stuff-->  
   <script>  
     function getSobjects(callback){              
       Visualforce.remoting.Manager.invokeAction(  
         '{!$RemoteAction.AngularRemoting_Con.getData}',  
         callback,  
         {escape: false}  
       );        
     }  
   </script>  
   <script>  
     var vfremote = angular.module('VFRemoting',[]);  
     vfremote.factory('VFRemotingFactory',function($q,$rootScope){  
       var factory = {};  
       factory.getData = function(){  
         var deferred = $q.defer();  
         getSobjects(function(result){  
           $rootScope.$apply(function(){  
             deferred.resolve(result);  
           });  
         });  
         return deferred.promise;  
       }  
       return factory;  
     });  
     vfremote.controller('VFRemoteController',function($scope,VFRemotingFactory){  
       VFRemotingFactory.getData().then(function(result){  
         $scope.data = result;  
       });  
     });  
   </script>  
   <div ng-app="VFRemoting">  
     <div ng-controller="VFRemoteController">  
       <ul class="list-group">  
         <li ng-repeat="acc in data" class="list-group-item">{{acc.Name}}</li>  
       </ul>  
     </div>  
   </div>  
   </vs:visualstrapblock>  
 </apex:page>


Controller Class


 public class AngularRemoting_Con {  
   @RemoteAction  
   public static List<SObject> getData(){  
     return [SELECT Id,Name FROM Account];  
   }  
 }  


How it looks like ?


A bit more about the pages

  • The pages uses VisualStrap for styling, that can be removed along with all the components starting with namespace "vs".
  • The Angularjs brings data from controller using Visualforce Remoting, it depends on "getSobjects" method to call controller method.
  • AngularJs uses Factories to communicate with remoting method "getSobject"

This page can be extended to do a lot more things, bringing data dynamically to the VF pages, with the two way binding that angular provides, you can use this to make superfast single page apps.

I am looking forward to Visualforce Remote objects and probably will be posting a blog about how to use them with AngularJS, but at the end I don't think there would be much of differences rather than some basic changes in the code structure. And obviously with Remote objects you won't need a controller!