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
riojohnriojohn 

Hi, I am trying to write test class for trigger not yet covered any lines.

trigger Trg_IntegrityIndex on Project__c (before update) {
    
    for(Project__c P : Trigger.New){
        String PID = p.Id;
        Project__c P1=[select Id,Name,Status__r.Name, Value__c, ContractValue__c, Contract__r.Name, Country__c, Workflow__c, CreatedDate,Scope__c  from Project__c where Id=:PID];
        
        string Status =P1.Status__r.Name;
        String sContract =P1.Contract__r.Name;
        String sContractValue =String.Valueof(P.ContractValue__c);
        String sScope =String.valueof(P.Scope__c);
        String sIndustry=P.Industry__c;
        String sName=P.Name; //for Project name
        String sScopeReplace;
        Integer iScope =0;
        
        Boolean bBad=false;
        Boolean bFair=false;
        String sErrorComments='';
  List<News__c> News= [select Id,Date__c,Name from News__c where Project__c=:PID];
        if (News.Size() >0){
              for(News__c N :News){
              //Validation 8: News:News-Study    Should be within 90 days from today   except design build in contract type             
                if(Status=='Study' &&  !(sContract=='Design Build (DB)')){
                  if(N.Date__c > date.today() +90 && N.Date__c < date.today() +97){
                    bFair = True; 
                    sErrorComments +='Status-Study Should be within 90 days from today for News Record-' + N.Name + '\r\n'; 
                  }else if (N.Date__c > date.today() +97){
                    bBad= True; 
                    sErrorComments +='Status Study as exceeded 97 days from today for News Record-' + N.Name + '\r\n';                                         
                  } 
               } 
i wrote test class below i dont know what is wrong in this can anyone have solutions for this . Kindly help me.

@isTest 
Public class testIntegrity{
    public static void init()
    {
        List <Project__c> proj = New List<Project__c>();
        Public Static String PID = p.Id;
        Project__c sproj=[select Id,Name,Status__r.Name, Value__c, ContractValue__c, Contract__r.Name, Country__c, Workflow__c, CreatedDate,Scope__c  from Project__c where Id=:PID];
        Public Static String Status = sproj.Status__r.Name;
        Public Static String sContract =sproj.Contract__r.Name;
        Public Static String sContractValue =String.Valueof(sproj.ContractValue__c);
        
    }
    
    
@isTest Static void testinsertproject()
    {
        
        Project__c sproj = New Project__c();
        sproj.Parent__c = 'a042000000IPjwg';
        sproj.Name ='Abu Dhabi Commercial Bank - Office Building';
        sproj.Type__c='a0I200000064HgY';
        sproj.Type_MCI__c='a0I200000064HgY';
        sproj.Location__c ='a0I20000006hEdO';
        sproj.Workflow__c = 'Recalled';
        sproj.Status__r.Name = 'Study';
        sproj.Inactive__c=true;
        sproj.Value__c=400;
        sproj.Integrity_Errors__c='Scope Should be more than 5 Pointers for the Status :Design';
        sproj.ContractValue__c=40;
        sproj.Contract__r.Name='Design Build (DB)';
        //sproj.Contract_Type_MCI__c='Build';
        sproj.Scope__c='Abu Dhabi Commercial Bank is constructing an office building in Abu Dhabi.\n\nScope of the project includes:\n*Office building (12-storey)\n*Associated facilities';
        sproj.Brownfield__c=false;
        sproj.Offshore__c=false;
        insert sproj;
        
        
        News__c sobj = New News__c();
        sobj.Inactive__c = false;
        sobj.News__c = 'fsfsfdsgsddsf';
        sobj.Date__c = Date.valueOf('20-10-2017');
        sobj.Source_Type__c ='Primary';
        sobj.Number_of_Updates__c ='2';
        sobj.Project__c = sobj.Id;
        insert sobj;
        
        
        if(sproj.Status__c =='Study' && !(sproj.Contract__c=='Design Build (DB)'))
        {
            String spro = String.valueof(sproj.Id);
            if(sobj.Date__c>date.today() +90 && sobj.Date__c<date.today() +97 )
            {
                
            }
            else if(sobj.Date__c > date.today()+97)
            {
               //do nothing 
            }
                
        }
    
        }
    
}

Thanks 
Ankit GargAnkit Garg
Hi, 

There are many issues with this code:

1. Trigger must be on after insert event.
2. Trigger must be bulkified. Query has been written inside the for loop.
3. There may be other issues. Need to get more details on that.

Thanks.
riojohnriojohn
Hi Ankit,
I am having objects like Project, News. Projects having many related objects like news, role all are interconnected. Project having status field, and news object having date field. While project- status =study , news object date value should be from today within 90 days. For this i wrote trigger. 

Thanks.
Peter_sfdcPeter_sfdc
Setting aside the flaws in the code mentioned above...

Have you looked at the debug logs for when you run the tests? Are the tests successful? Do they report code coverage of any amount? 

One obvious thing that looks out of place (apart from what has already been mentioned) is this bit: 
sobj.Project__c = sobj.Id;

...should be...

sobj.Project__c = sproj.Id;
You are attempting to assign the Id field from this object to the Project__c field. 

There is also this bit: 
sproj.Parent__c = 'a042000000IPjwg';
sproj.Name ='Abu Dhabi Commercial Bank - Office Building';
sproj.Type__c='a0I200000064HgY';
sproj.Type_MCI__c='a0I200000064HgY';
sproj.Location__c ='a0I20000006hEdO';
Assuming you've gotten IDs from actual records in your org, those IDs will not be available in the test framework, unless you've explicitly annotated your test class.

And you shouldn't do that. 

Because even if they work in this org, when you deploy to production, they won't exist there. 

For testing you should create all your data, and you can write some code that will do that once in a given test class run with the `@testSetup` annotation. First create some static variables in your class to store those IDs: 
 
static Id parentProjectId;
static Id typeId;
static Id typeMCIId;
static Id locationID;
Now go write a test data factory class. In it, you'll have a bunch of methods that generate different types of data that you will want to run test cases around, or use as setup data, like these ID values. I'll pretend I've done that and called it `ProjectDataFactory`. 

Now I use that to populate these: 
@testSetup
static void setupData(){

  ...write code here to setup data...
  parentProjectId = ProjectDataFactory.createProject();
  ...

}
You can then set static variables with IDs that are ephemeral and last only for the length of your tests in this class. 

One other thing...you are not using any of the System.assert... methods to test for correct values. But that could be because nothing else is working...

If you have not done so already, I'd strongly recommend you go through the Trailhead module on testing Apex: 

https://trailhead.salesforce.com/modules/apex_testing

If you've done enough Apex modules, you should also check out the superbadge, which will force you to work your way through some more complex testing requirements.