lundi 23 décembre 2019

How to resolve the System validation error for the test class

I need help in resolving the Test Class error "FIELD_CUSTOM_VALIDATION_EXCEPTION", the code coverage is showing 100%, i have even disabled the validation rules on the lead object but i am getting the "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error::::::Divide by 0: [CRM_Owner__c]" error

trigger ShareWithCRMOwner on Lead (after insert,after update) {
List<LeadShare> csShareList = new List<LeadShare>();
for( Lead cs : trigger.new ) {
    if( cs.CRM_Owner__c != NULL ) {
        // Create a new LeadShare object for each Lead where CRM_Owner__c field is not NULL.
        LeadShare csShare = new LeadShare();
        // Give Read write access to that user for this particular Lead record.
        csShare.LeadAccessLevel = 'edit';
        // Assign Lead Id of Lead record.
        csShare.LeadId = cs.id;
        // Assign user id to grant read write access to this particular Lead record.
        csShare.UserOrGroupId = cs.CRM_Owner__c;
        csShareList.add( csShare );
    }
}
if( csShareList != null && csShareList.size() != 0 ) {
    try {
        insert csShareList;
        update csShareList;
        if(Test.isRunningTest())
        {
            integer k=1/0;
        }
    }catch( Exception e ) {
        trigger.new[0].CRM_Owner__c.addError('Error::::::'+e.getMessage());
    }
}

}

test class

@isTest

private class TestShareWithCRMOwner {

// test that newly inserted records marked as pubic=true have corresponding shares created
static testMethod void testAddShares() {

    Set<ID> ids = new Set<ID>();
    List<Lead> Leads = new List<Lead>();

    for (Integer i=0;i<50;i++)
        Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                           Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));

    insert Leads;

    // get a set of all new created ids
    for (Lead c : Leads)
        ids.add(c.id);

    // assert that 50 shares were created
    List<LeadShare> shares = [select id from LeadShare where 
                              LeadId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),50);

}

// insert records and switch them from public = true to public = false
static testMethod void testUpdateContacts() {

    Set<ID> ids = new Set<ID>();
    List<Lead> Leads = new List<Lead>();

    for (Integer i=0;i<50;i++)
        Leads.add(new Lead(FirstName='First ',LastName='Name '+i,RecordTypeId='012p0000000Nn05AAC',
                           Email='email'+i+'@email.com',Company='ABSYZ',CRM_Owner__c='00528000006OKhPAAW'));

    insert Leads;

    for (Lead c : Leads)
        ids.add(c.id);

    update Leads;

    // assert that 0 shares exist
    List<LeadShare> shares = [select id from LeadShare where 
                              LeadId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);

    for (Lead c : Leads)
        c.CRM_Owner__c='2F00528000006OKhP';

    update Leads;

    // assert that 50 shares were created
    shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),50);

    for (Lead c : Leads)
        c.CRM_Owner__c='2F00528000006OKhP';

    update Leads;

    // assert that 0 shares exist
    shares = [select id from LeadShare where LeadId IN :ids and RowCause = 'Manual'];
    System.assertEquals(shares.size(),0);

}

}

Aucun commentaire:

Enregistrer un commentaire