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
Nisha M 6Nisha M 6 

Trigger on Case to update a look up field

Hi,
I was trying out a simple trigger on Case Object.There is a field Hiring_Manager__c (looks up to User) in Case .On update or insert of a case, this field has to be populated with the Case Owner's Manager.I created the trigger as follows.It is not bulkified as I was just trying out for a single record.
I could see the value getting populated correctly in debug statements.But it is not updated on the record.

trigger HiringManagerupdate_case on Case (before insert,before update) {
    Case updatedCaseRec= [select id,ownerId,Hiring_Manager__c,CaseNumber from case where id in :Trigger.new];
      system.debug('Case Number'+updatedCaseRec.CaseNumber);
      system.debug('Manager before updation'+updatedCaseRec.Hiring_Manager__c);
    Map<id,String> mapOwnerMgr=new Map<id,String>();
    try{
       User caseOwner = [Select Id,Name, ManagerId, Manager.Email From User Where Id = :updatedCaseRec.ownerId];
                updatedCaseRec.Hiring_Manager__c=caseOwner.ManagerId;
                
    }
    Catch(DMLException de){
        system.debug('Could not find Manager');
    }
    system.debug('Manager'+updatedCaseRec.Hiring_Manager__c);
 
}
karthikeyan perumalkarthikeyan perumal
Hello Nisha, 

Try below code. 
trigger HiringManagerupdate_case on Case (before insert,before update) {
    Case updatedCaseRec= [select id,ownerId,Hiring_Manager__c,CaseNumber from case where id in :Trigger.new];
      system.debug('Case Number'+updatedCaseRec.CaseNumber);
      system.debug('Manager before updation'+updatedCaseRec.Hiring_Manager__c);
    Map<id,String> mapOwnerMgr=new Map<id,String>();
	User caseOwner = [Select Id,Name, ManagerId, Manager.Email From User Where Id = :updatedCaseRec.ownerId];
	
    try{ 
	         
				for (case Tempcase: updatedCaseRec)
				{
                Tempcase.Hiring_Manager__c=caseOwner.ManagerId;
				}
				update Tempcase;
                
    }
    Catch(DMLException de){
        system.debug('Could not find Manager');
    }
    system.debug('Manager'+updatedCaseRec.Hiring_Manager__c);
 
}

Hope it will help you. mark best ANS if works for you.

Thanks
karthik
 
Dileep KumarDileep Kumar

Hi Nisha,

trigger HiringManagerupdate_case on Case (before insert,before update) {
for(Case  cas :Trigger.new){
        try{
       User caseOwner = [Select Id,Name, ManagerId, Manager.Email From User Where Id = :cas.ownerId];
                cas.Hiring_Manager__c=caseOwner.ManagerId;
                
    }
    Catch(DMLException de){
        system.debug('Could not find Manager');
    }
    system.debug('Manager'+cas.Hiring_Manager__c);
 
}

I hope.It will work.please try and let me know.If it will work then mark as a answer.

Thanks,

Dileep

Nisha M 6Nisha M 6
Thank you both for the reply,
@Karthik - I tried the code.But the variable Tempcase  at line 14 doesnt have any scope outside for loop.Again, we can't iterate over a single Case object.It should be a collection.
@Dileep: The code is working fine.Thanks.But I couldn't understand the difference in logic :(
If I am aiming at updating a single record, mine and urs logic looks the same (plz correct me if I am wrong).
Again, Have a doubt regarding update statement at line 14 mentioned in Karthik's reply.Do we need to explicitly call update for before triggers? My understanding is the save and commit happens after the field update for before triggers and hence the changes will be saved automatically without calling update statement.
One more stuff- Will calling select inside for loop affect governor limit?
Dileep KumarDileep Kumar

Hi Nisha,

we are writing trigger generally for all cases ,when DML operation will perform it will fire.we are not writing trigger for particular one record.If you want to update particular one record through trigger then it is possible.we will have to do some change in filter criteria of this query for that purpose then it will work.

Have a doubt regarding update statement at line 14 mentioned in Karthik's reply.Do we need to explicitly call update for before triggers?

[Good Question ]

Actually,we are writing trigger on particular Object.In this case ,when we will update record of same trigger object then we don't need to write upade keyword .Because it will automaticall update,we already mentioned insert and update thing inside 'trigger Event'.again we will write update inside for loop then it will wrong and it will create problem.It can exceed DML operation.Please always avoid DML operation inside for loop.

 

Other thing,when we will update,insert  some other Object Except Trigger object. then we need to use insert ,update .But we will not write this inside for loop to avoid Dml inside for loop we can use List<object> objList; for Insert List<Objcet> objList1 = new List<Object>(); outside for loop and you can add multiple record into this list and after for loop we need to write 'Update objList; ' and ' insert objList1 ;'

Please mark this as a best answer then other can take help from this also.

if you will face any problem please post here.

Thanks,Dileep 

Dileep KumarDileep Kumar

Hi Nisha,

One more stuff- Will calling select inside for loop affect governor limit?

we can call select statement  inside for loop it will not affect governor limit but we can not write insert, update inside for loop.

Thanks,

Dileep