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
PallavPallav 

How to get the code coverage of the Apex code lines where addError statement?

Hi All,

I am facing a issue with the trigger that I have developed, where validation takes place and shows the user with the custom error message. When I run the test class for the code coverage code. I throws the custom field validation messages that I have specified on the code of the trigger.

If any one has nay knowledge in these regard, I would request you to provide me with the same.

Here is the code.

trigger ValidateAccOnProject_trigg on SSI_Project__c (before insert, before update)
{
    try
    {
        for(SSI_Project__c sp : trigger.new)//loops through the Projets that have been updated or inserted
        {
            if(sp.System__c =='Majic')// checkes wheather the System field value is 'Majic'
            {
                //Retrives the value of the Site Id and Site In Buiness fields.
                List<Account> acc = new List<Account>([Select Site_ID__c, Site_In_Business__c from Account where Id =: sp.Account__c]);
                if(acc.get(0).Site_ID__c != null)//Checks if Site Id Exist on the related Account
                {
                    if(acc.get(0).Site_In_Business__c == true)//Chceks if Site In Business field is cheked or not.
                    {
                        sp.Site_ID__c = acc.get(0).Site_ID__c;// assigns the site id of the related account to the Site Id of the Project
                        sp.Company_ID__c = null;
                    }
                    else sp.addError(' Invalid Account :  Must select Active Account');
                }
                else sp.addError(' Invalid Account :  Must select Account with Site ID');
            }
            if(sp.System__c =='eSynergy')// checks if the System field value is 'eSynergy'
            {
                //Retrives the value of the Business Status and Customer Id field from Account
                List<Account> acc1 = new List<Account>([Select Business_Status__c, Customer_ID__c from Account where Id =: sp.Account__c]);
                if(acc1.get(0).Customer_ID__c != null)// cehcks if Customer Id exist on the related account
                {
                    if(acc1.get(0).Business_Status__c =='Active')// Checks if the value of the Business Status field is set to 'Active'.
                    {
                        sp.Company_ID__c = acc1.get(0).Customer_ID__c;// Assigns the value of the Customer id from Account to the Company Id field on Project.
                        sp.Site_ID__c = null;
                    }
                    else sp.addError('  Invalid Account:  Must select Active Account');
                }
                else sp.addError(' Invalid Account :  Must select Account with Customer ID');
            }
            if(sp.Billable__c == false)// Checks if Project's Billable field is checked or not.
            {
                if(sp.Parent_Project__c != null)// Checks if the project that we are creating or updating has a Parent project associated to it.
                {
                    // Retrives the Currency type of the Parent Project associated to a Sub Project.
                    List<SSI_Project__c> ssip = new List<SSI_Project__c>([Select CurrencyIsoCode from SSI_Project__c where Id =: sp.Parent_Project__c]);
                   
                    if(sp.CurrencyIsoCode != ssip.get(0).CurrencyIsoCode)// Checks if the Currency type of the the Sub Project Matches with the Currency type of the Sub Project
                        sp.addError('This project must be in ' + ssip.get(0).CurrencyIsoCode +' Currency');
                }
            }
        }
    }
    catch(Exception e)
    {}
}