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
sumit dsumit d 

Field is not writeable in test class

 HI All,
       I have a test class which is giving me error when i try to run it.
System.SObjectException: Field is not writeable: rie__Registrant_Exhibitor__c.rie__Registration__c
My class is below:-
public without sharing class BatchMatchMaking implements Database.Batchable<sObject>{
    // Start Method
    public Database.queryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select id, rie__BatchProcessedForMatchMaking__c '+ 
                                        'from rie__Registration__c '+ 
                                        'where rie__BatchProcessedForMatchMaking__c = false');
    }
    //Execute Method
    public void execute(Database.BatchableContext BC, List<rie__Registration__c> regList){
        RegistrationTriggerHelper.runTrigger = false;
        Set<Id> regIds = new Set<Id>();
        if(regList != Null && regList.size() > 0){
            for(rie__Registration__c regobj : regList){
                regObj.rie__BatchProcessedForMatchMaking__c = true;
                regIds.add(regObj.Id);
            }
        }
        if(regIds != Null && regIds.size() > 0){
            Map<Id, rie__Registrant_Answer__c> regQuestionAnswersMap = new Map<Id, rie__Registrant_Answer__c>();
            
            //Get all the registrant Answers related to Registrants
            List<rie__Registrant_Answer__c> regAnswersList = new List<rie__Registrant_Answer__c>([Select id, rie__Question__c,
                                                                                                 rie__Answer__c, rie__Registration__c
                                                                                                 from rie__Registrant_Answer__c
                                                                                                 where rie__Registration__c IN: regIds]);
            
            if(regAnswersList != Null && regAnswersList.size() > 0){
                for(rie__Registrant_Answer__c regAnswers : regAnswersList){
                    regQuestionAnswersMap.put(regAnswers.rie__Question__c , regAnswers);
                }
            }
            
            if(regQuestionAnswersMap != Null && regQuestionAnswersMap.size() > 0){
                // This map contains Exhibitor as a key and Final score as value 
                Map<Id , Integer> exhibitorTotalScoreMap = new Map<Id , Integer>();
                
                //Get All Exhibitor Questionsand Score
                List<rie__Exhibitor_Question__c> exhibitorQuestionsList = new List<rie__Exhibitor_Question__c>([Select id, rie__Answer__c,
                                                                                                            rie__Exhibitor__c, rie__Question__c ,
                                                                                                            rie__Score__c
                                                                                                            from rie__Exhibitor_Question__c
                                                                                                            where rie__Question__c IN: regQuestionAnswersMap.keySet()]);
                
                List<rie__Registrant_Exhibitor__c> regExhibitorListToInsert = new List<rie__Registrant_Exhibitor__c>();
                Set<Id> registrantIds = new Set<Id>();
                if(exhibitorQuestionsList != Null && exhibitorQuestionsList.size() > 0){
                    for(rie__Exhibitor_Question__c exhibitorQuestions : exhibitorQuestionsList){
                        if(regQuestionAnswersMap.containsKey(exhibitorQuestions.rie__Question__c) && 
                           exhibitorQuestions.rie__Score__c != Null && 
                           exhibitorQuestions.rie__Answer__c == regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Answer__c){ 
                            Integer totalScoreList = 0;
                            if(exhibitorTotalScoreMap.containsKey(exhibitorQuestions.rie__Exhibitor__c)){
                                
                                totalScoreList = exhibitorTotalScoreMap.get(exhibitorQuestions.rie__Exhibitor__c);
                                
                                totalScoreList = totalScoreList + Integer.valueOf(exhibitorQuestions.rie__Score__c);
                            }else{
                                totalScoreList = Integer.valueOf(exhibitorQuestions.rie__Score__c); 
                            }
                       exhibitorTotalScoreMap.put(exhibitorQuestions.rie__Exhibitor__c , totalScoreList);
                            registrantIds.add(regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Registration__c);   
                        }
                     }
                     //Get all the existing registrant exhibior 
                    List<rie__Registrant_Exhibitor__c> regExihibitorList = new List<rie__Registrant_Exhibitor__c>([Select id, rie__Registration__c 
                                                                                                                   from rie__Registrant_Exhibitor__c 
                                                                                                                   where rie__Registration__c IN: registrantIds]);
                    Map<Id, rie__Registrant_Exhibitor__c> registrantExhibitorMap = new Map<Id, rie__Registrant_Exhibitor__c>();
                    if(regExihibitorList != Null && regExihibitorList.size() > 0){
                        for(rie__Registrant_Exhibitor__c regExhibitor : regExihibitorList){
                            registrantExhibitorMap.put(regExhibitor.rie__Registration__c, regExhibitor);
                        }
                    }
                               Integer finalScore = 0;             
                    for(rie__Exhibitor_Question__c exhibitorQuestions : exhibitorQuestionsList){
                            //Checking for exhibitor question
                            if(regQuestionAnswersMap.containsKey(exhibitorQuestions.rie__Question__c)){
                                //Checking for registrant answer
                                if(exhibitorQuestions.rie__Answer__c == regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Answer__c){
                                    if(exhibitorTotalScoreMap.containsKey(exhibitorQuestions.rie__Exhibitor__c) && 
                                       finalScore < exhibitorTotalScoreMap.get(exhibitorQuestions.rie__Exhibitor__c)){
                                           //Checking for any existing registrant exhibitor
                                           
                                           rie__Registrant_Exhibitor__c regExhibitor;
                                           if(registrantExhibitorMap.containsKey(regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Registration__c)){
                                             regExhibitor = new rie__Registrant_Exhibitor__c(Id = registrantExhibitorMap.get(regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Registration__c).Id); 
                                           }else{
                                             regExhibitor = new rie__Registrant_Exhibitor__c();
                                           }
                                           regExhibitor.rie__Registration__c =  regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Registration__c; // Error is coming from here

                                           finalScore = exhibitorTotalScoreMap.get(exhibitorQuestions.rie__Exhibitor__c);
                                           regExhibitor.rie__Exhibitor__c = exhibitorQuestions.rie__Exhibitor__c;
                                           regExhibitor.rie__Score__c = finalScore; 
                                           regExhibitorListToInsert.add(regExhibitor); 
                                       }
                                }    
                            }
                    }
                }
                                //upsert the new Registrant Exhibitors
                if(regExhibitorListToInsert != Null && regExhibitorListToInsert.size() > 0){
                    upsert regExhibitorListToInsert;
                }
            }
        }
        update regList;
    }
     //Finish Method for batch
    public void finish(Database.BatchableContext BC){
    }
 }
