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
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7 

Test coverage issue when deploy trigger

Hi,

I have tried to deploy a trigger which based on assign pricebook to the opportunity. When i deploying it throws "code coverage error" as follows.

Code Coverage Failure
The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
CarrierChannelPB


Last time when i deployed a same type of trigger i don't get any issues. I have even tried various test class modules and got failed.
For the reference, I have given below my trigger and Testclass for that. 

Any help on this. Code coverage should change in trigger or test class? Anything need to change in my coding ?

Apex Trigger :

trigger CarrierPB on Opportunity (Before insert, Before update) {
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate))
            {
                 List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> abbAccmap = new Map<Id,String>();
                 List<RecordType> recordtypId =  [Select Id,Name From RecordType Where SobjectType = 'Opportunity'];
                 Map<Id,String> recMap = new Map<Id,String>();
                 Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
                 Set<Id> accountIds = new Set<Id>();
                 Set<Id> RectypIds = new Set<Id>();
                              
                if(!prcbooklist.isEmpty())
                {
                    for(Pricebook2 prcIter : prcbooklist)
                    {
                        prcbookMap.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMap);
                
                for(RecordType rectyp : recordtypId)
                {
                    recMap.put(rectyp.Id,rectyp.name);
                }
                
                for(Opportunity OppIdsItr : Trigger.new)
                {
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,Region__C FROM Account WHERE Id IN:accountIds])
                {
                    abbAccmap.put(AccItr.id,AccItr.Region__C);
                }
            
                for(Opportunity OppItr : Trigger.new)
                {
                    if(prcbookMap.size() > 0)
                    { 
                         
                        if (recMap.get(OppItr.RecordTypeId) == 'p)Carrier-South')
                        {
                            OppItr.Pricebook2Id = prcbookMap.get('SouthPB');
                        }
                        else if(Trigger.isUpdate)
                        {
                          if(OppItr.RecordTypeId!=Trigger.oldMap.get(OppItr.Id).RecordTypeId)
                            {
                            if(recMap.get(OppItr.RecordTypeId) == 'p)Carrier-South')
                            OppItr.Pricebook2Id = prcbookMap.get('SouthPB');
                            else if(abbAccmap.get(OppItr.accountId) == 'North - D3')
                            {
                             OppItr.Pricebook2Id = prcbookMap.get('NorthPB');
                            }
                            System.debug('====assign'+OppItr.Pricebook2Id);
                
                        }
              }
              }

            }
}



Apex Test Class : 

@isTest (SeeAllData = true)
public class TestNewcarrier {
    Public static testmethod void validatepric(){
  
  Zip_Code__c zipRec = new Zip_Code__c(name = '12345',Opp_System__c = 'South', PriceBook_Name__c = 'SouthPB',
                                              Service_Type__c = 'D3',City__c = 'Taren',State__c = 'MA',System__c = 'South');
        insert zipRec;
    
    Id pbID = null;

    pbID = [Select Name, Description From Pricebook2 where Name = 'Carrier-South'].Id;
    List<RecordType> recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'p)Carrier-South'];
    Account acc = new Account(name='TestAcc',BillingPostalCode= zipRec.Name);
        insert acc;
    
    Pricebook2  standardPb = [SELECT id, name, isActive FROM Pricebook2 WHERE IsStandard = true LIMIT 1];
        
        Pricebook2 customPb;
        List<Pricebook2> customPbList = [SELECT id, name, isActive FROM Pricebook2 WHERE Name = 'SouthPB' LIMIT 1];
        if(customPbList.isEmpty()) {
            customPb = new Pricebook2 (Name='SouthPB', Description='Test Pricebook Entry 1', isActive=true);
            insert customPb;
        }else {
            customPb = customPbList[0];
        }

Opportunity objOpp = new Opportunity(Name = 'newTest',recordtypeId=recordtypId[0].Id,AccountId = acc.Id,StageName ='Prospecting',CloseDate=Date.today());
        objOpp.Pricebook2Id=pbID;       
insert objOpp;

Opportunity objUOppt = new opportunity(Id =objOpp.Id );
update objUOppt ;

}
}



Thanks in advance.

Thanks,
Selva
cloudSavvyProgcloudSavvyProg
Hi Selva,

Have you run the test? Run it in Setup->App Setup ->Apex Test Execution -> Choose you test -> Run Or run it Developer Console.

Test should pass for it account towards code coverage. Code coverage is for the classes and triggers.

Let us know if your still having issues.

Regards,
CloudSavvyProg
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7
Hi,

Thanks for your reply.

Yes. I  did and executed successfully. But only when i deploy trigger i got errors such test coverage failure. 

I believe there is no issues with my test class since it is executing properly.

Thanks
cloudSavvyProgcloudSavvyProg
Your error is displaying a different trigger called 'CarrierChannelPB' and you have pasted a different trigger called 'CarrierPB'. Is it correct? Are you looking at the right one?

May be their is no test class for 'CarrierChannelPB'.

Check please.

Regards,
CloudSavvyProg
Selvakumar Anbazhagan 7Selvakumar Anbazhagan 7
Hi,

"CarrierPB" is the correct one. I have again changed the name and have executed. So no issue with the name as my test class remain CarrierPB now.

Thanks,