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
Deepu2123Deepu2123 

First error: Field is not writeable: Note.ParentId

Hi Developers,
Please help me if i did any wrong becasue batch is throwing the error.

First error: Field is not writeable: Note.ParentId

global class BatchDuplicateContactCleanup3 implements Database.Batchable<Sobject> {
    
    //Database.executeBatch(new BatchDuplicateContactCleanup3(), 30);  
   
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT id,Duplicate_Of__c FROM Contact where Duplicate_Of__c!=null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope) {    
        
        set<Id> dupConIds = new Set<Id>();
        Map<string,string> dupcontactsmap = new Map<string,string>();
        
        for(Contact vCon: (List<Contact>) scope){
            dupConIds.add(vCon.id);
            dupcontactsmap.put(vCon.id,vCon.Duplicate_Of__c);         
        }
        
        List<Note> listNotes = [select id,parentid from Note where parentid=:dupConIds];        
        List<Note> updateNotes = new List<Note>();
        
        if(!listNotes.isEmpty()){            
            for(Note vNote: listNotes){
                if(dupcontactsmap.get(vNote.parentid) != null){
                //Attachment b = New Attachment(Name = a.Name, Body = a.Body);
               //b.parentID = newParentId;
                    vNote.parentid = dupcontactsmap.get(vNote.parentid);
                    updateNotes.add(vNote);
                } 
            }
            if(!updateNotes.isEmpty()){
                update updateNotes;
            }
        }
        
        List<Attachment> listAttachments = [select id,parentid from Attachment where parentid=:dupConIds];        
        List<Attachment> updateAttachments = new List<Attachment>();
        
        if(!listAttachments.isEmpty()){            
            for(Attachment vAtt: listAttachments){
                if(dupcontactsmap.get(vAtt.parentid) != null){
                    vAtt.parentid = dupcontactsmap.get(vAtt.parentid);
                    updateAttachments.add(vAtt);
                } 
            }
            if(!updateAttachments.isEmpty()){
                update updateAttachments;
            }
        }
    }
    
    global void finish(Database.BatchableContext bc) {
    }    
}

Thanks,
Saicharan.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Saicharan,

I trust you are doing very well.

ParentId becomes a read-only field after initial insertion, as is true of any master-detail link field. The parent ID is not writeable after insert. 

Please refer to the below link with a similar discussion which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/16129/how-to-update-parentid-on-attachments-in-salesforce

https://salesforce.stackexchange.com/questions/106558/reparent-notes-and-attachments-using-apex

https://developer.salesforce.com/forums/?id=906F000000090WSIAY


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas