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
Kevin Chiles 930Kevin Chiles 930 

Not enough coverage on Trigger for Record Type

Hello!

I am having an error where I do not have enough coverage on my trigger to deploy.  My code covers the whole trigger except the crucial record type update.  Any help is greatly appreciated!

Trigger:
trigger PartnerContractRecordtype on Case (before update) {

for (Case c : Trigger.New) {
 
Partner_Contract__c pc =[Select Id ,RecordTypeId from Partner_Contract__c where id=:c.Partner_Contract__c limit 1];

RecordType rt = [select id from Recordtype where SobjectType='Partner_Contract__c' and Name =:'Approval'];

if (c.Status =='Partner - Pending Paperwork' && c.Partner_Contract__c!=Null){



pc.RecordtypeId=rt.id;

update pc;
}
}
}

Class:

@isTest(SeeAllData = true)
public class PartnerContractTrigger
{
    public static testMethod void PartnerContractTrigger()
    {
    
    Account a = New Account(Name='test',recordtypeId=[Select Name, Id From RecordType
        Where sObjectType='Account' and isActive=true and Name='Affiliate'].id);
        
        insert a;
        
        Contact ct=New Contact(FirstName='sarah',LastName='jenny',email='test@test.com',AccountId=a.id);
    insert ct;
        
        Account mpdefault = New Account(Name='Megapath - Default',recordtypeId=[Select Name, Id From RecordType
        Where sObjectType='Account' and isActive=true and Name='Affiliate'].id);
        
        Contact ct2=New Contact(FirstName='sarah',LastName='jenny',email='test@test.com',AccountId=mpdefault.id);
        insert ct2;
        
        Insert mpdefault;
        
        Partner_Contract__c pc = New Partner_Contract__c(Name='0',Parent_Partner__c=mpdefault.id,Status__c='Active',
        PartnerProgram__c='Referral',Partner__c=a.Id,recordtypeId=[Select Name, Id From RecordType
        Where sObjectType='Partner_Contract__c' and isActive=true and Name='Non Approval'].id
        );
        Insert pc; 
        
        Availiable_Contract__c ac = New Availiable_Contract__c(Name='0',Contract_Rank__c=1,Partner__c=a.Id,Partner_Contract__c=pc.Id 
        );
                   
        Insert ac;       
        
        RecordType recordtypeId2=[Select Name, Id From RecordType
        Where sObjectType='Partner_Contract__c' and isActive=true and Name='Approval'];
        
        Case C= new Case(AccountId=a.id,ContactId=ct.Id,Sales_Rep__c=UserInfo.getUserId(),Case_Request_Type__c='Partner Onboarding',
        recordtypeId=[Select Name, Id From RecordType
        Where sObjectType='Case' and isActive=true and Name='Channel Support'].id,Partner_Contract__c=pc.id,Company_Name__c=a.Name,
        Clarity_Account_ID__c='N/A',Site_Contact__c='Test', Site_Contact_Phone__c='3157775695',Primary_Affiliate__c='Megapath - Default',
        Primary_Affiliate_ID_2__c='1980',Subject='Onboarding Request for New Partner',Description='Onboarding Request for New Partner');
        
        insert c;
                     
        c.Status='Pending Partner Paperwork';
        update c;
        
        
          
        Pc.Name='1234';
        pc.Status__c='Active';
        Pc.Partner_Contract_Status__c='Partner Activated';
       // pc.recordtypeId=[Select Name, Id From RecordType
       // Where sObjectType='Partner_Contract__c' and isActive=true and Name='Approval'].id;
        update pc;  




    }
}
 
Best Answer chosen by Kevin Chiles 930
pconpcon
Sorry, your test was a little hard to read and I got mixed up with the SOQL inline as part of the case creation. Your issue was that your Status in your test is missing your hyphen.

I've taken the liberty of updating your trigger to make it bulk ready as well are reducing the number of SOQL queries you have to make.  You may want to think about updating the if criteria (line 12-14) to first get the old value and compare it to the new value and only change your record type if the status has changed.
 
trigger PartnerContractRecordtype on Case (before update) {
    RecordType rt = [
        select Id
        from Recordtype
        where SobjectType = 'Partner_Contract__c' and
            Name = 'Approval'
    ];

    List<Partner_Contract__c> contractsToUpdate = new List<Partner_Contract__c>();

    for (Case c : Trigger.new) {
        if (
            c.Status == 'Partner - Pending Paperwork' &&
            c.Partner_Contract__C != null
        ) {
            contractsToUpdate.add(new Partner_Contract__c(
                Id = c.Partner_Contract__c,
                RecordTypeId = rt.Id
            ));
        }
    }

    if (!contractsToUpdate.isEmpty()) {
        update contractsToUpdate;
    }
}
 
