0

My compiler says that this field is not writeable:

Field is not writeable: CAM_Members__c.Affiliate_State__c

I have this class

static testMethod void AffliateTest(){

    SS_Data__c ns = new SS_Data__c();
    SS.Name = 'test SS';
    insert ss;


   Account a = new Account();
   a.name = 'Test Account Affliate';
   a.BillingState = 'WA';
   insert a;

   contact c = new contact();
   c.lastname = 'Test';
   insert c;


   Relationship__c rc  = new Relationship__c();
   rc.Account__c = a.id;
   rc.Contact__c = c.id;
   rc.Start_Date__c = system.today();

   insert rc;


   CAM_Members__c mem = new CAM_Members__c();
   mem.Name = 'test VM';
   mem.SS_Data__c = ss.Id;
   mem.Member_Name__c = '0034000001TTfQW';
   mem.Member_ID__c = '9999';
   mem.Relationship__c = rc.Id;
   insert mem;

    CAM_Members__c insertedRecord = [
    SELECT Affiliate_State__c FROM CAM_Members__c WHERE Id = :mem.Id
    ];
   system.assertEquals(a.BillingState, insertedRecord.Affiliate_State__c, 'WA');


}

The field has this formula:

Relationship__r.Account__r.BillingState

What am I doing wrong? How can I fix it? My Problem now is it has no coverage try the above Affliate State = WA wherein it should be covered but not working

  • Tests are meant to cover other classes. Not formulas, nor the test itself. There is nothing to cover here... – Adrian Larson Jun 23 '17 at 14:25
5

You cannot create or update the value for formula fields. The only way to control their value is to set up the related data in a way that it calculates what you want. In this case, you would need to set the parent Relationship__c and then insert the record:

mem.Relationship__c = rc.Id;
insert mem;

CAM_Members insertedRecord = [
    SELECT Affiliate_State__c FROM CAM_Members__c WHERE Id = :mem.Id
];
system.assertEquals(a1.BillingState, insertedRecord.Affiliate_State__c, '<message_here>');

That said, you shouldn't test the output of your formulas with a unit test. You should use regression testing to verify their behavior. Unit tests on configuration aspects which can be easily changed are very brittle and will harm your org health in the long run.

| improve this answer | |
  • I modified the code above but it doesn't cover something like this for (CAM_Members__c cam: trigger.new){ IF( cam.Current_Program_Year__c == '2015-2016'){ IF(cam.Affiliate_State__c == 'WA'){ can.Product__c = CAM4354534[0].id; can.Monthly_Amount__c=S5435Price[0].Unitprice; cam.Annual_Commitment__c = A54354Price[0].Unitprice * 11; – Torrific Jun 23 '17 at 14:24
  • 1
    That trigger logic is not going to work. You are going to have to retrieve the values in a trigger context via other means. See also: Parent and children relationships accessible via trigger? – Adrian Larson Jun 23 '17 at 14:25
  • oh great, thank you so much! apologies still part of learning. – Torrific Jun 23 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.