You need to sign in to do that
Don't have an account?
gopal neeluru
How to update Custom look up field to user in Case with Account ownerId which is lookup to user using Apex Trigger
trigger Example123 on Case (before insert, before update) {
public List <Account> lstAcc = new List<Account>();
public Id accountOwner;
/* For (Case c : Trigger.new){
lst.add(c.id);
}*/
//system.debug('******' +lst);
list<case> cc= [select id,AccountId, case.Account.OwnerId, AdditionalCon__c from case where id =: Trigger.new];
system.debug('******' +cc);
for(Case cas : cc){
lstAcc = [Select Id, Ownerid from Account where Id =: cas.AccountId];
}
for (Case c: cc){
// system.debug('c.account.Ownerid**********'+c.account.Ownerid);
if((Trigger.isInsert || Trigger.isUpdate ) && Trigger.isBefore ){ //Before Insert
//system.debug('c.account.Ownerid**********'+c.account.Ownerid);
if(c.AdditionalCon__c ==null ){
for(Account a : lstAcc ){
if(c.AccountId == a.Id){
accountOwner = a.OwnerId;
}
}
system.debug('AccountOwner**********'+accountOwner);
c.AdditionalCon__c = accountOwner;
system.debug('AdditionalCon :' +c.AdditionalCon__c);
}
}
//lst.add(c);
}
//update lst;
}
public List <Account> lstAcc = new List<Account>();
public Id accountOwner;
/* For (Case c : Trigger.new){
lst.add(c.id);
}*/
//system.debug('******' +lst);
list<case> cc= [select id,AccountId, case.Account.OwnerId, AdditionalCon__c from case where id =: Trigger.new];
system.debug('******' +cc);
for(Case cas : cc){
lstAcc = [Select Id, Ownerid from Account where Id =: cas.AccountId];
}
for (Case c: cc){
// system.debug('c.account.Ownerid**********'+c.account.Ownerid);
if((Trigger.isInsert || Trigger.isUpdate ) && Trigger.isBefore ){ //Before Insert
//system.debug('c.account.Ownerid**********'+c.account.Ownerid);
if(c.AdditionalCon__c ==null ){
for(Account a : lstAcc ){
if(c.AccountId == a.Id){
accountOwner = a.OwnerId;
}
}
system.debug('AccountOwner**********'+accountOwner);
c.AdditionalCon__c = accountOwner;
system.debug('AdditionalCon :' +c.AdditionalCon__c);
}
}
//lst.add(c);
}
//update lst;
}
Try this code:
trigger caseupdate on case(before update,before insert) {
list<case> caselist=new list<case>();
map<id,string> casemap=new map<id,string> ();
set<id> caseids= new set<id> ();
caseids.addall(trigger.newmap.keyset());
for(case c :[select id, account.ownerid from case where id in : caseids]) {
casemap.put(c.id,c.account.ownerid);
}
for(case cs: trigger.new) {
cs.user__c=casemap.get(cs.id);
}
}
Here user__c is the custom lookup field on case.
Thanks,
Pratik
P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
All Answers
Try this code:
trigger caseupdate on case(before update,before insert) {
list<case> caselist=new list<case>();
map<id,string> casemap=new map<id,string> ();
set<id> caseids= new set<id> ();
caseids.addall(trigger.newmap.keyset());
for(case c :[select id, account.ownerid from case where id in : caseids]) {
casemap.put(c.id,c.account.ownerid);
}
for(case cs: trigger.new) {
cs.user__c=casemap.get(cs.id);
}
}
Here user__c is the custom lookup field on case.
Thanks,
Pratik
P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
Can You Please Mention the same in Apex Handler.
Test Class.
Public Static Void UpdateUser(List<Case> casemap)
But Trigger.newmap how to change