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
Silpi roy 16Silpi roy 16 

Help in increasing the code coverage for Trigger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
trigger Product_StatusChangeAlert on Product2 (after update) {
    if(Process_Exception__c.getInstance(Userinfo.getProfileId()).Run_Trigger__c){
        List<Product2> listStatusChanged = new List<Product2>();
        for (Product2 o : trigger.new) {
            if (o.Product_Status2__c != trigger.oldMap.get(o.ID).Product_Status2__c && ProductStatusChangeAlert__c.getInstance(o.Product_Status2__c) != null){
                listStatusChanged.add(o);
            }
        } 
        if (!listStatusChanged.isEmpty()) {
            ProductStatusChangeAlertController helper = new ProductStatusChangeAlertController(listStatusChanged);
            helper.sendEmailsToOpportunityOwners();
        }
    }
}
Test Class

@isTest
public class ProductStatusChangeAlertControllerTest {
 @isTest static void Product2StatusChangedTest() {
    // Insert "Process_Exception__c" related record with "System Administrator" profile
    Process_Exception__c procExcpRec = new Process_Exception__c(Name = 'Process Exception (Profile)',Run_Trigger__c = true, SetupOwnerId = '00e0Y0000015jIAQAY');
    insert procExcpRec;
    ProductStatusChangeAlert__c prodStaChaAlRec = new ProductStatusChangeAlert__c(Name = 'End of Life');
     insert prodStaChaAlRec;
    Product2 prd = new Product2(Name='Test Product', IsActive=true, MSTAV__c = '80');
    insert prd;

    Id stdPbId = Test.getStandardPricebookId();

    PricebookEntry objPBE = new PricebookEntry(Product2Id=prd.Id, Pricebook2Id=stdPbId, UnitPrice=100, IsActive=true);
    insert objPBE;
    List<Opportunity> tobeInsertedOpptyList = new List<Opportunity>();
    Opportunity objOpp = new Opportunity(Name = 'Testopp', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
    tobeInsertedOpptyList.add(objOpp);
    Opportunity objOpp2 = new Opportunity(Name = 'Testopp2', CloseDate = date.today().addDays(100), StageName = 'Prospecting');
    tobeInsertedOpptyList.add(objOpp2);
    insert tobeInsertedOpptyList;
     
    OpportunityLineItem objOLI = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp.Id);
    insert objOLI;
    OpportunityLineItem objOLI2 = new OpportunityLineItem(PricebookEntryID = objPBE.Id, Quantity = 1, UnitPrice = 100, OpportunityId = objOpp2.Id);
    insert objOLI2;
     
    Product2 objProduct = [select Id, Product_Status2__c,MSTAV__c from Product2 where Id = :objPBE.Product2Id];

    objProduct.MSTAV__c  = '80';
    update objProduct;
    Product2 objProduct2 = [select Id, Product_Status2__c,MSTAV__c from Product2 where Id = :objPBE.Product2Id];
    System.assertEquals('End of Life',objProduct2.Product_Status2__c );
  }
}

Please advise very urgent
Raj VakatiRaj Vakati
try somethink like below
 
@IsTest(SeeAllData=true) 
public with sharing class TriggerTestClass {
    static testMethod void validateTrigger() {
		
		ProductStatusChangeAlert__c ins = new ProductStatusChangeAlert__c();
		// add all fields
		insert ins ;
		
      // Insert Product
Product2 pr = new Product2();
pr.Name='Moto - G1';
pr.isActive=true;
pr.Product_Status2__c ='status vale1';
insert pr;

pr.Product_Status2__c ='Value map to custom setting';


 update pr;
      
    }
}