Saturday, 29 November 2014

Salesforce to facebook integration

STEP 1 : Login To (https://developers.facebook.com/)

STEP 2 : Give your face book credentials and login

STEP 3 : Go to APP Tab (add new app)

STEP 4 : Select website and give APP Name (Click on create new Facebook App id)

STEP 4 : Under Category Section select (Apps for pages) and click (Create app id)

STEP 5 : Under (Tell us about your website) Section Site Url = Give your Visual force page url 
(https://c.ap1.visual.force.com/apex/facebookPage) and click (Next)

Note: Before going to this process create a visual force page with page name (facebookPage) and controller (FaceBookController)

VF: 
<apex:page controller="FaceBookController">
<apex:form >
<apex:commandButton value="ClickMe" action="{!fbAction}"/>
</apex:form>
</apex:page>



Controller:

public with sharing class FaceBookController {
public PageReference fbAction() {
HttpRequest req=new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://graph.facebook.com/oauth/access_token? client_id=0000000000000000
&redirect_uri=https://c.ap1.visual.force.com/apex/facebookPage
&client_secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
&code=oxoxoxoxoxoxoxoxoxoxoxoxoxo');
HttpResponse res = new HttpResponse();
http h = new Http();
res = h.send(req);

system.debug( 'Aceestoken-------'+res.getbody());
return null;
}
}


STEP 6 : Click on Finish

STEP 7 : Copy the (App Id) and (App Secret)


 



STEP 8 : Authenticate URL


https://www.facebook.com/dialog/oauth?client_id=1505806869677932&redirect_uri=https://c.ap1.visual.force.com/apex/fbpage&scope=user_about_me,read_mailbox,read_insights,user_photos,friends_photos,publish_stream,read_friendlists,read_insights,read_requests,read_stream,xmpp_login,ads_management,create_event,manage_friendlists,manage_notifications,&state=immu1038

In the above url replace

1. client_id : with your Facebook App Id (EX: 1573492886214474)

2. redirect_uri : With your VF page url (EX: https://c.ap1.visual.force.com/apex/facebookPage )

3. state : Any string (EX: abcsdefg..)

Copy the above Authenticate URl

Open a new tab in browser paste it and click enter

It will redirect to visual force page

STEP 9 : Copy the redirect page URL and Paste in note pad

EX:

https://c.ap1.visual.force.com/apex/facebookPage?code=AQCAmhJfKJ5zSa8pYhI11qNz9L7nLEcVHz6YjiS2VUQfUJbvoIylB8DfI1M7Blwjx0jdVluatTgB_cslPf1m4mLN_PQ6ZMkcHNHpTKkiZxctic6dil_4KYpXgZv91L9cSV4t2KH7a3ZXYr3bJ2zf8U_wWEJ1_kRcdwozF7Y22ET6gH70VlnR87qaEjEEWltraO6MyEbcw7e9ChBQNLHsfJCN579lFXNmqwlhCzSmdQHCQxRWxRUNSdvMi9_jy5CGzD0hOo9MaplIbg0StUx-goGANIBz5sM0cXcyeLhc4ArU-bDL8R18zRB_O0yAmvULEq0vgBcvkG8tXOMU34SISffu&state=NanduKumar#_=_

STEP 10 : Open your controller (FaceBookController)

Replace: 
req.setEndpoint('https://graph.facebook.com/oauth/access_token? client_id=0000000000000000
&redirect_uri=https://c.ap1.visual.force.com/apex/facebookPage
&client_secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
&code=oxoxoxoxoxoxoxoxoxoxoxoxoxo');

1. client_id : Your facebook App Id

2. redirect_uri : Your visual force page url

3. client_secret : Your App Secret

4 . code : Follow step 9


STEP 11 : Save it and click the button (Click me)

STEP 12 : Open the debug log and copy the acesstoken

STEP 13 : Open developer console (or) work bench

HttpRequest req=new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://graph.facebook.com/me/feed?
access_token= CAAWXFVyKF0oBAAT6
jw3zU66dZBX3e6W0zZCL07ppLyYJrSRnTb2aMP7eYAPQdtUGpp2sT
PvuX4N4Q9xlx5H2qDwSHCoDhtlTp5Y3aHvGHgZBFYqRWiaOxVQuEFTwlha574l5
8f6KPULGiSORARMPtlQCcgQGjOVd1IValQJNtvZBFf3ew9SLFbPNFVZ
Aj7B9ZCkrCGTxfXCU7xmt1plZCaE');
HttpResponse res = null;
http h= new Http();
res= h.send(req);
system.debug('resp----------'+ res.getbody());


Copy the above code and paste in your developer console or workbench

Replace security token with your security token

Executive 
Open the debug log find the resp data
NOTE :



1. The security token is time limit , ones it is expired follow the below STEPS


STEP 1: Go to STEP 8 copy the URL

Replace State = new String (EX: ddhjdhjasdhja) 
STEP 2: Follow the STEP 9 , 10 , 11, 12 , 13

2. List of End point URL

To see albums use endpoint urls
https://graph.facebook.com/albums
To see specefied albums's photo
https://graph.facebook.com/albumid/photos
To see posts
https://graph.facebook.com/me/feed

3. To insert photo in to facebook using apex code

HttpRequest req=new HttpRequest();
req.setMethod('POST);
string url ='URL=http://www.visiblebanking.com/wp-content/uploads/
2012/04/Progressive- Flo-FacebookPage-Long-Processed.png';
req.setbody(url);
req.setheader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length',string.valueof(url.length()) );
//albumid=000000000000000000
req.setEndpoint('https://graph.facebook.com/000000000000000/photos? access_token=hfsdfhshdfjksdf mdfbsjkf');
HttpResponse res = null;
http h= new Http();
res= h.send(req);

system.debug( 'Resp--------------'+res.getbody());



4 . To comment on photo 
HttpRequest req=new HttpRequest();
req.setMethod('POST);
string comment ='message=hai ra mama';
req.setbody(comment);
req.setheader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length',string.valueof(sss.length()) );
req.setEndpoint('https://graph.facebook.com/0000000000000/comments?

access_token=hfsdfhshdfjksdf mdfbsjkf');
HttpResponse res = null;
http h= new Http();
res= h.send(req);

system.debug( 'REsp----------------------'+res.getbody());

Salesforce custom settings

Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user.

All custom settings data is exposed in the application cache,which enables efficient access without the cost of repeated queries to the database.

This data can then be used by formula fields, validation rules, Apex, and the SOAP API.

          List Custom Settings : 

A type of custom setting that provides a reusable set of static data that can be accessed across your organization.

If you use a particular set of data frequently within your application, putting that data in a list custom setting streamlines access to it.

Data in list settings does not vary with profile or user, but is available organization-wide.
Examples of list data include two-letter state abbreviations, international dialing prefixes,
and catalog numbers for products. Because the data is cached, access is low-cost and efficient
you don't have to use SOQL queries that count against your governor limits. 
EX:

Map<String_dataset_name, CustomSettingName__c> mcs = CustomSettingName__c.getAll();
CustomSettingName__c mc = CustomSettingName__c.getValues(data_set_name);


Hierarchy Custom Settings : 

A type of custom setting that uses a built-in hierarchical logic that lets you “personalize” settings for specific profiles or users.

The hierarchy logic checks the organization, profile, and user settings for the current user and returns the most specific, or “lowest,” value.

In the hierarchy, settings for an organization are overridden by profile settings, which, in turn, are overridden by user settings. 
EX: 
CustomSettingName__c mc = CustomSettingName__c.getOrgDefaults();
CustomSettingName__c mc = CustomSettingName__c.getInstance(Profile_ID);

Wednesday, 19 November 2014

Using custom label in visualforce page

A custom label can used to store a desired text value of length up to 1000. A 
custom label value can then be referenced by apex code or visualforce page. A 
custom label can store values of length up to 1000 characters in it. We can have 
a total of 5000 custom labels in an org.
Create a custom label by navigating to set up -> create -> custom label

In the below example let us see how we can access the custom label in visualforce 
page directly.

Visualforce page that accesses the custom label 

<apex:page controller="custlabelcontroller">
   <apex:form >
     <apex:pageblock >
       Value stored in custom label is: {!$Label.category}
     </apex:pageblock>
   </apex:form>

</apex:page>


Action function and Action support in Salesforce

Difference between action support and action function


Before understanding the difference between Action support and Action Function let us go through what they do and their similarities:

1. Both action support and function can be used to call a controller method using an AJAX request.
   * for example call controller onclick of a inputcheck box
   * or call a controller method onfocus of a input field
Well, they both do the same thing of calling controller method.

Difference between both:

1. Action function can call the controller method from java script.
2. Action support adds AJAX support to another visualforce component and then call the controller method. 
    for example:

     <apex:outputpanel id="outptpnl">
             <apex:outputText value="click here"/>
         <apex:actionSupport event="onclick" action="{!controllerMethodName}"  rerender="pgblck" />
     </apex:outputpanel> 
  
Here action support adds AJAX to output panel, so once you click on output panel controller method will be called.
  
3. Action function cannot add AJAX support to another component. But from a particular component which has AJAX support(onclick, onblur etc) action function can be called to call the controller method.
Example:

 <apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myactionfun" />

In this example onlick of input checkbox "myactionfun" action function is called from where controller method "actionFunMethod" gets called.
Apart from this, the main difference between the "two" action support and action function is that, the action function can also be called from java script.
Example:

<apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myJavaMethod()" />
<script>
   function myJavaMethod(){
     myactionfun();// this call the action function
  }
  </script>

Here onclick of the inputcheck box java script is called from where the action function gets called and ultimately your controller method.
Lets demo both as a full fledged example:

Click in the Input text to call controller method using action support 
Click the input check box to call Java script, then confirm in java script, upon confirmation controller method is called using action function.

Tex in the lower pageblock gets changed depending on whether the controller method is called by action support or action function. 
<apex:page controller="ActionSupFunController">
 <apex:form >
  <h1>Demonstration of difference between Action function and Action Support</h1>

  <apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/><br></br> <br></br> 
  
  Input Text <apex:inputText >
                <apex:actionSupport action="{!actionSupMethod}" event="onclick" reRender="outptText" />
             </apex:inputText> <br></br>
             
Click me to call action function method   <apex:inputcheckbox onclick="myJavaMethod()" /><br></br> <br></br>   
    <apex:pageBlock >
        <apex:outputText value="{!Display_This_String}" id="outptText"/>
    </apex:pageBlock>         

  <script>
   function myJavaMethod(){
   var checkinput = confirm('Are sure you wnat to call action function method?');
   if(checkinput == true) 
      myactionfun();
  }
  </script>
 </apex:form> 
</apex:page>

 -- Controller --

Public with sharing class ActionSupFunController {
Public string Display_This_String{get;set;}
    Public ActionSupFunController (){
     Display_This_String = 'value set in constructor';
    }
    
    Public void actionFunMethod(){
      Display_This_String = 'value set in action function method';
    }
    
    Public void actionSupMethod(){
      Display_This_String = 'value set in action Support method';
    }
}