Skip to main content
My class: 

 

public with sharing class SL_CallLogContact_new 

 

{

 

    public String callLogId {get; set;}

 

    public Call_Log__c callLog {get; set;}

 

    public Call_Log_Contact__c callLogContact {get; set;}

 

    public SL_CallLogContact_new()

 

    {

 

        callLogId = ApexPages.currentPage().getParameters().get('clid');

 

        if(callLogId != null)  {

 

 callLog = [SELECT ID,Company__c FROM Call_Log__c WHERE ID= : callLogId];

 

            callLogContact = new Call_Log_Contact__c(Call_Log__c = this.callLogId);

 

        }

 

        

 

    }

 

    public Boolean doSave()

 

    {

 

        if (String.valueOf(callLogContact.Contact__c)=='') 

 

        {

 

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact is empty.'));

 

            return false;

 

        }

 

        

 

        try

 

        {

 

            ID testID = (ID) String.valueOf(callLogContact.Contact__c);

 

        }

 

        catch(Exception ex)

 

        {

 

            String contName = String.valueOf(callLogContact.Contact__c);

 

            callLogContact.Contact__c = null;

 

            for(Contact item : Database.query('select Id,Name from Contact where Name like \''+String.escapeSingleQuotes(contName)+'%\' limit 1'))

 

            {

 

                callLogContact.Contact__c = item.id;

 

            }

 

            if(callLogContact.Contact__c==null)

 

            {               

 

                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact is incorrect.'));

 

                return false;

 

            }

 

        }

 

        

 

        

 

        if(callLogContact != null) 

 

        {

 

            try {

 

                insert callLogContact;

 

                return true;

 

            } catch (Exception e) {

 

                if(e.getMessage().contains('INSUFFICIENT')) ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Insufficient Privileges. You do not have the level of access necessary to perform the operation you requested.'));

 

                else if(e.getMessage().contains('Contacts cannot be duplicated')){}

 

                else ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));

 

            }

 

        }

 

        return false;

 

    }

 

    public PageReference save()

 

    {

 

        if(this.doSave())

 

        {

 

            PageReference res = new PageReference('/'+callLogId);

 

            res.setRedirect(true);

 

            return res;

 

        }

 

        return null;

 

    }

 

    public PageReference saveAndNew()

 

    {

 

        if(this.doSave())

 

        {

 

            PageReference res = new PageReference('/apex/SL_CallLogContact_new?clid='+callLogId);

 

            res.setRedirect(true);

 

            return res;

 

        }

 

        return null;

 

    }

 

    public PageReference cancel()

 

    {

 

        PageReference res = new PageReference('/'+callLogId);

 

        res.setRedirect(true);

 

        return res;

 

    }

 

}

 

My test class is at 62% ( how to cover the Save and Save&New method and the catch expection to bring code coverage to 75%) ?

 

Test class:

 

@isTest(SeeAllData=true)

 

private class Test_SL_CallLogContact_new 

 

{

 

    static testMethod void cover_SL_CallLogContact_new() 

 

