Thursday, February 12, 2015

Distinct keyword in salesforce


Distinct Records in Salesforce

In a table or a Object(In Salesforce), a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
In SQL we can write a Query to extract distinct records from table by making use of a sample Query like this 
SELECT DISTINCT column_name,column_name
FROM table_name;
But in SOQL there is no distinct keyword to do the operation and it is really boring to write out own custom logic to pick the distinct values. Please care to drop this idea in App exchange.
Eventhough Salesforce suggests the use of aggregate methods(Partial solution) to solve this I felt it's complex to implement it and it didn't work as expected. So I need to come up with a custom logic to pick distinct records from a custom object and insert it in to a unrelated second object.
/*****
***Description: The unique distinct records are picked from
***sessions aggregate object and inserted as unique records in detail reporting group object
******/


trigger InsertDistinctReportGroudId on Sessions_Aggregate__c (after insert) { 
//Collect the whole List of Session Aggregate data
        list SAlist = [select id,Reporting_Group_Name__c,SOName__c from Sessions_Aggregate__c where Reporting_Group_Name__c!=null  limit 9999];
        system.debug('SAlist '+SAlist);
//create a new set to pick the unique records and add the distinct values inside the set
        Set s1 = new Set();    
    for(Sessions_Aggregate__c c:SAlist){
//Iterate thhru the main list and assign the distinct values from a set
        s1.add(c.Reporting_Group_Name__c);
    }
        system.debug('*****'+s1.size());
//Collect the pre-Existing records in the second custom object to compare 
        list oldRGlist = [select ReportingGroupName__c from Detail_Reporting_Group__c limit 9999];
        List distinctRGnames = new List();
//Create sets to compare the existing record values of the second object with the 1st object
        Set s2 = new Set();
        Set s3 = new Set();   
    if(oldRGlist.size()>0){
    for(Detail_Reporting_Group__c S:oldRGlist){
        s2.add(S.ReportingGroupName__c);
    }
    }
    for(string s : s1){
        if(s2.contains(s)){
//if 1st object list contains any of the field values from the 2nd object the remove the repeating value from the 2nd object set.
            s2.remove(s);    
        }
    else{
//if there is no duplicates the add the 2nd obj value to the 1st object list's value 
    s3.add(s);
     }           
    }    
//Now we got all the distinct values in the set now add these value in to a master list for Insertion.
        distinctRGnames.addAll(s3);
        system.debug('+distinctRGnames+'+distinctRGnames);
          list<Detail_Reporting_Group__c> RGlist= new list<Detail_Reporting_Group__c>();
          for (integer i=0;i<distinctRGnames.size();i++){
            Detail_Reporting_Group__c DGR=new Detail_Reporting_Group__c();
            DGR.ReportingGroupName__c=distinctRGnames[i];
             RGlist.add(DGR);    
    }
//Insert The main List
    insert RGlist;


This would serve the purpose of collecting the distinct records of one object and inserting it in to another object.

Authored by: Nirmal Christopher,
 Salesforce.com Certified Developer, 
Technical Consultant, 
Global Tech & Resources, Inc. (GTR).


1 comment:

asterisk dialer said...

This post is very simple to read and appreciate without leaving any details out. Great work !blended call center solutions