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:
Trigger on Case Object:
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:
- public class PairStrings {
- String subject, description;
- public PairStrings(String str1, String str2) {
- subject = str1;
- description = str2;
- }
- public Boolean equals(Object obj) {
- if (obj instanceof PairStrings) {
- PairStrings p = (PairStrings)obj;
- return ((subject==p.subject) && (description==p.description));
- }
- return false;
- }
- public Integer hashCode() {
- return (31 * subject.hashCode()) ^ description.hashCode();
- }
- }
Trigger on Case Object:
- trigger CaseTrigger1 on Case (before insert, before update)
- {
- Set<String> subjects = new Set<String>();
- Set<String> descriptions = new Set<String>();
- for(Case currCase : Trigger.New)
- {
- subjects.add(currCase.subject);
- descriptions.add(currCase.Description);
- }
- Map<PairStrings, List<Account>> accountMap = new Map<PairStrings, List<Account>>();
- for(Account currAcc : [SELECT Id, Name, Description, AccountNumber FROM Account WHERE Name in:subjects AND AccountNumber in:descriptions])
- {
- PairStrings ps = new PairStrings(currAcc.Name, currAcc.AccountNumber);
- if(!accountMap.containsKey(ps))
- {
- accountMap.put(ps, new List<Account>{currAcc});
- }
- else
- {
- accountMap.get(ps).add(currAcc);
- }
- }
- for(Case currCase : Trigger.new)
- {
- PairStrings ps = new PairStrings(currCase.subject, currCase.Description);
- if(accountMap.containsKey(ps))
- {
- if(accountMap.get(ps).size() > 1)
- {
- currCase.Account_Status__c = 'More than one account found';
- }
- else
- {
- currCase.AccountId = accountMap.get(ps)[0].Id;
- }
- }
- else
- {
- currCase.Account_Status__c = 'No matching account found';
- }
- }
- }
Really enjoyed this article.Much thanks again. Want more
ReplyDeletesalesforce training
salesforce online training