My test class is below:-
@istest
private class BatchMatchMakingTest {
    static testmethod void matchMakingMethod(){
        
        rie__Event__c evt = new rie__Event__c();
        evt.rie__Event_Time_Zone__c = '(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)';
        insert evt;
        
        rie__Registration__c attendee = new rie__Registration__c();
        attendee.rie__BatchProcessedForMatchMaking__c = false;
        attendee.rie__Event__c = evt.id;
        insert attendee;
        
        rie__Question__c qus = new rie__Question__c();
        qus.rie__Question__c  = 'Are you looking for a Solution?';
        insert qus;
        
        rie__Registrant_Answer__c regAns =  new rie__Registrant_Answer__c();
        regAns.rie__Question__c = qus.id;
        regAns.rie__Registration__c =  attendee.id;
        regAns.rie__Answer__c = 'yes';
        insert regAns;
        
         rie__Exhibitor__c ex = new rie__Exhibitor__c();
         
        insert ex;
        
        rie__Exhibitor_Question__c eq = new rie__Exhibitor_Question__c();
        eq.rie__Question__c = qus.id;
        eq.rie__Exhibitor__c = ex.id;
        eq.rie__Answer__c = 'yes';
        eq.rie__Score__c = 50;
        insert eq;
        
        rie__Registrant_Exhibitor__c rex = new rie__Registrant_Exhibitor__c();
        rex.rie__Exhibitor__c = ex.id;
        rex.rie__Registration__c = attendee.id;
        insert rex;
       
        
        Test.startTest();
        BatchMatchMaking cp = new BatchMatchMaking();
        Database.executeBatch(cp);
        Test.stopTest();
        
        rie__Registrant_Exhibitor__c ras = [select id,rie__Exhibitor__c,rie__Registration__c 
                                         from rie__Registrant_Exhibitor__c
                                         where id =: rex.id];
        system.assertEquals(rex.rie__Exhibitor__c,ras.rie__Exhibitor__c );
        system.assertEquals(rex.rie__Registration__c,ras.rie__Registration__c );
    }
}
how to solve this error?
Best Answer chosen by sumit d
AnudeepAnudeep (Salesforce Developers) 
Hi Sumit, 

Do you have a master detail relationship between rie__Registrant_Exhibitor__c and rie__Registration__c? If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the rie__Registration__c field after the detail record has been created. You can, however, select 'Allow Reparenting' in the master-detail relationship definition to make it work

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

Anudeep
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Sumit, 

Do you have a master detail relationship between rie__Registrant_Exhibitor__c and rie__Registration__c? If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the rie__Registration__c field after the detail record has been created. You can, however, select 'Allow Reparenting' in the master-detail relationship definition to make it work

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

Anudeep
 
This was selected as the best answer
sumit dsumit d
Hi Anudeep,
Yes, its master-detail relationship. Can you tell me how can I update the code so that this error could be removed?
AnudeepAnudeep (Salesforce Developers) 
Hi Sumit, 

Can you try selecting 'Allow Reparenting' option in the master-detail relationship definition without making any changes to code to see if it works? Thanks

https://help.salesforce.com/articleView?id=relationships_considerations.htm&language=en&r=https%3A%2F%2Fwww.google.com%2F&type=5

Anudeep
David Zhu 🔥David Zhu 🔥
The cause of the error is what Anudep mentioned.

In you code, it seems rie__Registration__c does not need to be changed if registrantExhibitorMap.containsKey(xxxx) is true. Variable rie__Registration__c only needs to be assiged a value when you instanitiate a new variable.
I would suggest you change your code a bit to fix the error.

}else{
          regExhibitor = new rie__Registrant_Exhibitor__c();
          regExhibitor.rie__Registration__c =  regQuestionAnswersMap.get(exhibitorQuestions.rie__Question__c).rie__Registration__c; 
}
sumit dsumit d
Hi Anudeep,
Will it affect data in my org if I select Allow reparenting checkbox?
AnudeepAnudeep (Salesforce Developers) 
Selecting 'Allow reparenting checkbox' will allow child records in master-detail relationships on custom objects to be reparented to different parent records. It will not affect data in your org