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
DannyTKDannyTK 

Field is not writable on test class

Good evening, 

need help getting the proper test coverage on trigger, and one issue i'm having with the test class is the field that i'm actually trying to test.  the trigger is to populate an Event record custom field ('Fortune_1000_Ranking__c') from a field pulled from the related Contact ('Fortune_1000_2013_Ranking__c'), which is actually a forrmula field pulled from the same field on the Account record (master-detail).  The trigger works fine:

trigger UpdateEventFortune1000 on Event (before insert, before update) {
Set<Id>CIds = new Set<Id>();for (Event E:trigger.new)
{String wId = e.WhoId; If(wID!=null && wId.startsWith('003')&& !cIds.contains(e.WhoId))
{cIds.add(e.WhoId);}}
    
    List<Contact> EventC = [Select Id, Fortune_1000_2013_Ranking__c from Contact where
                            Id in :cIds];
    
    Map<Id, Contact> CMap = new Map<Id, Contact>(); for (Contact c: EventC)
        
    {CMap.put(c.Id,c);}
    
    for (Event e:trigger.new)
    {String wId = e.WhoId; if (wId!=null && wId.startswith('003'))
    {Contact thisC = cMap.get(e.WhoId);
    if(thisC!=null)
    {e.Fortune_1000_Ranking__c = thisC.Fortune_1000_2013_Ranking__c;}}}
 
}

---------------------------------------------------------
The test class is throwing an error on the Fortune_1000_2013_Ranking__c line below due to the field is unwritable (value is pulled from the Account record)...any help would be appreciated!!

@isTest
private class UpdateEventFortune1000testclass {
    
static testMethod void UpdateEventFortune1000 () {
List<Contact>Contact = new List<Contact> {new Contact (
    firstname = 'test',
    lastname = 'testtest',
    Fortune_1000_2013_Ranking__c = 1,
    phone = '(555) 555-5555') }; 
    insert Contact;
    
List<Event> e = new List<Event> {new Event(
    WhoID = Contact[0].id, 
    subject = 'test trigger',
    Type = 'Meeting')};
    
    insert e;
    
List<Event> Eventstoupdate = New List<event>{[select id from event where id in :e]};
    for (event eok:eventstoupdate)
    eok.Fortune_1000_Ranking__c=null;
    
    update eventstoupdate;
    
Contact[] c = [select id,Fortune_1000_2013_Ranking__c from Contact where id in :Contact];
    System.assertEquals(1,Contact[0].Fortune_1000_2013_Ranking__c);
    
}
}
Sagar PareekSagar Pareek
Try creating a account first in you test class before creating a contact . When you create a contact try passing account id to the contact . As the formula field is dependent on the account it will get auto populated . The error you are getting because formula fields are not writeable so do not assign any value to that field in your code. 
Arunkumar RArunkumar R
Hi,

Is this field "Fortune_1000_2013_Ranking__c" formula field right? If yes, you can't assign the value to this field. Those formula field value will get automatically after insert a record. 
Yogesh KulkarniYogesh Kulkarni
Hi Danny,

In trigger you are not assigning value to Fortune_1000_2013_Ranking__c  but just fetching it thats why you are not getting the error. If this is a formula field you are not supposed to populate the field value, it will be populated automatically.

Thanks,
Yogesh
DannyTKDannyTK
Thanks everyone,

I'm not a developer so probably will need little more guidance.  I replaced the Contact list with Account list on the test class, since that's where the field originates from and changed the condition of the test class to assoicated the whatID on the event, but doesn't seem to give me enough coverage (58%).  how would i insert a line and test on the WhoID (contact) for that field?..
I appreciate everyone's help thus far

-d