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
jbroquistjbroquist 

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);
analysisPage.setRedirect(true);

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;
a.recordtypeid=recType;
insert a;

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

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

controller.analysis = a;


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

System.assertEquals(ref0,ref1);
}

 

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

 

 

 

Thanks,

Jonathan

 

 

 

Message Edited by jbroquist on 02-18-2009 02:10 PM
wesnoltewesnolte
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?
jbroquistjbroquist

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:

 

System.currentPageReference().getParameters().get('id');

 

 

 

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