    {

 

        ID InvestmentFirm = [SELECT ID FROM RecordType WHERE DeveloperName = 'Investment_Firm' limit 1].ID;

 

        Account newAccount = new Account(Name = 'Company 3',

 

                                 BillingCity = 'New York',

 

                                 BillingState = 'Illinois',

 

                                 BillingPostalCode = '53200',

 

                                 WebSite = 'youtube.com',

 

                                 Phone = '1112123',

 

                                 RecordTypeId = InvestmentFirm,

 

                                 type='Private Equity',

 

                                 Fax = '123231');

 

        insert newAccount;

 

        contact c = new contact( accountid= newAccount.Id, lastname ='xyz', firstname ='abc', email ='abc@xytest.com');

 

        insert c;

 

        Project__c newProject = new Project__c(Company__c = newAccount.Id, Name='Test');

 

        insert newProject;

 

        Call_Log__c oCallLog = new Call_Log__c(Project__c = newProject.Id, Company__c = newAccount.Id);

 

        insert oCallLog;

 

        ApexPages.currentPage().getParameters().put('clid',oCallLog.Id);

 

        

 

        SL_CallLogContact_new controller = new SL_CallLogContact_new();

 

        controller.save(); 

 

        controller.saveAndNew();

 

        controller.cancel();

 

    }

 

}
2 respuestas
  1. 10 mar 2021, 1:42
    Hi Pooja,

     

    You can force an error to increase coverage.

     

    In this code, the following is always null so I don't think it will work.

     

    String contName = String.valueOf(callLogContact.Contact__c);

     

     

    public with sharing class SL_CallLogContact_new {

    public String callLogId {get; set;}

    public Call_Log__c callLog {get; set;}

    public Call_Log_Contact__c callLogContact {get; set;}

    private Boolean isError = false;

    public SL_CallLogContact_new()

    {

    callLogId = ApexPages.currentPage().getParameters().get('clid');

    if(callLogId != null) {

    callLog = [SELECT ID,Company__c FROM Call_Log__c WHERE ID= : callLogId];

    callLogContact = new Call_Log_Contact__c(Call_Log__c = this.callLogId);

    }

    }

    public SL_CallLogContact_new(Boolean myError)

    {

    callLogId = ApexPages.currentPage().getParameters().get('clid');

    if(callLogId != null) {

    callLog = [SELECT ID,Company__c FROM Call_Log__c WHERE ID= : callLogId];

    callLogContact = new Call_Log_Contact__c(Call_Log__c = this.callLogId);

    }

    this.isError = myError;

    }

    public Boolean doSave()

    {

    if (String.valueOf(callLogContact.Contact__c)=='')

    {

    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact is empty.'));

    return false;

    }

    try

    {

    if (this.isError) {

    Integer I = 100/0;

    }

    ID testID = (ID) String.valueOf(callLogContact.Contact__c);

    }

    catch(Exception ex)

    {

    String contName = String.valueOf(callLogContact.Contact__c);

    callLogContact.Contact__c = null;

    System.debug('#4############################ --------' + String.escapeSingleQuotes(contName));//<-- null???

    for(Contact item : Database.query('select Id,Name from Contact where Name like \''+String.escapeSingleQuotes(contName)+'%\' limit 1'))

    {

    callLogContact.Contact__c = item.id;

    }

    if(callLogContact.Contact__c==null)

    {

    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Contact is incorrect.'));

    return false;

    }

    }

    if(callLogContact != null)

    {

    try {

    insert callLogContact;

    return true;

    } catch (Exception e) {

    if(e.getMessage().contains('INSUFFICIENT')) ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Insufficient Privileges. You do not have the level of access necessary to perform the operation you requested.'));

    else if(e.getMessage().contains('Contacts cannot be duplicated')){}

    else ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));

    }

    }

    return false;

    }

    public PageReference save()

    {

    if(this.doSave())

    {

    PageReference res = new PageReference('/'+callLogId);

    res.setRedirect(true);

    return res;

    }

    return null;

    }

    public PageReference saveAndNew()

    {

    if(this.doSave())

    {

    PageReference res = new PageReference('/apex/SL_CallLogContact_new?clid='+callLogId);

    res.setRedirect(true);

    return res;

    }

    return null;

    }

    public PageReference cancel()

    {

    PageReference res = new PageReference('/'+callLogId);

    res.setRedirect(true);

    return res;

    }

    }

     

    @isTest(SeeAllData=true)

    public class Test_SL_CallLogContact_new {

    static testMethod void cover_SL_CallLogContact_new()

    {

    ID InvestmentFirm = [SELECT ID FROM RecordType WHERE DeveloperName = 'Investment_Firm' limit 1].ID;

    Account newAccount = new Account(Name = 'Company 3',

    BillingCity = 'New York',

    BillingState = 'Illinois',

    BillingPostalCode = '53200',

    WebSite = 'youtube.com',

    Phone = '1112123',

    RecordTypeId = InvestmentFirm,

      type='Private Equity',

    Fax = '123231');

    insert newAccount;

    contact c = new contact( accountid= newAccount.Id, lastname ='xyz', firstname ='abc', email ='abc@xytest.com');

    insert c;

    Project__c newProject = new Project__c(Company__c = newAccount.Id, Name='Test');

    insert newProject;

    Call_Log__c oCallLog = new Call_Log__c(Project__c = newProject.Id, Company__c = newAccount.Id);

    insert oCallLog;

    Call_Log_Contact__c oCallLogContact = new Call_Log_Contact__c(Call_Log__c=oCallLog.Id,contact__c = c.Id);

    insert oCallLogContact;

    ApexPages.currentPage().getParameters().put('clid',oCallLog.Id);

    SL_CallLogContact_new controller = new SL_CallLogContact_new();

    controller.save();

    controller.saveAndNew();

    controller.cancel();

    }

    static testMethod void cover_SL_CallLogContact_new_error()

    {

    ID InvestmentFirm = [SELECT ID FROM RecordType WHERE DeveloperName = 'Investment_Firm' limit 1].ID;

    Account newAccount = new Account(Name = 'Company 3',

    BillingCity = 'New York',

    BillingState = 'Illinois',

    BillingPostalCode = '53200',

    WebSite = 'youtube.com',

    Phone = '1112123',

    RecordTypeId = InvestmentFirm,

      type='Private Equity',

    Fax = '123231');

    insert newAccount;

    contact c = new contact( accountid= newAccount.Id, lastname ='xyz', firstname ='abc', email ='abc@xytest.com');

    insert c;

    Project__c newProject = new Project__c(Company__c = newAccount.Id, Name='Test');

    insert newProject;

    Call_Log__c oCallLog = new Call_Log__c(Project__c = newProject.Id, Company__c = newAccount.Id);

    insert oCallLog;

    Call_Log_Contact__c oCallLogContact = new Call_Log_Contact__c(Call_Log__c=oCallLog.Id,contact__c = c.Id);

    insert oCallLogContact;

    ApexPages.currentPage().getParameters().put('clid',oCallLog.Id);

    SL_CallLogContact_new controller = new SL_CallLogContact_new(true);

    controller.save();

    controller.saveAndNew();

    controller.cancel();

    }

    }

     

     
0/9000