Can someone please help me how can I get the code coverage for below line:

code coverage

Test class code:

public class Testownerassisgnbasedonaccount {

      @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
                Lead le=new Lead(
                        Company='Park controls',   
        Insert le;
               List<string> cmpny=new List<String>();
               Map<string,Id> mp1=new Map<string,Id>();   
               Account acct = new Account(Name='Test Account',Billingcountry='India',Industry='Energy',Activity__c='Distributor');
                insert acct;
                cmpny.add('Test Account');
               List<Account> ACC = [select Phone,Name,Id,OwnerId from Account where Name Like:cmpny LIMIT 1];
            for(Account ac:ACC){
       Lead lne=new lead();
        Insert lne;


Thanks in advance!...

  • Can you please share the code where you define leadAccoubtIds?
    – bpilling
    Jul 16, 2018 at 4:50
  • Hi bpilling Thanks for your reply List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account WHERE Name LIKE :soqlLikeStr]; The above actually I'm with leadAccountIds
    – VINOD
    Jul 16, 2018 at 4:59
  • 1
    Hi Vinod - Looks like you have a query at line #7 of your screenshot. Have you tried to create test data so that at least one record will be returned for the query at line #7
    – Ansuman
    Jul 16, 2018 at 5:45
  • HI Ansuman, Yes, I have tried with that.
    – VINOD
    Jul 16, 2018 at 6:16
  • @VINOD, in your screenshot you have applied condition : leadAccountIds.size()==1. Have you satisfying this condition in your test class? means, Is your test-data returning only one record which satisfy your query?
    – sfdc_sk
    Jul 16, 2018 at 6:41

1 Answer 1


Hope this helps you..

In Test Class, You need to insert Account first and then Lead

I guess, your trigger would be something like this:

trigger StackExchangeQ2 on Lead (before insert) {
    List<String> companies = new List<String>();
    For (lead l:trigger.new){
    List<String> soqlLikeStr = new List<String>();
    for(String str:companies) {
    System.debug('soql like str = ' + soqlLikeStr);

    List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account 
    WHERE Name LIKE :soqlLikeStr];
    System.debug('leadAccountIds = ' + leadAccountIds);

    Map<String, Id> acctNameId=new Map<String,Id>();
    Map<String, Id> acctNameOwner=new Map<String,Id>();

    if(!leadAccountIds.isEmpty() && leadAccountIds.size()==1){
        For(Account a : leadAccountIds){
        For(Lead l2 : trigger.new){

Test Class for this -(gives 100% code coverage)

  public class StackExchangeQ2Test {

  @isTest static void TestDeleteAccountWithOneOpportunity() {
    //insert Account
    Account acc = new Account(
        Name = 'Test Park controls Account'
    insert acc;
    //insert Lead
    Lead l = new Lead(
        FirstName = 'Jake',
        LastName = 'Doe',
        Phone = '9874563210',
        Country = 'India',
        Company = 'Park controls'
    Insert l;
  • 2
    It may give coverage, but this test method is incomplete because it doesn't have any assertions. Assertions are the things that make unit tests truly useful because they are used to verify that your code produces the results you expect it to. Without assertions, the only thing that's being tested is that your code doesn't run into any governor limits or unhandled exceptions. A high-quality answer will also take the time to explain why your solution works. Both of these things are a tad on the hard side to do though, given OP only provided a small portion of their code.
    – Derek F
    Jul 16, 2018 at 13:00
  • Hi @DerekF, thanks for your guidance but my main aim behind this answer was to tell that Account should be inserted first and then the lead, rest of the logic can be added/modified by the asker himself. Jul 17, 2018 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.