function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Parth SrivastavaParth Srivastava 

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

Raj VakatiRaj Vakati
Try this
 
trigger CaseOwnerCheck on Case (before update){
    Map<Id,Case> caseOldMap = Trigger.oldMap ; 
	Map<Id,Case> casenewMap = Trigger.newMap ; 
	
	for(Case c : casenewMap.values()){
		if(c.OwnerId != caseOldMap.get(c.Id).OwnerId && string.valueOf(c.OwnerId).startsWith('00G')){
			
			c.OwnerId = c.Account.OwnerId ; 
		}
	
	
     
}

https://www.biswajeetsamal.com/blog/check-case-owner-is-a-user-or-queue/
Parth SrivastavaParth Srivastava
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;     }     }