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
Dilyan DimitrovDilyan Dimitrov 

Apex Account to Contact set email

Hello,

I'm trying to execute the code in the following test class:
static testMethod void testLeadStatusExistingDeal() {
    Test.startTest();

    Lead record = new Lead(LastName = 'Dimitrov', Company ='emp1', Monthly_Volume__c= '1234', Phone='1423542452', Website='www.google.com');
    insert record;
    System.debug('record ' + record);

    Contact contact = new Contact(Company_Name__c = record.id, email = 'dylan@dylan.com');
    Contact contact1 = new Contact(Company_Name__c = record.id, email = 'dilyan@dilyan.com');
    insert contact;
    insert contact1;

    Account account = new Account(name = 'acc1');
    insert account;
    Merchant_To_Contact__c merchantToAccount = new Merchant_To_Contact__c(Merchant_Name__c = account.id, email__c = 'dylan@dylan.com');
    Merchant_To_Contact__c merchantToAccount1 = new Merchant_To_Contact__c(Merchant_Name__c = account.id, email__c = 'dilyan@dilyan.com'); 

    insert merchantToAccount;
    insert merchantToAccount1;

    ApexPages.StandardController c = new ApexPages.StandardController(record);
    LeadToMerchantController ltmc = new LeadToMerchantController(c);
    String leadStatusExistingDeal = ltmc.leadStatusExistingDeal(record);
    System.debug('leadStatusExistingDeal ' + leadStatusExistingDeal);
    Test.stopTest();
}
Instead I get the following error Field is not writeable: Merchant_To_Contact__c.Email__c

Could you please advise what am I doing wrong and how to execute this test class without such error?

Regards,

Dilyan
 
srlawr uksrlawr uk
my reflex would be to look into the configuration of the Email__c field in Merchant_To_Contact__c

Is it Read-only?

Or perhaps a Formula field (which you cannot write) - this could make sense as a formula can be used to lookup values in parent objects, so in this case it could be to a field on Account?

Have a look at the field (maybe stick a screen shot of the config if you don't understand it) and you'll see!
Dilyan DimitrovDilyan Dimitrov
Hi,

Indeed the Email__c field in Merchant_To_Contact__c is a formula field.

User-added image
It is also a custom, filterable and nilable.

Could you please advise what I need to do next in order to avoid the following error 
Field is not writeable: Merchant_To_Contact__c.Email__c
I'd like to ask you to include some code example as well.

Regards,

Dilyan
 
srlawr uksrlawr uk
yes, sure!

You can't write the contents of a formula field... it's like trying to "set" the answer to 2+2 to your own value, it's not possible, 2+2 is just 4, it just is.

To remove this error from your code (here is the code example!) you need to not include Email in your merchant_to_contact__c records:
 
Merchant_To_Contact__c merchantToAccount = new Merchant_To_Contact__c(Merchant_Name__c = account.id);
Merchant_To_Contact__c merchantToAccount1 = new Merchant_To_Contact__c(Merchant_Name__c = account.id);
(lines 15 and 16)
Dilyan DimitrovDilyan Dimitrov
Hi,

Thanks for the advice but I've tried to execute the code without
Merchant_To_Contact__c merchantToAccount = new Merchant_To_Contact__c(Merchant_Name__c = account.id);
Merchant_To_Contact__c merchantToAccount1 = new Merchant_To_Contact__c(Merchant_Name__c = account.id);
and I got some strange error which I do not know how to solve.

Please have a look at the screenshot of the execution log.

User-added image

Could you please advise how to avoid such error and run my test method?

Regards,

Dilyan