Please Suggest how to implement this condition in trigger ---if lead owner getting changed to queue name then we need to populate account field on case object with account owner name in trigger
Hi Raj, Thanks for the Reply.Actually i am working on two requirements .In first one i have written trigger which match account and case domain name and field on case which gives matched account name in account matched field.Now i want -if case owner getting changed to queue name then we need to get account field which is alredy filled on case object with that account owner name in trigger.I am sharing code for More understanding
public class CaseTriggerHandler{ public Static void onBeforeInsert(list<Case> newCaseLst){ setCaseFlds(newCaseLst,null); } public Static void onBeforeUpdate(list<Case> newCaseLst, map<Id, sobject> oldCaseMap){ setCaseFlds(newCaseLst, oldCaseMap); } public Static void setCaseFlds(list<Case> newCaseLst, map<Id, sobject> oldCaseMap){ set<String> emlDomainSet = new set<String>(); list<Case> caseLstToProcess = new list<Case>(); for(Case Case_obj : newCaseLst){ //populate account matched Field if(!String.isBlank(Case_obj.Email__c) && (oldCaseMap == null || Case_obj.Email__c != ((Case)oldCaseMap.get(Case_obj.Id)).Email__c)){ emlDomainSet.add('%' + extractDomainFromEmail(Case_obj.Email__c) + '%'); caseLstToProcess.add(Case_obj); } } if(!emlDomainSet.isEmpty()){ map<String, Id> websiteAcctIdMap = new map<String, Id>(); String webDomainName; for(Account acct : [Select Id, Website, Name From Account Where Website != null AND Website like : emlDomainSet]){ webDomainName = acct.Website.replace('https://', '' ).replace('http://', '' ); webDomainName = webDomainName.replace('www.', '' ); webDomainName = webDomainName.replace('/', '' ); webDomainName = webDomainName.toLowerCase(); websiteAcctIdMap.put(webDomainName, acct.Id); } if(!websiteAcctIdMap.isEmpty()){ String emailDomain; for(Case CaseToUpdate : caseLstToProcess){ emailDomain = extractDomainFromEmail(CaseToUpdate.Email__c); if(websiteAcctIdMap.containsKey(emailDomain) && CaseToUpdate.Account_Matched__c != websiteAcctIdMap.get(emailDomain)){ system.debug('-------------1-------------'); CaseToUpdate.Account_Matched__c = websiteAcctIdMap.get(emailDomain).ownerId; } }//end for loop }//end if }//end outer if } public Static String extractDomainFromEmail(String email) { // for e.g. email = abc@testDomain.com the domain will be testDomain.com String startChar = '@'; if(email != null && email.contains(startChar)){ return email.substring(email.indexOf(startChar)+1); } return null; } }
https://www.biswajeetsamal.com/blog/check-case-owner-is-a-user-or-queue/
Thanks for the Reply.Actually i am working on two requirements .In first one i have written trigger which match account and case domain name and field on case which gives matched account name in account matched field.Now i want -if case owner getting changed to queue name then we need to get account field which is alredy filled on case object with that account owner name in trigger.I am sharing code for More understanding
public class CaseTriggerHandler{ public Static void onBeforeInsert(list<Case> newCaseLst){ setCaseFlds(newCaseLst,null); } public Static void onBeforeUpdate(list<Case> newCaseLst, map<Id, sobject> oldCaseMap){ setCaseFlds(newCaseLst, oldCaseMap); } public Static void setCaseFlds(list<Case> newCaseLst, map<Id, sobject> oldCaseMap){ set<String> emlDomainSet = new set<String>(); list<Case> caseLstToProcess = new list<Case>(); for(Case Case_obj : newCaseLst){ //populate account matched Field if(!String.isBlank(Case_obj.Email__c) && (oldCaseMap == null || Case_obj.Email__c != ((Case)oldCaseMap.get(Case_obj.Id)).Email__c)){ emlDomainSet.add('%' + extractDomainFromEmail(Case_obj.Email__c) + '%'); caseLstToProcess.add(Case_obj); } } if(!emlDomainSet.isEmpty()){ map<String, Id> websiteAcctIdMap = new map<String, Id>(); String webDomainName; for(Account acct : [Select Id, Website, Name From Account Where Website != null AND Website like : emlDomainSet]){ webDomainName = acct.Website.replace('https://', '' ).replace('http://', '' ); webDomainName = webDomainName.replace('www.', '' ); webDomainName = webDomainName.replace('/', '' ); webDomainName = webDomainName.toLowerCase(); websiteAcctIdMap.put(webDomainName, acct.Id); } if(!websiteAcctIdMap.isEmpty()){ String emailDomain; for(Case CaseToUpdate : caseLstToProcess){ emailDomain = extractDomainFromEmail(CaseToUpdate.Email__c); if(websiteAcctIdMap.containsKey(emailDomain) && CaseToUpdate.Account_Matched__c != websiteAcctIdMap.get(emailDomain)){ system.debug('-------------1-------------'); CaseToUpdate.Account_Matched__c = websiteAcctIdMap.get(emailDomain).ownerId; } }//end for loop }//end if }//end outer if } public Static String extractDomainFromEmail(String email) { // for e.g. email = abc@testDomain.com the domain will be testDomain.com String startChar = '@'; if(email != null && email.contains(startChar)){ return email.substring(email.indexOf(startChar)+1); } return null; } }