Send Email with Generated PDF as attachment from Trigger

There may be scenario in Salesforce that you need to send a Visualforce page rendered as PDF as a part of Email Attachment. This will be very easy if you want to perform this using Controller or Extension class, we just have to call getContentAsPDF() method of PageReference class and use returned blob data as a attachment in Email.

However, If we are talking about achieving same in Trigger then it would be problem. Trigger does not support getContent() method of PageReference class. If you are thinking to use getContent() in future call then again you are not lucky, because @future methods does not support it. Also Apex job doesn’t support this method.

Now, I hope you understood that in which situation we are :)

So, In this article, I am going to explain how to resolve this issue. Not exactly resolve but workaround for above problem.

Solution is very simple, We will expose apex method as a REST API. Code for sending email will be written in APEX based REST API and our Trigger will call this method asynchronously using @future annotation. Continue reading

Step by step guide to Setup Federated Authentication (SAML) based SSO in Salesforce – Video Tutorial

In this post, We will be dicussing how to setup Federated SAML based Authentication in Salesforce.

SAML stands for “Security Assertion Markup Language” and it is Open standard for exchanging Authentication and Authorization between Systems. SAML based authentication is supported by all editions of Salesforce.

User Validation can be initiated by any one of below two types:

  1. Service Provider Initiated SSO
  2. Identity Provider (IDp) initioated SSO

We are going to use Identity Provider Initiated SSO in this article. Means User will Login from Outside(IDp) and will be redirected to Salesforce (Service Provider). Identity Provider must follow Federated Authentication (SAML) standard which should be deployed to DMZ (URL should be publicly accessible on Internet) layer of your Organization. As a Salesforce developer you should assume that you will always get IDp URL which implements SSO and implements valid SAML response. To Quickly start with this tutorial assume that your organization already deployed SAML based Authentication endpoint and for that we will be using great Heroku app available freely as open source named “AXIOM“.

AXIOM is java based heroku application which implements SAML and can be used for testing  purpose to check whether SSO is working properly or not.

IDp Initiated Single Sign On :

In IDp Initiated SSO, User Directly logins to Identity provider and IDp redirects user to proper Salesforce Instance with SAML assertion in request (Service Provider). If SAML assertion is valid then Salesforce validates that user successfuly. Continue reading

Introduction to Flexipage in Salesforce1 and Deploying it using Workbench

We all are well aware about PageLayout and Visualforce Pages. From API 29 (Winter 14 release), Salesforce has introduced new functionality in platform named “FlexiPage”. Like Pagelayout, We can add Custom components in Flexipage however Flexipage will be visible only in Salesforce1 application in sidebar.
We can use Flexipage to make custom Homepage for some product which will be accessed directly from Salesforce1. One important thing to note here is that, till date (at the time of writing this tutorial) Flexipage can be defined in XML file and then deploying it using Eclipse, Ant Migration tool, workbench or any other migration tool. There is no way to define Flexipage directly in Salesforce using point and click functionality.

Step1 : Defining XML file for Flexipage containing detail of components

Filename – “AccountFlexi.flexipage” Continue reading

Salesforce REST API Playground

What is REST API ?

In my words, Getting data from Other System or Same System using HTTP request is known as REST API.

If you know, how website works, you know REST API. Before REST API, there was SOAP request which needed lots of configuration and very tightly coupled. If you make any modification , you had to modify client side also by generating stubs / proxy classes again. There were many more disadvantages which got address by REST API. However, it doesn’t mean we don’t need SOAP.

Back to REST API, It is very simple to setup and almost any programming language can make HTTP request so very easy to get data from other source. Return type of REST API can be XML, Simple HTTP, JSON or any media like image. However, JSON is very popular response type amongst all.

In Past, I have already written one article demonstrating REST API usage.

How to use Salesforce REST API using JQuery in Visualforce ?

REST API Playground : I have created Simple Visualforce page, which demonstrates how to use REST API of Salesforce. It is written in Visualforce with the help of JQuery. You can study this code and play with it in your organization, you just need to copy and paste it.

You also need to add URL “http://cdnjs.cloudflare.com” in Remote Site Settings, so that salesforce will allow Visualforce page to get JQuery library from CDN.

Continue reading

Pagination, Searching and Sorting of Data Table using AngularJS

We have already discussed “Basics of AngularJS” and “Simple Searching and Sorting“. In this article, we will add Pagination feature also to our previous example. What makes difficult is Sorting result when values are returned from Search and pagination is enabled. Unlike previous post we cannot use standard “OrderBy” filter provided by AngularJs framework, this time we have to create Javascript function inside Controller for Sorting purpose.

Prerequisite Libraries :

  1. UnderscoreJs
  2. Bootstrap CSS
  3. AngularJs

This post used “Custom Service”, so please read this article to understand Dependency Injection in AngularJs.

