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
sshawkinssshawkins 

Unable to make ContactId field to Null on Case object

I have a requirement to make the Case object's ContactId field to Null when the owner of the case changes. Understand that the contactId is populated by salesforce standard process based on the case owner. But is there anyway i can achieve this.?

Tried calling a Queueable class to update case record on Case After update trigger, but it still did not work when the owner is community user.
Please suggest. below is the Queueable class called from case after update trigger context. (used after update context even though update is on same object, tried before update and could see ContactId was getting updated back to case owner)
 
public class CaseContactUpdateQueueable implements Queueable {
    private Map<Id,Case> newMap;
    public CaseContactUpdateQueueable(Map<Id,Case> newMap) {
        this.newMap = newMap;
    }
    public void execute(QueueableContext context) {
        List<Case> caseList = new List<Case>();

        for(Case cs:newMap.values()){
           if(cs.ContactId != null)){
               caseList.add(new Case(Id = cs.Id,ContactId = null));
            }
        }

        update caseList;
    }
}

 
TobyKutlerTobyKutler
I do not think the way you are doing it by creating a new Case record with an existing Id will work when you do the update. Try it like this which is checking to see if the case's contact is populated and if so setting it to null;  
 
public class CaseContactUpdateQueueable implements Queueable {
    private Map<Id,Case> newMap;
    public CaseContactUpdateQueueable(Map<Id,Case> newMap) {
        this.newMap = newMap;
    }
    public void execute(QueueableContext context) {
        List<Case> caseList = new List<Case>();

        for(Case cs:newMap.values()){
           if(cs.ContactId != null)){
                cs.ContactId = null; 
                caseList.add(cs);
            }
        }

        update caseList;
    }
}