Thursday, 14 May 2015

Visualforce Remoting: an unsung hero of the Salesforce world!


In this ultra-fast, action-packed world of Salesforce that we all live in, nobody likes to wait for a response, not even your HTTP callout! Just the other day, I needed to work on a requirement, where I needed to update records through a Visualforce page, send the updated data to a third-party web service and display the results on my Visualforce page! Sounds quite a handful, doesn’t it? That’s when Visualforce remoting came to my rescue.

Initial hurdles…

In order to perform an HTTP callout from my page, I knew I needed to develop my controller in a way that would help me show only the relevant things on my page depending on the response. Using re-render and rendered attributes on my elements was the key that worked to good effect, except one small thing: the response times.

Using apex methods meant that the page would perform AJAX calls to the Salesforce server, which in turn would perform the HTTP callout and provide me with the results, so that I could further render the elements on my page. The major drawback of this method was that the user would be forced to wait and allow it to finish. I needed something faster, something that rendered the execution more efficiently and swiftly.

When Lightning struck…

Suddenly, an idea which was hiding in a remote (pun intended) corner of my mind came up: Visualforce Remoting. I had hit the bull’s eye! Here’s how I did it:
  • I called a Visualforce remoting method from an apex class on the update of the record. This remoting method performed the callout to the third party web-service.
  • The method then returned a JSON object that contained values that needed to be displayed on the page
  • I then went on to parse the JSON response and perform the relevant operations
  • Finally, I displayed the results on the page 

The Process Flow…

I have included the process flow below for easy understanding:


The entire operation was considerably faster now. Finally I could breathe easy!

I have included the sample code snippet below, where I am accepting the account name from a text input on my page and creating a new record with that name in another developer org.


This remoteAction method was called asynchronously by the remoting script written on my page. The string response that I received from the createAccountRemote was checked for its trueness and an alert message was displayed on the page accordingly.


It is important to note that in this case, there was no other action to be performed, but using Visualforce remoting method was still a better move than using traditional future methods.

Why is Visualforce Remoting indispensable?

  • Faster response times as compared to apex actions
  • Flexibility to modify the HTML as remoting enables you to directly perform JavaScript actions based on the response
  • Remoting methods are executed in the context of the user
  • Methods are not bound to the view state of the page
  • Can be used to perform complex operations on the page that would not be easily possible using apex tags

…but it’s not all hunky dory…

  • As Visualforce remoting methods are supposed to run in the static context, they cannot access controller variables, so the user needs to perform another logic to access controller data
  •  Visualforce remoting methods are asynchronous, making it difficult to handle situations where serial execution of events is needed. Explicit care needs to be taken in order to ensure that the results are available before the next set of processing takes place.
  • In order to use Visualforce remoting, a basic understanding of JavaScript is required

Its limitations notwithstanding, Visualforce remoting is quite handful when you need faster response times using Apex. So next time you use Visualforce remoting for one of your requirements, don’t forget to say a quiet ‘thank you’ to this unsung hero of the Salesforce world.

References:

https://www.salesforce.com/docs/developer/pages/Content/pages_js_remoting_example.htm



Written by Saket Joshi, Salesforce Developer at Eternus Solutions
Read More »

Wednesday, 13 May 2015

Duplicate Management in Salesforce

If you have worked on Salesforce, you have invariably waded through troubled waters, thanks to the innumerable duplicates in your data. Tracking duplicates and weeding them out of my Salesforce orgs for accurate reporting and forecasting periodically is one of the most frustrating tasks that I have ever undertaken. That is why when Salesforce introduced the Duplicate Rule in the winter ’15 release, my joy knew no bounds!

Discovering the Duplicate Rule

Organizations can take advantage of the duplicate management rule to keep their sales teams’ contacts and accounts free from duplicates. The Duplicate rule gives users the ability to prevent data redundancy and maintain data integrity. It's also a boon for sales reps as it enables them to avoid multiple calls being made to the same lead/contact, thus streamlining the Sales Process. It reduces the effort that you would take to create Apex code for eliminating duplicates, or marking fields as Unique to avoid duplicates by multiple field values. Simple ‘point and click’ will suffice!

The UI is easily navigable and based heavily on the Workflow and Workflow Rule creation process. A user defined alert is displayed whenever a duplicate record is found.

Duplicate Management: Because two of everything isn’t always good!

Using Duplicate Management in Salesforce, the users can:
  • Allow/block the creation of duplicate records
  • Customize the criteria for identifying duplicate records, such as:
    • Duplicates by email only
    • Duplicates by email, first name and last name combined
  • Report on the duplicate records found by the rules. You can also create custom report Types to report on duplicates.

Empowering your Sales Reps

Nothing can be worse than having two of your Sales reps call the same lead! Using the duplicate rule, you can easily avoid such embarrassing situations. Let me show you how your Sales reps can get back their mojo, ensuring a surge in their efficiency, productivity and overall data integrity.
  1. Define a Matching Rule as I have shown below, which will help your Sales reps to customize the matching criteria to identify duplicate records for their leads. For example, they can use the combination of Email and phone number to identify the duplicate leads.


  2. You will then need to create a Duplicate Rule and associate it with the Matching Rule that you created above. It also gives you the provision to create customized alert messages.



  3. While creating the rule, you have an option to allow/block the creation of duplicate records which can then be used in reporting. The fields that are included in the duplicate identification criteria are highlighted as shown below while trying to save/edit a record that matches the criteria.


    (a) Saving duplicate records

    (b) Sales reps cannot save records until they change duplicate values
And it’s done! Your Sales reps won’t stop thanking you forever!

Duplicate Management: Limitations

Duplicate Management was only introduced recently by Salesforce, in the winter’15 release and therefore has its fair share of limitations still associated with it:
  • Duplicate Management is available only for Accounts, Contacts, Leads and custom objects
  • Duplicate Rules are not fired while using Quick Create or lead conversion
  • As of now, you cannot deploy them via change sets
That said, Duplicate Management is quite a useful tool that truly makes life easier for the Salesforce users. Once Salesforce irons these minor chinks, and I am sure it is just a release or two away, Duplicate Management will definitely be an unputdownable functionality.


Written by Saba Shaikh, Salesforce Developer at Eternus Solutions
Read More »