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
pvandepvande 

Help with Apex Class Coverage (please)

I am new to Apex trigger/class coding.  I was able to create a simple trigger that works, but I am having trouble with code coverage and can't roll it to the production environment.  I would appreciate any help with Apex Class coding.  (or could you point me toward a tutorial?) Thank you in advance.

My trigger is as follows:

trigger WorkstreamStamp on Project__c (before insert) {
    For(Project__c p : Trigger.New)
    {
         If(p.Workstream__c == null)
         {
           List<Account> incomingprojinfo = [Select Workstream__c FROM  Account WHERE Id =:p.Sponsor_Account_Ultimate_Parent__c LIMIT 1];
                               {
               p.Workstream__c = incomingprojinfo[0].Workstream__c;
           }                                                      
          
         }
       
    }                           
}
Phil WPhil W
Hi pvande,

To have "code coverage" means writing unit tests that exercise the production code. The code that is "exercised" is "covered" by the tests, whilst the code that isn't run by the tests is outside this coverage.

Unit tests are classes in your code base with the @IsTest annotation against them containing methods also marked as @IsTest (or using the deprecated "testMethod" keyword).

In this specific case, to exercise your trigger you need to write a test that causes the trigger to be invoked. That means, here, that your test must create and insert/upsert new Project__c instances. The tests do this by creating various Project__c instances with Workstream__c set to a value and and/or set as null. In both cases the tests should assert that the Workstream__c field is set appropriately after the insert/upsert call.

To get to grips with unit testing I recommend you start at https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests and go from there.

Phil
Ricky_ThedaRicky_Theda
You can try to build some test class like this:
@isTest
private class TestTriggerWorkstreamStamp {
    static testmethod void sendemail(){
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        Project__c projects = new Project__c();
        projects.Name = 'Test Project';
        projects.Sponsor_Account_Ultimate_Parent__c = acc.Id;
        insert projects;
    }
    
}

It should cover all your lines code.
pvandepvande
Thank  you for your help, Ricky.  Sponsor_Account_Ultimate_Parent__c is a formula field, so I am getting the following error:  Error: Compile Error: Field is not writeable: Project__c.Sponsor_Account_Ultimate_Parent__c at line 9 column 18

Is there any work aroud for this situation?
Thank you again.
Ricky_ThedaRicky_Theda
Just insert a field that used on your formula field. For example:
Formula field name Total_Amount__c : Amount * 2
Then you just need to insert Amount field on your test class:
Amount = 50000