+ Start a Discussion
OssieOssie 

Change Query Owner using After Update Trigger

Hi All,

Need some help please.

I have created a trigger which should update the OwnerId on Case object using UserId from field Back_Up__c On Case object.

But i cant get this to work.  Yet i get no error messages. Any help would be most appreciated. 

trigger CaseOutofOffice on Case (after update) {

    If (Trigger.isAfter){
        List<Case> Cases = new List<Case>();
            for (Case c : cases) {
                If (c.Status != 'Closed' && (c.Back_Up__c!=null || c.Back_Up__c!='')){
                    c.OwnerId = c.Back_Up__c;
                    c.IsBackUp__c = True;                   
}
    Update c;
}}}
Best Answer chosen by Ossie
ManojjenaManojjena
Hi Ossie,


Try with below code as trigger.new record is always read only so you can not update trigger.new records .You need to query with the relared id from daabase .
 
trigger CaseOutofOffice on Case (after update) {
	List<Case> caseListToUpdate = new List<Case>();
	Set<Id> caseIdSet=new Set<Id>();
	for (Case cs : trigger.new) {
		If (cs.Status != 'Closed' && (cs.Back_Up__c!=null || cs.Back_Up__c!='')){
		   caseIdSet.add(cs.Id);
		}
	}
	for(case cse :[SELECT id,OwnerId,Back_Up__c,IsBackUp__c FROM Case WHERE Id IN :caseIdSet ]){
	   cse.OwnerId=cse.Back_Up__c
	   cse.IsBackUp__c = True;
	   caseListToUpdate.add(cse);
	}
	try{
	update caseListToUpdate;
	}catch(dmlException de ){
	  System.debug(de);
	}
}

Let me know any issue .
Thanks 
Mnaoj
 

All Answers

surasura
may I know from where your Cases list get populated ? becuase from code you have shared cases is an empty list therfore your for loop never runs .
surasura
try this
trigger CaseOutofOffice on Case (after update) {

    If (Trigger.isAfter){
        List<Case> Cases = new List<Case>();
            for (Case c : trigger.new) {
                If (c.Status != 'Closed' && (c.Back_Up__c!=null || c.Back_Up__c!='')){
                    c.OwnerId = c.Back_Up__c;
                    c.IsBackUp__c = True;  
                     Cases.add(c)             
                }
          }
         updater Cases;

}}

 
ManojjenaManojjena
Hi Ossie,


Try with below code as trigger.new record is always read only so you can not update trigger.new records .You need to query with the relared id from daabase .
 
trigger CaseOutofOffice on Case (after update) {
	List<Case> caseListToUpdate = new List<Case>();
	Set<Id> caseIdSet=new Set<Id>();
	for (Case cs : trigger.new) {
		If (cs.Status != 'Closed' && (cs.Back_Up__c!=null || cs.Back_Up__c!='')){
		   caseIdSet.add(cs.Id);
		}
	}
	for(case cse :[SELECT id,OwnerId,Back_Up__c,IsBackUp__c FROM Case WHERE Id IN :caseIdSet ]){
	   cse.OwnerId=cse.Back_Up__c
	   cse.IsBackUp__c = True;
	   caseListToUpdate.add(cse);
	}
	try{
	update caseListToUpdate;
	}catch(dmlException de ){
	  System.debug(de);
	}
}

Let me know any issue .
Thanks 
Mnaoj
 
This was selected as the best answer
OssieOssie
Hi Manoj,

Thast perffect! You Legend :-) 
Thanks you so much. i can ask last question plz? if i wanted to simplify this by only displayign an error message (adderror) rather than change the ownerId would i use the same code as above or would this require significant changes? 
ManojjenaManojjena
Hi Ossie,

You need to change the trigger for sure ,If you want to display error message ,then first you need to change the event to before from after .
Second no need to update the record .

I think below code is enough please have a try .
 
trigger CaseOutofOffice on Case (before update) {
	for (Case cs : trigger.new) {
		If (cs.Status != 'Closed' && (cs.Back_Up__c!=null || cs.Back_Up__c!='')){
		  cs.adderror('Your error message here ');
		}
	}
}

Let me know any issue .
Thanks 
Manoj
OssieOssie
Hi Manoj,

This did not work. The trigger is firing everytime, even when the Back_Up__c is BLANK or NULL.  
ManojjenaManojjena
Hi Ossie ,

What exactly you want ,when to throw the error ? According to the trigger it will trrow you error when case =close and backup is null .

Please clarify.
Thnaks 
Manoj
OssieOssie
Hi Manoj,

I have created a custom field called "Back_Up__c" on the User Object. I have then created a custom field called "Back_Up__c" on the Case Object also.

If a user is out of the office they must then populate the custom field "Back_Up__c" in the User Object. Therefore, if a User then tries to assign a Case to a User which has Out of Office then the trigger should fire and erro rmessage should be adderror('Please assign Case to Back_Up__c (i.e.John Smith etc). But the problem im having is that the trigger is firing even when the User is NOT (= BLANK or NULL ) out of the office. Instead error message is displayed 'Please assign Case to null'. I think my Back_Up__c field on the Case object is not being populated in time, when changing the owner..