+ Start a Discussion

Test method help

I'm new to Apex/Visforce and I'm having trouble wrapping my brain around test methods and getting them to work. I currently have a method that is giving me the following error and I can't figure out why:


System.SObjectException: Field is not writeable: Merchant_Analysis__c.Opportunity__c

 It refers to the bolded line below:


public PageReference save()
analysis.Opportunity__c = oppID;
analysis.recordtypeid = recType;
insert analysis;

PageReference analysisPage = new PageReference('/' + analysis.id);

return analysisPage;

static testMethod void testSave()
analysisController controller = new analysisController();
String recType = '012400000005QI0AAM';

Opportunity o = new Opportunity(name='test opp',stagename='test stage',closedate=System.today());
insert o;

Merchant_Analysis__c a = new Merchant_Analysis__c();
a.Opportunity__c = o.id;
insert a;

PageReference testPage = new PageReference('/' + a.id);

controller.oppID = o.id;
controller.recType = recType;

controller.analysis = a;

String ref0 = testPage.getUrl();
String ref1 = controller.save().getUrl();



If someone could help troubleshoot this issue with me that would be awesome.









Message Edited by jbroquist on 02-18-2009 02:10 PM
I just had something similar to this when using a controller to maintain a fake http session. Are you setting this same field somewhere earlier in your code? Or on a previous page perhaps?

oppID is a public class level variable that simply holds the ID of the opportunity that we're attaching this custom object too. I use the following to set oppID in the class constructor:






No that is the only place I reference the Opportunity__c relationship field within this VisualForce.
And you've checked obvious things like field level security and read-only permissions on the object fields?