Saturday 28 March 2015

Named Credential and Auth Provider Concept In Salesforce-Linkedin and Salesforce Integration

This is my first technical post after I have moved to Texas from Bangalore and Hope I make it interesting read 

One of the common challenges when Integrating salesforce with other external system which supports Oauth 2.0 has been to manage the Oauth 2.0 flow using custom objects in Salesforce ,where there was need for some temporary storage for storing either the access token or the refresh token or an alternative approach was to do authentication every time user makes a request to other external system.


The below diagram summarizes oauth 2.0 and for more information would suggest you to read some articles in google or wiki




If i tell you for some system like google,twitter,facebook,Linkedin,google I dont have to handle Oauth 2.0 myself with lot of code and salesforce will automatically take care of it ,I don't think you will trust me .

So in this blogpost ,I will be integrating linked.in with Salesforce with very minimal code and will pull my resume as a chatter app,so grab a coffee and lets together continue doing this interesting piece of integration.

Before we do lets see how much complexity it involved in past ,

Terry Luschen  is an awesome blogger on Salesforce Integration with external system and I picked his post as starting point 


So i see that we have some 30 to 40 lines of code just to establish connection and then there is hint on how to handle refresh token and oauth token .This is excellent post almost an year back and with SFDC coming with Named Principal and Auth Provider makes life simpler for these type of Integrations.

Business Scenario-Handshake between Salesforce and Linkedin as a Proof of concept and a decent technical guide to build more robust functionality like searching linkedin data,showing linkedin feeds ,fetching related contacts to increase more contacts .There is lot of business process that you can tie up and build some cool stuff and add business value with linkedin data .

1)Configuration of Auth Provider

In Salesforce Environment navigate as below path

Administration Setup>Security controls>Auth. Providers

Fill this form and leave blank for consumer and Consumer Secret


Note URL Suffix is automatically filled once we have name.

Please note Callback login URL for next steps .This will be fed to the LinkedIn app.

2)Configuration of LinkedIn Application 

Navigate to https://developer.linkedin.com/  and click on My Apps for creating a sample application
Click on Add new App for adding your Application .

All the information is self explanatory that are required to filled except for Oauth 2.0 redirect URL which is very essential .This needs to be fetched from salesforce Auth Provider Setting .Salesforce Auth Provider once it is saved ,it will provide us with necessary URL

You will see a consumer secret and consumer key once linkedin form is saved 

The Consumer Key and secret needs to be updated back into the Auth Provider so complete the Auth Provider setting

3)Configuration of Named Principal 

This is an excellent feature thats been introduced in Spring 15 and it makes life excellent to avoid the lengthy code just to authenticate ,below is how we configure it for linkedin API

Administration Setup>Security controls>Named Principal

Select Oauth 2.0 as your authentication mechanism and Auth provider as one you just created and Identity Type as per user 

Now from API guide the URL that will be common will be https://api.linkedin.com/v1/

Hence use that as a URL for named Principal .

Also dont forget to check Start Authentication flow on Save if you need to run oauth .On Save oauth flow will happen and there will be success message if authentication is successful.

This is how the prompt will show if all successful .

Once you confirm this app to allow access ,you will see a success prompt or warning prompt confirming all success and registered .

 4)Need now apex magic to complete

Now using named principal we will do callout and fetch data inside salesforce ,parse from JSON and show in UI .

Apex code for parsing class is as below




The Code for controller doing callout and fetching data is as below


The next steps are creating tabs for VF and adding as a Subtab Application .Here is how finished product looks like .Obviously with some CSS you can make things look more fancy



Now one final question is what will happen when other users login and try and see their app .Will the app show them their profile .

When we created named principal we said our Identity Type is "Per User".

5)Assigning Permission set to other Users so that they can add their external source

So lets say I have another User in my org and he navigates to the LinkedIn profile page ,he will see his profile Info from the linkedIn if he follows below steps assuming administrator has assigned a permission set with access to the Named principal 


Each user should be trained to go to My Connected Data in their personal set up and add this as external data for working of app




Some more cool Idea would be show data in bootstrap format and make UI responsive ,Build some linkedIn search engine via API and lot .

The power of "named credential" is I have no code to run my oauth .

