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
sk3501.3915272120779841E12sk3501.3915272120779841E12 

formula field

Hi,
I have created an custom object product and it has so many field but the problem is that when i am trying to create a test class in which i am inserting date then it is giving error that the field is not writable.
     
     I have used formula in InHometrgtdateminus13days__c = In_Home_Target_Date__c-13
     Products__c ps = new Products__c();
      ps.ms_123postcards__Account__c = comp.id;
      ps.Fulfillment_Status__c = 'Currently in Design';
      ps.In_Home_Target_Date__c = system.today().adddays(13);
      ps.To_Finalize_Design__c = False;
      Ps.Do_not_repeat_finalize_design_task__c = FALSE;
      ps.Receive_New_Agreement__c = system.today().adddays(-1);
      ps.InHometrgtdateminus13days__c = system.today();
      insert ps;

     This error is giving when i am trying to insert ps.InHometrgtdateminus13days__c = system.today();

Can anyone help me to resolve this issue?
sunil_kumarsunil_kumar
Hi,

Please find below reasons for this error:
  • If InHometrgtdateminus13days__c field is formula field, then there is no need to assigned value to it. Assign values to fields which is used in order to define formula for this field.
  • This field will be read only for the logged in user or user with which you are creating records in test class.
sk3501.3915272120779841E12sk3501.3915272120779841E12
Ok but the thing is that i am running some DML operation in the class and it is not covering when i am leving this field.

date finalizedisgntaskdate = system.today();
prodserv = [select ms_123postcards__Account__c, id, Fulfillment_Status__c from Products__c where InHometrgtdateminus13days__c = :finalizedisgntaskdate];
sk3501.3915272120779841E12sk3501.3915272120779841E12
Hi Sunil, have you find any solution for this? If you got anything then please help me.
Sunil02KumarSunil02Kumar
Hi,

Fectch the formula field in your query. 
date finalizedisgntaskdate = system.today();
prodserv = [select ms_123postcards__Account__c, id,InHometrgtdateminus13days__c, Fulfillment_Status__c from Products__c where InHometrgtdateminus13days__c = :finalizedisgntaskdate];

and then try.

If this won't help, can you share abstract form of your class and test class to understand real issue.
sk3501.3915272120779841E12sk3501.3915272120779841E12
Hi Sunil,

The problem is that i can not change query that is in the class.

Without making any changes in the class i want to increase code coverage.