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
hassabhassab 

Master-Detail fields not writable?

Hello,

 

I have written a controller to prepopulate a visualforce page.  The controller works fine in sandbox, and when I run through the steps is prepopulates perfectly in the browser but when I try to run tests it tells me the field is not writable.  I seem only to have this problem with lookup fields, when I remove those it compiles, tests and deploys fine.  I tried also prepopulate those using the url but visualforce pages have very odd field ids and the url seems not to understand them.  Does anyone have any suggestions?  Here' s my controller:

 

public class addContactAffiliation {
 Public npe5__Affiliation__c affil;
 Public Contact contact;
 Public string AccountID = System.currentPagereference().getParameters().get('AccountID');
 Public string ContactID = System.currentPagereference().getParameters().get('ContactID'); 
 
    public npe5__Affiliation__c getAffil() {
      if(affil == null) affil = new npe5__Affiliation__c();
      contact = [select Name, LastName, id, npe01__WorkEmail__c,
                      npe01__Workphone__c, npe01__AlternateEmail__c,mobilephone,
                      otherphone, work_fax__c, mailingcity, mailingcountry,
                  mailingpostalcode, mailingstate, mailingstreet from contact where Id =:      

    contactId];
           
          affil.npe5__Contact__c = contact.name;
          affil.email__c = contact.npe01__WorkEmail__c;
          affil.office_phone__c = contact.npe01__Workphone__c;
          affil.email_2__c = contact.npe01__AlternateEmail__c;
          affil.cell_phone__c = contact.mobilephone;
          affil.alt_phone__c = contact.otherphone;
          affil.street__c = contact.mailingstreet;
          affil.city__c = contact.mailingcity;
          affil.state__c = contact.mailingstate;
          affil.zip__c = contact.mailingpostalcode;
          affil.country__c = contact.mailingcountry;
          affil.fax__c = contact.work_fax__c;
             
      return affil;
   }

 

Really appreciate your help - it's so frustrating to get to this point and not to be able to prepopulate the most important field!

Best Answer chosen by Admin (Salesforce Developers) 
hassabhassab

I was, but I think I tried that after I was getting the error and forgot to change it back before posting.  Turns out the issue was the test was set up to pull getAffili() when setting to an existing affiliation record.  This caused the test to try to update the Master-Detail record, which turns out you can't do.  I adjusted the test so that running getAffil() creates a new record and sets the Master-Detail, and the test passed.  

 

Thanks for all your responses, I think this one was just me not taking into account the constraints of the Master-Detail field.

All Answers

SurekaSureka

Hi,

 

Can you elaborate more - In which line is it failing?, which is the lookup field?

 

Thanks

hassabhassab

Sorry, the contact field is the lookup field.  It fails at line 15.  Message:  System.SObjectException: Field is not writeable: npe5__Affiliation__c.npe5__Contact__c

hassabhassab

Also, need to clarify, looks like these are Master Detail fields.  Is that the problem?  If so, how can I get around it?

hassabhassab

OK, I was able to test by cloning the object and changing the Master-Detail data type to Lookup and the test passed.  So it seems the data type is the issue.  The object is actually a managed object so I'm not able to change the datatype and I'd hate to recreate the whole thing in production just because of this issue.  Is there any way around it?

Jeremy.NottinghJeremy.Nottingh

It seems like you're setting a lookup field to a String value:

          affil.npe5__Contact__c = contact.name;

Shouldn't this be something like:

          affil.npe5__Contact__c = contact.Id;

If that's not the problem, maybe check FLS on that master/detail field. You can't create a Detail object if you can't specify what its Master is.

 

Jeremy

hassabhassab

I was, but I think I tried that after I was getting the error and forgot to change it back before posting.  Turns out the issue was the test was set up to pull getAffili() when setting to an existing affiliation record.  This caused the test to try to update the Master-Detail record, which turns out you can't do.  I adjusted the test so that running getAffil() creates a new record and sets the Master-Detail, and the test passed.  

 

Thanks for all your responses, I think this one was just me not taking into account the constraints of the Master-Detail field.

This was selected as the best answer