Please feel free to comment what you feel on this blog .I have common complain that my blogs receive no comments and no feedback :(  

Saturday 21 March 2015

Reduce your learning Curve through Salesforce new learning tool Trialhead

4 Years back when I started Learning Salesforce ,I wish I had access to tool like TrailHead .

Please visit below URL to explore more

https://developer.salesforce.com/trailhead

What you need to complete TrailHead Challenges?
  
You will need Salesforce Developer ORG to work with TrailHead  Challenges .Make sure to have a work which has no custom domain enabled and also has no namespace.Recommend to sign up for new org.

You also require a sign up on developer.force.com .You enter into a world of Salesforce Developer Community through this site .


What will you gain by spending Time on TrailHead?

You will be able to do hands On Exercise which are carefully designed to test your understanding of the content presented and it makes sure that you work examples on developer org

What is Trailhead Badge? 

Once you complete a Challenge your developer profile on developer.force.com gets a badge to show case that you have completed all challenges and you have gained some pratical insight into subject presented in the module .


I came across this tool through twitter feeds and did not bother to try out assuming I will have no returns out of the tool since I almost knew all the topics that Initially Salesforce launched on this tool .Salesforce Lightning module was launched by salesforce when salesforce in this year Dreamforce .

I was quite confident that I would learn through salesforce release notes and developer guides .Because of hectic project work I really could not make time for going through release notes .One day I came across a twitter feed saying TrailHead is offering tutorial module on lightning components .This urged me to straight dig into this tool.

At the end of the modules ,I discovered this tool is really an excellent resource for anyone who is really new to any of salesforce topics .This is one of the reasons I went back and completed all the modules .














My favourite topic after covering all modules has been Visualforce Mobile .Lets walk through what we learn in this module

Get Started with Visualforce in Salesforce1

For a new developer before moving to this topic I would recommend to go through Visualforce Basics .Trailhead has a good module around this as well .Please visit below link if you are new to Visualforce

https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/visualforce_fundamentals/visualforce_intro

Once you have completed Visualforce modules ,i would recommend some time to learn Javascript remoting and Remote Objects .This will help you understand this topic more 

You will learn how Visualforce pages can be used in Salesforce1 and also it will help you to reason out when to prefer  to develop mobile apps in Salesforce1 instead of using the Mobile SDK

Adding Visualforce Pages to the Navigation Menu

This module will help you understand how to make your VF available on your Salesforce 1 App.it covers concepts like sforce.one library

Implementing Global Actions with Visualforce Pages

This module will cover global actions and how to configure them for SF1 App.Also this will explain the the Publisher SDK and how to use CSS styling to get mobile look and feel for your Salesforce 1 app

Implementing Object-Specific Actions with Visualforce Pages

With this module you will discover you will now know how to develop VF pages and add an object-Specific action and configure the action to appear in SF1 App.

You will learn how to use publisher SDK to close the publisher and refresh the object page.

Using Visualforce Pages in Page Layouts and Mobile Cards

With Mobile Cards you can expose your VF on the Object related list in Sf1 app.This feature is explained and also you will become an expert on how to use Compact Layouts for Sf1 Apps.

Adopting User Interface Guidelines

This module came as surprise .Since I was not from design world ,I probably lacked this .This module will help you to learn responsive CSS and also the Visualforce components that should be avoided when building mobile apps.

Using CSS and JavaScript Mobile Frameworks

A complex Enterprise application will force us to adopt to one of well known frameworks when we are building an SF1 App using VF for responsiveness and ease .

Some of the JS and CSS frameworks we must be familiar for building Enterprise application  includes 

Bootstrap
Ratchet
Ionicframework
Angularjs


Hopefully this explains the importance of TrailHead .Also Trailhead team I have been knowing from Salesforce have been super helpful.

Please contact Josh,Sandeep Bhanot or Chris Duarte for any serious issues .I have tested all modules so far and they work great .

Tweet me if any issues and I am glad to help .

Also Trailhead has a developer forum and you can raise your issues here and developer community will be happy to help.






  

Sunday 1 March 2015

Trialhead -Self Tutor

I have no words for TrialHead that was launched in dreamforce last year .I experimented using TrialHead for lightning module last week and I must say worth spending time on TrialHead .

I am very new to Lightning and I had read Release notes and Lightning developer guide but somehow I was not so confident .I must say once I have completed challenges on the TrialHead for these modules my confidence level has gone up.No more I feel that I dont know basics since I have coded the assignments asked and have passed all those to earn badeges.


I am thankful to creators of this tool and this has made learning real fun .The great team at salesforce Sandeep ,josh or Chris Duarte are really helpful .I have reached them no of times on twitter if i find any difficulty related to any problem in Trailhead and they have been super helpful .

Thanks TrailHead and I have requested for more lightning related tutorials on this . Please note Trailhead is free of cost and your best tutor.

Invoking Apex Callout From Process Builder

Process builder is GA in Spring 15 and one of the queries I came across was around how to invoke apex callouts from Process builder .

Before process builder came we had two common approaches of calling webservice 


1)We have outbound messages as one of the Actions for workflows.This works if other party implements the WSDL that is generated once Outbound messaging is defined with appropriate end point .


2)Most of times future method invoked through triggers allowed to do apex callouts and invoke external web service provided the future method is annotated with @future(callout=true).This provides lot of flexibility and one of the best approaches .


3)Flow triggers was in BETA and this was also one of the ways we could invoke callouts provided the Flow implements process plugin .To understand in detail how to implement process plugin refer to the below example


The purpose of this blogpost is to demonstrate the new possibility of invoking apex callout through Process Builder 


I tried initially to make this callout synchronous and invoke apex callout directly inside @InvocableMethod and received uncommitted pending DML issue and I have no solution for this and may be an Idea post to salesforce to allow this .


My approach included below steps 


1)Invoke apex through process builder using @InvocableMethod

2)Use Future method inside @InvocableMethod and invoke apex callout 

Creating Process Builder Flow


1)Navigate to Process Builder 


















  


2)Specify the Object and when to start the process setting





                                               








3)Define Criteria for our action defined











4)Define the action ,here our action comprise of calling the Apex method annotated with @InvocableMethod












The input variable of apex method needs to map to related record ,here it maps to Account object .

Below is the code for the MakeApexCallout class ,Future class and also the JSON parser weather info class

For callout for this example I have used a sample REST API from api.wunderground.com that is available for free

The response is JSON which returns the Zipcode also and we stamp Zipcode returned from the response on the Account record .

A small video demonstrating this is as below



I am quite surprised that even though it happens in future but after the page refreshes the value is found stamped on the record (Almost given us feel to naked eye that it happened synchronously )


I hope you enjoyed this post and any queries or solution please let me know .


Introducing Lightning Base Components

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