Thursday, 25 June 2015

equlas and hashcode method example in Salesforce

Here is the simple real time example of Equlas and HashCode method usage in Salesforce,

You can compare more than one field in SOQL Query for bulk insert and update operation. Based on the comparison search result you can do your logic. The below example will explain the functionality as follows,

Case Object:

1. Status(Standard Field)
2. Description(Standard Field)

Account Object:

1. Name(Standard Field)
2. AccountNumber(Standard Field)

Scenario:

While insert/update Case record, if both status and Description field matches to the Account name and AccountNumber means, i need to update the case with matched account.

For e.g:
Case Status: Arun
Case Description: 123

Account Name: Arun
AccountNumber: 123

The above case record matched with account field values. So in this scenario need to update account field value on case. If Multiple/No related accounts found, then I just updated another field in Case.

Wrapper Class with Equals and HashCode Method:


  1. public class PairStrings {
  2.     String subject, description;
  3.     public PairStrings(String str1, String str2) {
  4.         subject = str1;
  5.         description = str2;
  6.     }
  7.     public Boolean equals(Object obj) {
  8.         if (obj instanceof PairStrings) {
  9.             PairStrings p = (PairStrings)obj;
  10.             return ((subject==p.subject) && (description==p.description));
  11.         }
  12.         return false;
  13.     }
  14.     public Integer hashCode() {
  15.         return (31 * subject.hashCode()) ^ description.hashCode();
  16.     }
  17. }


Trigger on Case Object:


  1. trigger CaseTrigger1 on Case (before insert, before update)
  2. {
  3.     Set<String> subjects = new Set<String>();
  4.     Set<String> descriptions = new Set<String>();
  5.     for(Case currCase : Trigger.New)
  6.     {
  7.         subjects.add(currCase.subject);
  8.         descriptions.add(currCase.Description);
  9.     }
  10.    
  11.     Map<PairStrings, List<Account>> accountMap = new Map<PairStrings, List<Account>>();
  12.     for(Account currAcc : [SELECT Id, Name, Description, AccountNumber FROM Account WHERE Name in:subjects AND AccountNumber in:descriptions])
  13.     {
  14.         PairStrings ps = new PairStrings(currAcc.Name, currAcc.AccountNumber);
  15.        
  16.         if(!accountMap.containsKey(ps))
  17.         {
  18.             accountMap.put(ps, new List<Account>{currAcc});
  19.         }
  20.         else
  21.         {
  22.             accountMap.get(ps).add(currAcc);
  23.         }
  24.     }
  25.    
  26.     for(Case currCase : Trigger.new)
  27.     {
  28.         PairStrings ps = new PairStrings(currCase.subject, currCase.Description);
  29.        
  30.         if(accountMap.containsKey(ps))
  31.         {
  32.             if(accountMap.get(ps).size() > 1)
  33.             {
  34.                 currCase.Account_Status__c = 'More than one account found';
  35.             }
  36.             else
  37.             {
  38.                 currCase.AccountId = accountMap.get(ps)[0].Id;
  39.             }
  40.         }
  41.         else
  42.         {
  43.             currCase.Account_Status__c = 'No matching account found';
  44.         }
  45.     }
  46.    
  47. }


1 comment:

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...