Monday, May 2, 2016

Salesforce  - A brief introduction



So what is salesforce? Well, when i first got selected to work in the salesforce domain i was really confused. When I heard my interviewer tell me  that you will be probably taken up into saleforce team, I thought OMG they are going to put me in marketing section. Maybe due to my expression, he told me that its  also a development tool and asked me to search about it.


So, when i searched more about salesforce , It was stated that its a CRM tool
Well what is a CRM tool ?.. lol  I was also unfamiliar with that, CRM is Customer Relationship Management Tool .

 Ok, what is CRM?
Well it basically deals with communication with the clients , storing of customer data and automation of certain workflows etc.

Lets move on to sales-force,

Ok you might be familiar with tables in database. What is a table used for? We use it to store details about a certain object. Similarly object stores the details or properties of a particular entity. Each attribute of such an object is called a field. We can create custom objects in salesforce which just like the tables.

When we create a custom object we have to give it a name , it will  have a default field which will be the key of the table.It can auto number or any value as we decide then we can define rest of the fields in the custom  object (ie. the table)



When we create a field we can define the type and name of the field


OK, let me tell you an example

Suppose you have to store the details  of a student, here student can be selected as the name of the custom object. OK so we create a custom object student and it will have a primary field Student name which will be a required field.

Now we can define the rest of the fields for student such as 

  •  Address - Long Text
  • Phone Number - Phone
  • State - Picklist
  • District - Picklist
  • Known skills - Multi picklist  etc

You might be familiar with  Long text and Phone data type , A picklist is just like drop-down box
We can make district a related field which will show district only pertaining to a corresponding state.
A multipicklist is one in which we can choose  more than one option according to the criteria.
After we create an object we can define validation rules.
We can use workflow rules and process builder to automate on what happens when certain condition are met like automated email alert. 
We can define how the layout would be for different users.
Apex is the coding language and we use it in cases to define complicated function and triggers 
We can also design the front end using visual-force which uses apex tags and is very similar to HTML tags.



We will define the certain scenarios and basics with examples in coming sessions :)

Sunday, May 1, 2016

COLLECTIONS IN APEX


Collection means a group of elements.We are familiar with arrays in c,c++ etc. its a classic example of collection.
But arrays can contain only single type of data.Often we have to form collection of elements of different types of data as in cases like defining properties of objects.In such cases arrays cant be used.We need more flexibility.This led to the formation of
versatile collections.Coming to Salesforce Apex,we have three  types of collections.
ie,
   1.Lists
   2.Sets
   3.Maps


 Let's describe those in detail

1.LISTS

   A list is an ordered collection of elements where each element is stored in specific location called index so that each element can be accessed by its index.So list is somewhat like array itself.When a list is created we have to specify its type.It can be anything from ID,String to  sObjects.



Syntax:-



  • To create an empty list of String


List<String> Lst = new List<String>();



  • To create a nested list


List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();


  • To define a new list

List<Integer> NList = new List<Integer>();



  • Adds a second element of value 27 to the end of the list
NList.add(27);                   

  • Retrieves the element at index 0                                

Integer i = NList.get(0);                   

  • Adds the integer 1 to the list at index 0
NList.set(0, 1);       


  •   Removes all elements from the list
              
NList.clear();                    









Sample Code:

trigger linkmyparent on child__c (after insert) {
  
   
   list<id> plist= new list<id>();

    
   for(child__c child:trigger.new)
   {
       
       plist.add(child.rltn__c);
        }

   list<parent__c> parent=[select Id,Count__c from parent__c where id in:plist];
    system.debug(parent);
   integer count=0;
    
   for(child__c c:trigger.new)
    {
        for(parent__c p:parent)
        {
            if(c.rltn__c==p.id)
           
           p.Count__c++;
        }
         
    }
   update parent;


}




2.SET


A set is similar to list except that its unordered. It does not contain duplicate values.
It can contain primary data types, like Integer,strings etc. It can also contain more complex data types, such as sObjects. Adding elements to the set will dynamically increase the size of the set.



Syntax:



  •  Defines a new set with two elements

             Set<string> stringset = new Set<String>{'p', 'q + r'};


  • Defines a new set that contains the elements of the set created in the previous step

             Set<String> s2 = new Set<String>(s1);



Sample Code:


Set<String> parentid= new Set<String>();

for(Parent__c p:[select id from Parent__c ])
{
    parentid.add(p.id);

  }




3.MAPS


Maps  are similar to hash tables.Maps represent collection of key value pairs.
They are  organized based on the key. It uses the key to access the elements in the collection.
Keys and values in maps can be any data type—primitive types, collections, sObjects, user-defined types
Map is used when you need to access elements by using key.



Syntax:



  •   Define a new map with key Id and value string
               Map<ID, String> maplst = new Map<ID, String>();


  •  Define a new map with key Id and value list of strings


               Map<ID, list<String>> maplst = new Map<ID, list<String>>();


  •  Define a new map with key Integer and value string and populating the map with values


                Map<Integer, String> color = new Map<Integer, String>();
                m.put(1, 'Red');                  
                m.put(2, 'Blue'); 


  •  Define a new map with key Id and value sObjects


                Map<Id, sObject> m = new Map<Id, sObject >();


  •  Define a new map with key string and value string


                Map<String, String> c = new Map<String, String>();




Sample Code:

Map<Id, List<Traineeobj__c >> mapChild  = new Map<Id, List<Traineeobj__c >>();

 List<id> parentid=new list<id>();  
    
    for(Traineeobj__c t:trigger.new)
    {
        parentid.add(t.parent__c);
    }

    for(Traineeobj__c t :[select id, name, parent__c, Status__c FROM Traineeobj__c WHERE parent__c in :parentid])
    {
        if(mapChild.containsKey(t.parent__c))
        {
            mapChild.get(t.parent__c).add(t);
        }
        
        else
        {
            mapChild.put(t.parent__c, new List<Traineeobj__c> {t});
        }

    }