Saturday, 20 July 2013

Fun With Maps In Apex

Maps are my favorite collection types .The Reason why its very essential to use Maps in apex  classes or triggers is it makes life simple for writing bulkified apex class or trigger .

Read more on Maps in below article from salesforce

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_maps.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_map.htm

Apex we all know runs on an multitenant  architecture .The resources are shared and hence we have governor limits in apex .The advantages of proper usage of maps will help  to  reduce script statements,No of queries for a single context of class or trigger execution,avoiding query on a single object twice .

As a fresher to salesforce  we are unaware of various techniques that we can employ to reduce the script statements and reduce heap size of collection like Lists,Maps and Sets.

You can read some nice articles in developerforce and get familiar with some of common techniques which developers have discovered to deal with no of script statements and heap size issues

My favorite  recipes are as follows

http://developer.force.com/cookbook/recipe/using-sobject-constructors-to-save-script-statements
http://developer.force.com/cookbook/recipe/initialising-maps-from-queries

If you have been coding in apex from long time you might  have come across common use case that you need to aggregate collection of child records grouped by ParentId.

Something like say i will query on Contact Object and i am retrieving all contacts and I need a collection Map of Parent Id (AccountId) as Key and the collection of contacts related to AccountId .In short in apex language terms Map<AccountId,List<Contacts>>

Here is the code that i have included as an example on how to form the collection of Map<Id,List<Contact>>



Hope you enjoyed the technique .Happy coding !Let me know the feedback on the Post.

Thursday, 4 July 2013

Tips On Passing Advanced Developer Salesforce Certification Programming Assignment

As promised as per my previous blog i am sharing my experience on how to clear the advanced developer programming assignment and essay exams to obtain the Advanced Developer Certification.

One of the basic things i would like to mention is ,this you will not have it  overnight as the demand for the course is very high and there are limited slots available in each quarter .One has to be very sharp to quickly register for the exam on the dates mentioned in the certification.salesforce.com. Small negligence  will cause you to wait for another 2 to 3 months .

The assignments are distributed nearly after a month once you have registered .So this gives ample amount of time to revise your apex and visualforce skills.

Once the assignments are distributed you will have nearly a month time to complete your assignment and i would suggest reading the whole pdf that comes with assignment more than thrice atleast to make sure you understand the logical requirements and the hidden aspects of the assignments .

Usually a month time is enough for this but one has to make sure they spend atleast one weekend (Assuming your office timings are hectic and you have lot of work to cover on weekdays)

While doing assignment remember few key points 

1)Follow best practices while designing your trigger and visualforce controllers .Some of the reference links for the wiki articles that helped me  are below 

http://wiki.developerforce.com/page/The_Path_to_Advanced_Developer_Certification

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

http://wiki.developerforce.com/page/Enforcing_CRUD_and_FLS


2)Testing is the most important aspect that is looked when your assignments are evaluated .Make sure you have adequate test methods with proper asserts 

3)Your application will also be evaluated as per security guideline's so make sure you understand the best practices for writing a secure code 


What does an Essay Examination consists of ?

Essay exams are just to evaluate that you have yourself designed the application and written the code .A Simple descriptive questions will be thrown that you should be good if you yourself have written the code .

To understand more on this i would suggest you to read the below article which is one of the best articles written so far on this subject


Hope you enjoyed this short read and its informational.Let me know your feedback on this article .

Saturday, 22 June 2013

Fun with Angular Js and Visualforce Page

Recently i authored a visualforce page just to demonstrate the use of angular JS (Javascript framework from google ).It was very good experience working with Harshit Pandey.

The idea was just to demonstrate how easily even with getters concept of visualforce one can bind data to the angular JS script

The page gives functionality of sorting ,pagination and searching all in a single UI.The UI is done through twitter bootstrap plugin.

The page code can be found below


The controller code is very simple and its just returning JSON object

The same post is posted by Harshit in a friendly format .You can find at following link http://www.oyecode.com/2013/06/getting-started-with-angularjs-on.html

Thanks and Happy Coding !

Monday, 3 June 2013

Using CSS in pdf for page numbering with API 28.0 (@page CSS properties)

Generating PDF in salesforce is very easy with renderAs="pdf" attribute of apex page tag.For those of you needing sample on how to achieve this ,the below link points in right direction to get started and create some professional documents using some CSS power and Visualforce tags

http://wiki.developerforce.com/page/Creating_Professional_PDF_Documents_with_CSS_and_Visualforce

With API 28.0 my designed visualforce pages stopped showing the pagenumbers and other CSS attributes using style with @page annotations .

Here is the code that i tried in sample page to figure the issue with less code



  
  


