+ Start a Discussion

How to give values to readonly fields in test class..

Hi Everyone,

Iam writing a test class where I need to create a quote and insert it.
Now we have two read-only fields Opportunity and Account.

How can we assign values to these two fields?

Public Class QuoteDescription_Test{
    Static testMethod void test1(){
       Account acc1 = new Account(name = 'Account1');
       insert acc1;
       Opportunity opp1 = new Opportunity ();
       opp1.Name = 'Opportunity1';
       opp1.StageName = 'Prospecting';
       opp1.CloseDate = Date.newInstance(2016, 2, 6);
       insert opp1;
       Quote quo1 = new Quote();
       quo1.Name = 'quote1';
       Account acc = [Select id, name from Account];
       quo1.Account = acc;
       quo1.Opportunity = 'Opportunity1';
       quo1.Status = 'Accepted';
       insert quo1;
This gives an error

Error: Compile Error: Field is not writeable: Account at line 18 column 8
Best Answer chosen by Vempally
Vivek DVivek D
Hi Vempally,
Opportunity and Account are reference field on Quote, you need to pass id of both to actually link them. They are actually with name AccountId and OpportunityId respectively. The correction will be :-
       Quote quo1 = new Quote();
       quo1.Name = 'quote1';
       /*  Add the id of created account and opportunity */
       quo1.AccountId = acc1.Id;   
       quo1.OpportunityId = opp1.Id;
       quo1.Status = 'Accepted';
       insert quo1;