Searching Sorting and Pagination in Table using AngularJS

Searching Sorting and Pagination in Table using AngularJS

Continue reading

Dependency Injection in AngularJs

In this series, we have gone through many articles on AngularJs. This time I will discuss about Dependency Injection in AngularJs .

Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time.

Before going to DI first we have to know about Service.

What is Service , factory or Provider in AngularJs?

They all are same, with different way of writing. This (Singleton design pattern) defines dependency between your controller and View through application.
We can Inject a service in Controller, which will watch over changes automatically and change its View as per required.

We have many inbuilt standard service available like $scope, $filter. All inbuilt services in AngularJS starts with this sign $. In same way we can create our own custom service and then inject in Controller. Continue reading

Table with Sorting and Searching using AngularJS

I hope, from last article you got basic information about AngularJS. This time, I will demonstrate how to add basic Searching and Sorting capability to data table using AngularJs.

Output will look something like this :

Searching and Sorting in Table using AngularJS

Searching and Sorting in Table using AngularJS

Prerequisite Libraries :

  1. UnderscoreJs
  2. Bootstrap CSS
  3. AngularJs

Continue reading

IndexedDB – Client Side Database

One of many features of HTML5 is capability to store data on client machine with the help of “Indexed Database API“. Using this technique web developers can add offline capabilities to their application. Currently almost every modern browser supports “IndexedDB”.

Unlike traditional RDBMS, IndexedDB doesn’t have table or columns which supports SQL. In RDBMS, it is very hard to change structure of table once schema is decided. IndexedDB actually stores object which can be of any type. Store Object in IndexedDB and process inside Javascript, it is that simple. Every object needs to have unique Index which makes it accessing faster and efficient. Before IndexedDB, we had “Web SQL Database“. However W3C announced that it is depreciated, still few browsers continues support for Web SQL.

Lets start with Example on Indexed DB :

Very first step in IndexedDB is opening database. Below Code Sample will explain the process.

Continue reading

Visualforce Remote Objects

One of the exciting feature of Spring14 release is introduction of “Visualforce Remote Objects”. You can say its actually replacement of JavaScript Remoting.

Why do we need “Visualforce Remote Objects” when we already have “JavaScript Remoting” ?

Well, here are few advantages of “Visualforce Remote Objects” :

  1. No need to write Controllers, Everything can be done in Visualforce only.
  2. As @RemoteAction annotated methods needs to be static so you had to take special precaution as it didn’t supported Viewstate. This hurdle is completely removed now.
  3. No need to write Test Class now, as no Controller is involved.

Continue reading

FAQ and Best Practices of Test Classes in Apex

Here I am going to share few answers related to Test Classes, which is being asked many times to me by novice Programmers. Please feel free to post Comments if your question is not answered here. I will try my best to add those Questions in this article. If you want to learn Test Class from very basic, you can check this article also.

  1. Best Practices for Test Classes
  2. Test method of Controller Extension for StandardController
  3. Test method of Controller Extension for StandardSetController
  4. Why getSelected() method of StandrdSetController is not working
  5. sample Template of class which generates Dummy Data
  6. checking Error Page Messages in Test Classes
  7. Setting Visualforce Page Parameter

What are some Best Practices while writing Test Classes ?

Well, It differs from person to person, as every programmer has its own style of writing code. However I will list few of mine.

  • Very important and first, “Test Coverage Target Should not be limited to 75%”. It is not about coverage, It is about testing complete functionality. It will be always better if your code fails during testing, It will be less devastating than failing functionality after product release.
  • If possible Don’t use seeAllData=true, Create your Own Test Data.
  • Create Different Class which will create Dummy Data for testing, and use it everywhere
  • If your Object’s Schema is not changing frequently, you can create CSV file of records and load in static resource. This file will act as Test data for your Test Classes.
  • Use As much as Assertions like System.AssertEquals or System.AssertNotEquals
  • Use Test.startTest() to reset Governor limits in Test methods
  • If you are doing any Asynchronous operation in code, then don’t forget to call Test.stopTest() to make sure that operation is completed.
  • Use System.runAs() method to enforce OWD and Profile related testings. This is very important from Security point of View.
  • Always try to pass null values in every methods. This is the area where most of program fails, unknowingly.
  • Always test Batch Capabilities of your code by passing 20 to 100 records.
  • Use Test.isRunningTest() in your code to identify that context of class is Test or not. You can use this condition with OR (||) to allow test classes to enter inside code bock. It is very handy while testing for webservices, we can generate fake response easily.
  • @TestVisible annotation can be used to access private members and methods inside Test Class. Now we don’t need to compromise with access specifiers for sake of code coverage.
  • End your test class with “_Test”. So that in Apex Class list view, Main class and Test class will come together, resulting easy navigation and time saver.

Continue reading