Wednesday, December 7, 2016

Collections in Salesforce | When do we use List, Set, Map.

List, Set, Map are called collections in Apex.

List : 

A list is an ordered collection.

1) so use list when you want to identify list element based on Index Number.
2) list can contain Duplicates.

EX: List<Account> accList = new List<Account>();


Set :

A set is an unordered collection.

1) Do not contain any duplicate elements. So, use set if you want to make sure that your collection should not contain Duplicates.

EX: Set<Account> accSet = new Set<Account>();

Set<String> setString = new Set<String>();
// Add two strings to it
setString .add('item1');
setString .add('item2');


Map : 

A map is a collection of key-value pairs.

Each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object.

EX: Map<Id, Account> accMap = new Map<Id, Account>();


Map<Integer, String> mapOfString = new Map<Integer, String>();
mapOfString.put(1, 'string1');
mapOfString.put(2, 'string2');
System.assert(mapOfString.containsKey(1));
String value = mapOfString.get(2);
System.assertEquals('string2', value);
Set<Integer> s = mapOfString.keySet();


Map<String, String> myMap = new Map<String, String>{'a' => 'b', 'c' => 'd'};





Example 1:- 

Trigger to Populate the Account Field(Accunt Type) on the Contact record(In Type__C Custom Field)   (only insert scenario).


If we use List and not Map


Apex Trigger:


trigger ContactTriggerWithList on Contact (before insert){
Set<Id> SetAccountId = new Set<Id>(); // Use set to collect unique account ID
for(Contact con: Trigger.new){
if(con.AccountId != null){
SetAccountId.add(con.AccountId); // add Account in Set
}
}
if( SetAccountId.size() >0 ){
List<Account> listAccount = [ Select Id, Name, Type from Account where Id IN :SetAccountId ]; // Query Related Account
for(Contact con: Trigger.new) {
if(con.AccountId != null){
for(Account acc: listAccount){
if(con.AccountId == acc.Id){
con.Type__c = acc.Type;
}
}
}
}
}  
}


NOTE:- In this we are using List of Accounts, Where we have to loop through the matching Account every time to populate the Type__c in the second for loop. In this case need to use nested loop.


Instead of  Nested Loop We can use the Map.


Same Trigger using the Map:


trigger ContactTriggerWithMap on Contact (before insert){
    Set<Id> SetAccountId = new Set<Id>();
    for(Contact cont: Trigger.new) {
        if(cont.AccountId != null) {
            SetAccountId.add(cont.AccountId);
        }
    }
    
    if(SetAccountId.size() > 0 ) {
        Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN :SetAccountId ]);
        for(Contact cont: Trigger.new){
            if(cont.AccountId != null && mapAccount.containsKey(cont.AccountId) ) {
                cont.Type__c = mapAccount.get(cont.AccountId).Type;
            }
        }
    }
}

NOTE:- Here we are using Map of Account so we can directly use the Map to get the Account record. No need of 2nd for loop here.


Tuesday, December 6, 2016

Live Agent Implementation in Salesforce.

Live Agent is a comprehensive chat solution that makes it easy for your organization's agents and support supervisors to assist customers in real time.

Step 1 :- Open live Agent Setting

Customize --> Live Agent----> Live Agent Settings




Step 2:- Enable Live Agent 

Click on Enable Live Agent Chat Box and click on save button




Once you will enable the Live Agent below object will auto created in your org.
  1. Live Chat Visitors
  2. Live Chat Transcripts
  3. Live Chat Transcript Events
  4. Live Agent Sessions
  5. Live Agent Supervisor
  6. Live Agent
  7. Quick Text

Step 3:- Setup Live Agent User.

Setup--> My Setting -> Advanced User Details--> Then click on Live Agent User checkbox and Service Cloud User Checkbox.





Step 4:- Set Skills

Click on Setup --> Live Agent --> Skills





 Then Click on New button and setup skill like below screen.




Step 5:- Upload Online and Offline Image

Upload online and offline image in "Static Resource" .





Step 6:- Setup Chat Buttons & Invitations

Setup--> Customize -->Live Agent --> 
Chat Buttons & Invitations.




Then click on New Button. And add all below details.


     


Once you will click on Save button the chat button code will come.

Copy and Paste the Chat Button Code in Notepad.





Step 7:- Create Deployment 

Click on Setup-> Customize --> Live Agent--> Deployments .




Then click on New button and provide all below detail.






Then click on Save button. After Saving Deployment code will come like below. 





Copy and Paste the Deployment Code in Notepad (Below Chat button Code).

Create the HTML page with Chat Button Code and Deployment Code.(Save the Notepad file as .html)





Now Setup the Agent Console ( Console App )

Step 1:- Open App Wizard

Setup--> Create --> Apps




Step 2:- Create new Console App

Click on New button then select console button .


Enter the Require Details While Creating Application.

While creating the App on Step 6 select the "Include Live Agent in this App" check box like below.




Now Live Agent App is Ready to use.



How To Test

Open The HTML page which you created.(Contains code for Chat Button and Deployments).

Once you page will open and Agent is not online the below Html page will show you offline image.








Once Agent will come online from agent console then image will change on client HTML page

How Agent will come online.


Step 1:- Select Live Agent Console App.









Step 2) Then from the Live Agent Click on Online button.







Now All Set Agent is online :)
Step 3) Refresh you HTML page.

Once Agent will Online the HTML will show you online image like below.



Then click on Image the set session will start. 



Accept chat from agent console.