This code yielded a pdf with no page number and hence conclusion that the @page CSS style properties are not working in API 28.0
Now the question arises like why it works in API 27.0 and not in API 28.0 .To understand this i read enhancements that came in API 28.0 on visualforce .I inserted html tag at start in API 27.0 and found again that @page attribute was not working.Hence injection of html above the head tag creates the issue .

To remove the issue i used the API 28.0 feature to stop the injection of html tag. Here is the working code



  
  

Setting applyHtmlTag as false stops the injection of the html tag and hence the page again started working as expected in API 28.0

Saturday, 11 May 2013

Mobile Application Development On Force.com Touch Platform-Part 3

This is in continuation with my previous posts .This post basically covers the difference between local and server side implementation approach for developing cross platform hybrid  mobile application on touch platform.

An hybrid mobile application is cross platform and generally use phonegap that comes bundled with mobile SDK to develop the application. Web developers familiar with the javascript will find it very friendly to learn and develop hybrid mobile application.

The following table below summarizes the difference and comparison between local(javascript on local device and using ForceTK/RemoteTk  library) and server side(Visualforce with Javascript Remoting) approach:



Local/ServerSide Hybrid mobile application development on touch platform
Feature local Approach using mobile packs(Backbone js/angulur js/jquerymobile) Server side Approach using Visualforce page
Databinding ForceTK or RemoteTK,Javascript modal functions for angular js or backbone js library functions Javascript Remoting can be used
Offline Support Smartstore.js can be used to achieve .Its relatively easier to provide this functionality in local side approach Html5 app cache concept can be used to provide offline(Not secure ,Smartstore Recommended),Browser capacity to store data is limited and hence if offline is major part of requirement its essential to develop with local side approach
Packaging Repeated Packaging for any changes to the application Less Frequent packaging due to One Time SFDC Package Deployment
SFDC API Remote TK consumes no API calls and hence can be used to save API calls,ForceTK consumes API call Visualforce Remoting feature and hence API calls are only consumed for DML operations
Code Storage The javascript library,CSS and whole code resides on the device The main javscript is written inside the visualforce pages
User Interface Jquery mobile can be used to provide good user interface.With angular and backbone libraries such as bootstrap can be used Jquery mobile is preferred .Although no hard rule ,any UI CSS library can be kept in static resource and used to develop mobile friendly UI
Development Effort The developer must be good in CSS,HTML5 and must be familiar with javscript libraries like angular or backbone.js The knowledge of visualforce ,jquerymobile and apex remoting is enough to develop server side hybrid application


The conclusion is at this point it really depends on the requirement to adopt which development model to prefer depending on the requirements .

Reference

http://www2.developerforce.com/mobile/

Monday, 29 April 2013

New Manifest Attribute support in Summer13 release for Offline Caching Of Data

Offline caching of data is very important feature .This feature is commonly required in mobile applications where we would like the pages to be cached locally in the browser ,so that user can browse through this pages even if the application goes offline temporarily due to network failure .

HTML 5 came with a manifest file concept where the files and pages to be cached in the browser are specified in a text file which is kept in the same server domain as the other pages .This file is basically a text file of content type is text/cache-manifest.

W3schools is a place where i learn basics .Thanks to w3schools for presenting content in simple language .Here is the reference link for all the enthusiastic.

http://www.w3schools.com/html/html5_app_cache.asp 

The App-cache concept is clearly explained on the link above . Summarizing ,its just the manifest file of type text/cache-manifest that tells the browser on the content that will be cached in the server. 

<html manifest="demo.appcache"> 

The html has an attribute to indicate the manifest file .An example shown above where demo.appcache is the text file that holds resources that will be cached once there is no network and still you can access content that's cached on the browser .

Visualforce had some new attributes in recent summer13 and a mainfest attribute was provided in <apex:page> tag to specify manifest file .


This small write up will take you through a very small tutorial where we cache a page and make it accessible even when the browser  is offline .

Lets begin step by step

1)We will first create a page that will be html5 and also specify the manifest file in that page 

Here is the simple visualforce page we create first and lets name it as test


<apex:page doctype="html-5.0" manifest="/apex/CacheManifest" showheader="false" sidebar="false" standardstylesheets="false">  <header>   <h1>  Congratulations!</h1></header> <article>    This page looks almost like HTML5! </article> <img src="/img/seasonLogos/2013_summer_aloha.png" /> </apex:page>


2)The manifest file can also be created from the visualforce page .Naming the page as "CacheManifest"



<apex:page contentType="text/cache-manifest" applyHtmlTag="false" standardStylesheets="false" showHeader="false">CACHE MANIFEST#img/img/seasonLogos/2013_summer_aloha.png</apex:page>



3)Open the visualforce page /apex/test and go offline by disconnecting the network.

Try refreshing and you will see even if your computer has no network still this page will exist due to browser caching of the image .

