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
SFDC BUGSFDC BUG 

Code coverage 75% to 100%

Hi Guys

Need to improve my code coverage of my trigger 75% to 100%
trigger DocumentAttached on Purchase_Order__c (before insert , before update) 
{ 
    for(Purchase_Order__c tsk : Trigger.new){ 
        Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].id]; 
        if(attList.size() > 0) { 
            tsk.PO_Uploaded__c =true; 
        }
    }
}

 
Abhi_TripathiAbhi_Tripathi
First of all your code is not written in best practice, you have a SOQL query insdie loop/
To write a test class here is the link using which you  can write the test class

https://cloudyabhi.blogspot.com/2013/10/salesforce-test-class-basics.html 
Amit Chaudhary 8Amit Chaudhary 8
Try to update your trigger like below
trigger DocumentAttached on Purchase_Order__c (before insert , before update) 
{ 
	Set<Id> setPOId = new Set<Id>();
    for(Purchase_Order__c tsk : Trigger.new){ 
		setPOId.add(tsk.id);
	}
	List<Attachment> lstAttchment = [ select id, name, ParentId from Attachment where ParentId in :setPOId ];
	Map<Id,Attachment> mapPOwiseAttch = new Map<Id,Attachment>();
	for(Attachment att : lstAttchment){
		mapPOwiseAttch.put(att.ParentId,att);
	}
	
    for(Purchase_Order__c tsk : Trigger.new){ 
        if(mapPOwiseAttch.containsKey(tsk.id)) { 
            tsk.PO_Uploaded__c =true; 
        }
    }
}
Your test class should be like below
@isTest 
public class TriggerTestClass 
{
    static testMethod void testMethod1() 
	{
		Purchase_Order__c PO = new Purchase_Order__c();
		// Add all required field
		insert PO;
		
	}
}

Let us know if this will help you