For Apex testing, I'm trying to create an object that is the same as the one created with a SOQL statement (see following method). The method retrieves the Quote_To_Lease__c
object from Salesforce and gets a Related List (Referral_Payment_Transaction__r
). When I try to create this object and related list (see following test code) in Salesforce I get a syntax error "Field is Not Writeable: Referral_Payment_Transactions__r
".
Is there a way to create a fake object which has a related list that looks like the real one created by a SOQL select?
Method
public static void getQtlWithRpTrans(){
Quote_to_Lease__c qtl =
[SELECT Id, Name
,Opportunity__c, Opportunity__r.Name, Opportunity__r.Referred_By_Lookup__c, Opportunity__r.Property_Name__c
, (select id, Name, FirstName__c, LastName__c, Status__c, Referral_Type__c
, Event_Name__c, Customer_Name__c, Email__c, Cost_Center__c, ExtId_Vendor_Account__c, Unit__c
, Date_Forwarded__c, Date_Fwd_Completed__c
,Contact__c, Lead__c, Opportunity__c, Referral_Payment_Account__c
from Referral_Payment_Transactions__r)
FROM Quote_to_Lease__c
where id = 'a0mQ0000003sM6wIAE'];
Referral_Payment_Transaction__c rpTrans = qtl.Referral_Payment_Transactions__r[0];
System.debug('Qtl=' + qtl.id
+ ', rpTrans=' + rpTrans.Id
+ ', rpTrans.DateForwarded=' + rpTrans.Date_Forwarded__c
);
}
Test code
// Create fake QTL object
Quote_To_Lease__c qtl = new Quote_to_lease__c(
id = RpTransQtl.fakeQtlId
, name='L12345'
);
// Create fake RpTrans object
Referral_Payment_Transaction__c rpTrans =
new Referral_Payment_Transaction__c(
Id=RpTransQtl.fakeRpTransId
);
// Create a list to place in the related field
List<Referral_Payment_Transaction__c> myRpTransList =
new List<Referral_Payment_Transaction__c>();
myRpTransList.add(rpTrans);
// Get syntax error on following line
// Field is not writeable: Referral_Payment_Transactions__r
qtl.Referral_Payment_Transactions__r = myRpTransList;