0

UPDATE: it was typo when I copy and paste the wrong error here is the error:

Compile Error: Field is not writeable

I have a custom object and one of the field I have is Next Date which is a formula field. I'm trying to update that field through Apex Class

 Integer interval = 0;

 if(interval__c != null)
 {
   interval = integer.valueOf(interval__c);
 }

 Date d = last_comp_date__c.addDays(integer.valueOf(interval));
 next_date__c = d;

Error:

Error: Compile Error: Invalid field next_date__c

1 Answer 1

0

Formula fields are read only and the value cannot be modified.

You error however is not due to trying to update a formula field it is saying the field does not exist. Check the API name, also there is no object associated with your field in the code you posted.

You code should look like this for example:

Account a = [Select Name From Account Limit 1];
a.Name = 'Test Name';

So next_dat__c would not even compile and should say

Variable does not exist: next_date__c

instead of the error you posted

6
  • oops my bad copy the wrong error message and i have updated my question, so there is no way to update the formula field from apex class?
    – Nick
    Commented Oct 7, 2015 at 0:53
  • A formula is by default the result of a formula, not the actual assignment of a value via apex, ui, data loader, or anything else
    – Eric
    Commented Oct 7, 2015 at 0:55
  • hmm.... I'm not sure how to implement the above code in the formula, I will post another question with more specific to formula
    – Nick
    Commented Oct 7, 2015 at 1:01
  • @AbuHamzah - If you are talking about my example, you don't it was to illustrate how you reference a field and the error you should get if you did it the way you did. Please mark the answer as accepted if it answered your question
    – Eric
    Commented Oct 7, 2015 at 1:06
  • No i was not talking about your example I was talking about the apex code I have posted, however I have create a new question please have a look if you can salesforce.stackexchange.com/questions/94919/…
    – Nick
    Commented Oct 7, 2015 at 1:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .