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
OssieOssie 

Help with basic trigger - Case Owner Blank

Hi,

I have written the following trigger which isn't working. When I change the Case Owner i get error message "Case Owner: owner cannot be blank". 
The Back_Up__c field is a lookup field and so exists in the User Object and so when you a user creates / updates a Case the Back_Up__c field is automatically populated via another Apex class.   

When my trigger is executed it seems that Salesforce has not populated field Back_Up__c and hence the erro rmessage above is displayed.  Is this correct?  How do i get around this? Please help?

trigger CaseAssignmentOutofOffice on Case (before update) {
       
   For (Case c : Trigger.New) {
        Case NewCase = Trigger.newMap.get(c.id);
        Case OldCase = Trigger.oldMap.get(c.id);
        
        If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed'){
        c.OwnerId = c.Back_Up__c;
        c.IsBackUp__c = True;     
        }
}
}
Dhriti MoulickDhriti Moulick
Hi,
 
give condition in your trigger as follows:
For (Case c : Trigger.New) {
        Case NewCase = Trigger.newMap.get(c.id);
        Case OldCase = Trigger.oldMap.get(c.id);
        
        If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed' && (c.Back_Up__c!=null || c.Back_Up__c!='')){
        c.OwnerId = c.Back_Up__c;
        c.IsBackUp__c = True;     
        }
}
}
OssieOssie
Hi,

I have already tried this. I still get the same error message as above "Case Owner: Owner cannot be Blank"

 
Dhriti MoulickDhriti Moulick
Hi ,

  Can you please tell me,while creating a case object,Back_Up__c field is populating record or not?Though this field is updating through apex class.

  Better you can update Back_Up__c field in after insert of Case.
 
OssieOssie
Hi,

Yes i can confirm that when creating / updating a Case then Back_Up__c is populated. But when changing the Owner i dont understand why i get the error message that Case Owner is Blank. I think you are right i will have to do a After Update. But not sure how i do this. Could you please help?   
Dhriti MoulickDhriti Moulick
Hi Ossie,

 Their is another concern,

  Is their any look up filter in Back_Up__c field?

Implement the same logic implemeted in apex class for after insert of trigger.
OssieOssie
Hi,

There is no filter!

I have implemendt the same logic but it wont work.

trigger CaseAssignmentOutofOffice on Case (after update) {

  set<id> setCaseOwnerIds=new set<id>(); 
   map<id,User> mapUser=new map<id,User>([select id, UserRole.Name, ManagerId, Senior_Coordinator__c, Back_Up__c from User where id in :setCaseOwnerIds]);

For (Case c : Trigger.New) {
        Case NewCase = Trigger.newMap.get(c.id);
        Case OldCase = Trigger.oldMap.get(c.id);
        
        if (mapUser.get(c.OwnerId)!=null){
        c.Back_Up__c=mapUser.get(c.OwnerId).Back_Up__c;
         
        If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed' && (c.Back_Up__c!=null || c.Back_Up__c!='')){
        c.OwnerId = c.Back_Up__c;
        c.IsBackUp__c = True;     
        }
        update c;
}}
}