Here are the some of the snapshots i took from my pre-release org to demonstrate this feature




















Observe in chrome how i view the cached resources using the inspect element utility provided by the chrome .


Now let me go offline and view the same page











One thing to note on this is html5 app-cache is  insecure and hence for enterprise applications where data security is primary consideration to develop offline applications this is not the suggested way .

Still this is popular when you want to store some pictures and small data in browser cache for offline .

 Thanks to Bob Buzzard for answering my query on stackexchange  and that had solve the issue of specifying CACHE MANIFEST.

Reference

http://salesforce.stackexchange.com/questions/11104/manifest-attribute-of-summer13-not-working-on-my-prerelease-org


Sunday, 21 April 2013

Mobile Application Development On Force.com Touch Platform-Part 2

This blog post is in continuation with Earlier blog post where we explored  some limitations of standard apps provided by salesforce .But every release salesforce keeps improving Salesforce Touch App ,Salesforce Mobile etc and hence its advisable to go through latest release to find out whether the customers requirement can be met from using those standard app and figure out actual need to go for development of completely customized app.

Force.com Touch Platform offers Salesforce SDK for Android and IOS .Using SDK its possible to develop native ,hybrid or an HTML web application.

http://salesforce.stackexchange.com/questions/8075/html-vs-hybrid-vs-native-app

There are lot of discussions around which type of application is best. (Hybrid or native or html).The conclusion it really depends on the requirement,developer resource and performance expected .

Native Apps are faster and provides great offline support but we may need to code in different languages depending on platform .Eg:Objective C for IOS and Java for Android .

 Hybrid Apps are developed using Javascript libraries and a web developer with Javascript skills along with knowledge of Jquery mobile and Html5 and CSS3 can handle .The Hybrid applications will provide cross platform support and there is a support for offline as well using Smartstore.js that comes bundled with salesforce mobile SDK.

http://www2.developerforce.com/mobile/getting-started/html5

The above link will take you developerforce where you will see the Javascript libraries that can be used to develop Hybrid Application.To mention few these are i)Angular.js ii)Backbone.js iii)Forcetk

JqueryMobile  is an excellent utility and using this the UI of the mobile application can be developed very quickly.This is also one of the components that comes bundled with mobile SDK .

Lets quickly summarize what are the things that comes bundled with the Salesforce SDK and whats there use (Salesforce SDK for Android ,Salesforce SDK for IOS).

a)bootconfig.js-This boots the app as name suggests .This configures the app .

Settings needed in  bootconfig.js for local Hybrid app 

i)remoteAccessConsumerKey -From your remote access settings  or connected apps place your consumer key in this variable 

eg:
var remoteAccessConsumerKey = "3MVG9A2kN3Bn17hvZpJm8_imuGQjzehEPJ94DYLTwjIp811dyZUSv3EEU1L0z5TAZHanF4clFyNF_2jygDscB";

ii)oauthRedirectURI-This variable must be populated with the Callback URL from your remote access settings or connected apps 

eg;

// The redirect URI value specified for your remote access object that defines
// your application in Salesforce.
var oauthRedirectURI = "testdemo:///mobilesdk/detect/oauth/done";

iii)startData-This is crucial part of configuration.This decides whether you want to land the app on local html (In case you have developed local hybrid application )or On server side visualforce as the first page as soon as app boots up(In case you have developed server side  hybrid application)

For local app use the following:


var startData = new SFHybridApp.LocalAppStartData();  // Used for local REST-based "index.html" PhoneGap apps.


For Visualforce based Server side application use the following

var startData = new SFHybridApp.RemoteAppStartData("/apex/Visualforcepage?context=container"); 

Where VisualforcePage is the name of Visualforce where you want to land the user when the app boots up

NOTE:I will be expanding in my next blog post whats the difference between local side hybrid app and server side hybrid application

b)Cordova-2.3.0.js-This is open source phonegap plugin.To read more about Phonegap refer the following link 
http://docs.phonegap.com/en/2.6.0/index.html

We wonder how can i access the device features(Camera,audio,video,geo location,file storage  etc)from Javascript .It is this open source Javascript library that has Javscript functions to invoke the device features.

c)SalesforceOauthPlugin.Js-This handles the authentication of the application using oauth 2.0 of force.com.Please Note oauth 2.0 is necessary only for local hybrid application

d)SFHybridApp.Js-This provides some utility functions necessary for offline caching of data and functions  to detect whether the device is online or offline etc 

e)Jquerymobile -This library eases the creation of UI for the application and makes it platform independent and also device independent .

There are lot to speak about mobile application development on touch platform and surely i fancy blogging more on this .Happy Learning!!!





Introducing Lightning Base Components

Lightning Base Components are great addition to the platform and in fact revolutionary .One of the concerns around lightning component ...