public class PartnerContractTrigger {
    public static testMethod void PartnerContractTrigger() {
        Map<Name, RecordType> rtMap = new Map<Name, RecordType>();

        for (RecordType rt [
            select Name
            from RecordType
            where (
                (
                    SObjectType = 'Account' and
                    Name = 'Affiliate'
                ) or (
                    SObjectType = 'Partner_Contract__c' and
                    Name = 'Non Approval'
                ) or (
                    SObjectType = 'Partner_Contract__c' and
                    Name = 'Approval'
                ) or (
                    SObjectType = 'Case' and
                    Name = 'Channel Support'
                )
            ) and
                isActive = true
        ]) {
            rtMap.put(rt.Name, rt);
        }

        Account a = new Account(
            Name = 'test',
            RecordTypeId = rtMap.get('Affiliate').Id
        );
        insert a;

        Contact ct = new Contact(
            FirstName = 'sarah',
            LastName = 'jenny',
            email = 'test@test.com',
            AccountId = a.id
        );
        insert ct;

        Account mpdefault = new Account(
            Name = 'Megapath - Default',
            RecordTypeId = rtMap.get('Affiliate').Id
        );
        insert mpdefault;

        Contact ct2 = new Contact(
            FirstName = 'sarah',
            LastName = 'jenny',
            email = 'test@test.com',
            AccountId = mpdefault.id
        );
        insert ct2;

        Partner_Contract__c pc = new Partner_Contract__c(
            Name = '0',
            Parent_Partner__c = mpdefault.id,
            Status__c = 'Active',
            PartnerProgram__c = 'Referral',
            Partner__c = a.Id,
            RecordTypeId = rtMap.get('Non Approval').Id
        );
        insert pc;

        Availiable_Contract__c ac = new Availiable_Contract__c(
            Name = '0',
            Contract_Rank__c = 1,
            Partner__c = a.Id,
            Partner_Contract__c = pc.Id
        );
        insert ac;

        Case c = new Case(
            AccountId = a.id,
            ContactId = ct.Id,
            Sales_Rep__c = UserInfo.getUserId(),
            Case_Request_Type__c = 'Partner Onboarding',
            RecordTypeId = rtMap.get('Channel Support').Id,
            Partner_Contract__c = pc.id,
            Company_Name__c = a.Name,
            Clarity_Account_ID__c = 'N/A',
            Site_Contact__c = 'Test',
            Site_Contact_Phone__c = '3157775695',
            Primary_Affiliate__c = 'Megapath - Default',
            Primary_Affiliate_ID_2__c = '1980',
            Subject = 'Onboarding Request for New Partner',
            Description = 'Onboarding Request for New Partner'
        );
        insert c;

        Test.startTest();

        c.Status = 'Pending - Partner Paperwork';
        update c;

        Test.stopTest();

        Partner_Contract__c result = [
            select RecordTypeId
            from Partner_Contract__c
            where Id = :pc.Id
        ];

        System.assertEquals(
            rtMap.get('Approval').Id,
            result.RecordTypeId,
            'The record type id was not updated'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

I would also recommend that you look into creating a factory to create your test data for you.

All Answers

pconpcon
The problem is your Partner_Contract__c field is not set on your case in your test class and therefore your if statement does not evaluate to true.  Additionally, please please please remove the seeAllData.  Unless you absolutely have to (and odds are you don't) you shouldn't use it.  All of the data you need for your test is either accessable without it or should be create explicitly for the test.

NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.
Kevin Chiles 930Kevin Chiles 930
Thanks for the feedback!! However I have the partner contract set on the test class currently. It is the pc.id so I am still stumped. I will try to remove see all data as well.
pconpcon
Sorry, your test was a little hard to read and I got mixed up with the SOQL inline as part of the case creation. Your issue was that your Status in your test is missing your hyphen.

I've taken the liberty of updating your trigger to make it bulk ready as well are reducing the number of SOQL queries you have to make.  You may want to think about updating the if criteria (line 12-14) to first get the old value and compare it to the new value and only change your record type if the status has changed.
 
trigger PartnerContractRecordtype on Case (before update) {
    RecordType rt = [
        select Id
        from Recordtype
        where SobjectType = 'Partner_Contract__c' and
            Name = 'Approval'
    ];

    List<Partner_Contract__c> contractsToUpdate = new List<Partner_Contract__c>();

    for (Case c : Trigger.new) {
        if (
            c.Status == 'Partner - Pending Paperwork' &&
            c.Partner_Contract__C != null
        ) {
            contractsToUpdate.add(new Partner_Contract__c(
                Id = c.Partner_Contract__c,
                RecordTypeId = rt.Id
            ));
        }
    }

    if (!contractsToUpdate.isEmpty()) {
        update contractsToUpdate;
    }
}
 
public class PartnerContractTrigger {
    public static testMethod void PartnerContractTrigger() {
        Map<Name, RecordType> rtMap = new Map<Name, RecordType>();

        for (RecordType rt [
            select Name
            from RecordType
            where (
                (
                    SObjectType = 'Account' and
                    Name = 'Affiliate'
                ) or (
                    SObjectType = 'Partner_Contract__c' and
                    Name = 'Non Approval'
                ) or (
                    SObjectType = 'Partner_Contract__c' and
                    Name = 'Approval'
                ) or (
                    SObjectType = 'Case' and
                    Name = 'Channel Support'
                )
            ) and
                isActive = true
        ]) {
            rtMap.put(rt.Name, rt);
        }

        Account a = new Account(
            Name = 'test',
            RecordTypeId = rtMap.get('Affiliate').Id
        );
        insert a;

        Contact ct = new Contact(
            FirstName = 'sarah',
            LastName = 'jenny',
            email = 'test@test.com',
            AccountId = a.id
        );
        insert ct;

        Account mpdefault = new Account(
            Name = 'Megapath - Default',
            RecordTypeId = rtMap.get('Affiliate').Id
        );
        insert mpdefault;

        Contact ct2 = new Contact(
            FirstName = 'sarah',
            LastName = 'jenny',
            email = 'test@test.com',
            AccountId = mpdefault.id
        );
        insert ct2;

        Partner_Contract__c pc = new Partner_Contract__c(
            Name = '0',
            Parent_Partner__c = mpdefault.id,
            Status__c = 'Active',
            PartnerProgram__c = 'Referral',
            Partner__c = a.Id,
            RecordTypeId = rtMap.get('Non Approval').Id
        );
        insert pc;

        Availiable_Contract__c ac = new Availiable_Contract__c(
            Name = '0',
            Contract_Rank__c = 1,
            Partner__c = a.Id,
            Partner_Contract__c = pc.Id
        );
        insert ac;

        Case c = new Case(
            AccountId = a.id,
            ContactId = ct.Id,
            Sales_Rep__c = UserInfo.getUserId(),
            Case_Request_Type__c = 'Partner Onboarding',
            RecordTypeId = rtMap.get('Channel Support').Id,
            Partner_Contract__c = pc.id,
            Company_Name__c = a.Name,
            Clarity_Account_ID__c = 'N/A',
            Site_Contact__c = 'Test',
            Site_Contact_Phone__c = '3157775695',
            Primary_Affiliate__c = 'Megapath - Default',
            Primary_Affiliate_ID_2__c = '1980',
            Subject = 'Onboarding Request for New Partner',
            Description = 'Onboarding Request for New Partner'
        );
        insert c;

        Test.startTest();

        c.Status = 'Pending - Partner Paperwork';
        update c;

        Test.stopTest();

        Partner_Contract__c result = [
            select RecordTypeId
            from Partner_Contract__c
            where Id = :pc.Id
        ];

        System.assertEquals(
            rtMap.get('Approval').Id,
            result.RecordTypeId,
            'The record type id was not updated'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

I would also recommend that you look into creating a factory to create your test data for you.
This was selected as the best answer
pconpcon
Crap, I even screwed up line 94 of the test.  It should read
 
c.Status = 'Partner - Pending Paperwork';

I would recommend that you create a utility class that contains statics for these types of things.  For example:
 
public class CaseUtils {
    public static string PENDING_PAPERWORK_STATUS = 'Partner - Pending Paperwork';
}

Then use that in your test classes and your triggers.  This will keep you from making a typo since the class will not deploy / save if you don't have the variable name set correctly.
 
c.Status = CaseUtils.PENDING_PAPERWORK_STATUS;
Kevin Chiles 930Kevin Chiles 930
Solid!  Thank you very much for the assistance!  This was an eye opening experience lol!  Thanks!
Kevin Chiles 930Kevin Chiles 930
For the Old vs New status, what would be the way to implement that?  I am still fairly new at this
pconpcon
So for example, you'd do
 
trigger PartnerContractRecordtype on Case (before update) {
    RecordType rt = [
        select Id
        from Recordtype
        where SobjectType = 'Partner_Contract__c' and
            Name = 'Approval'
    ];

    List<Partner_Contract__c> contractsToUpdate = new List<Partner_Contract__c>();

    for (Case c : Trigger.new) {
        Case oldCase = Trigger.oldMap.get(c.Id);
        if (
            c.Status == 'Partner - Pending Paperwork' &&
            c.Status != oldCase.Status &&
            c.Partner_Contract__c != null
        ) {
            contractsToUpdate.add(new Partner_Contract__c(
                Id = c.Partner_Contract__c,
                RecordTypeId = rt.Id
            ));
        }
    }

    if (!contractsToUpdate.isEmpty()) {
        update contractsToUpdate;
    }
}

By adding lines 12 and 15 we don't try to re assign the record type if the status was already set and we just happened to update something else on the object.
Kevin Chiles 930Kevin Chiles 930
gotcha!  that makes sense.  Thank you so much for your help on this!!!!