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
Ruchi Gupta KeshriRuchi Gupta Keshri 

Code is not Cover where i am wrong?

@isTest static void testUpsertCase() {
        // 1. Insert new record
        Lead lead1Id = LeadWebService.upsertLead('Test1', 'Tata', 'Working - Contacted','Web','9999999999',null);
        // Verify new record was created
        System.assert(lead1Id != null);
        Lead lead1 = [SELECT Id,LeadSource FROM Lead WHERE Id=:'00Q5i000005Ot9hEAC'];
        System.assert(lead1 != null);
        System.assertEquals(lead1.LeadSource, 'Web');
        // 2. Update status of existing record to Working
        Lead lead2Id = LeadWebService.upsertLead('Test1', 'Tata', 'Open - Not Contacted','Phone Inquiry','9999999999','00Q5i000005Ot9hEAC');
        // Verify record was updated
        System.assertEquals(lead1Id, lead2Id);
        Lead lead2 = [SELECT Id,Status FROM Lead WHERE Id=:'00Q5i000005Ot9hEAC'];
        System.assert(lead2 != null);
        System.assertEquals(lead2.Status, 'Open - Not Contacted');
    }User-added image
Best Answer chosen by Ruchi Gupta Keshri
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ruchi,

Can you try the below test method it will cover 100% code for that method.
 
@istest
public class TestUpsertLead {
@isTest static void testUpsertCase() {
        // 1. Insert new record
        lead l = new Lead();
    	l.LastName='test1';
    l.Company='tata';
    l.Status='Working - Contacted';
    l.LeadSource='web';
    l.ExternalId__c='00Q5i000005Ot9hEAC';
    insert l;
    
       // Lead lead1Id = LeadWebService.upsertLead('Test1', 'Tata', 'Working - Contacted','Web','9999999999',null);
       // // Verify new record was created
       // System.assert(lead1Id != null);
        //Lead lead1 = [SELECT Id,LeadSource FROM Lead WHERE Id=:'00Q5i000005Ot9hEAC'];
        //System.assert(lead1 != null);
       // System.assertEquals(lead1.LeadSource, 'Web');
        // 2. Update status of existing record to Working
        Lead lead2Id = LeadWebService.upsertLead('Test1', 'Tata', 'Open - Not Contacted','Phone Inquiry','9999999999','00Q5i000005Ot9hEAC');
    Lead lead2 = [SELECT Id,Status FROM Lead WHERE ExternalId__c=:'00Q5i000005Ot9hEAC'];
        System.assert(lead2 != null);
        System.assertEquals(lead2.Status, 'Open - Not Contacted');
       
     
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you share the apex class as you shared the test class. It is very hard to get the code from the screenshot.

Thanks,
 
Ruchi Gupta KeshriRuchi Gupta Keshri
   @HttpPut
    global static Lead upsertLead(String name, String company, String status, String leadSource, String phone, string idd) {
        RestRequest request= RestContext.request;
        String leeId =request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);

      lead obj= [select id,Name, Company, LeadSource, Status,ExternalId__c, Phone from Lead where ExternalId__c=:idd Limit 1];
          obj.LastName=name;
           obj.Company=company;
       obj.Status=status;
       obj.LeadSource=leadSource;
       obj.Phone=phone;
        Upsert obj;
        return obj;
    }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Ruchi,

Can you try the below test method it will cover 100% code for that method.
 
@istest
public class TestUpsertLead {
@isTest static void testUpsertCase() {
        // 1. Insert new record
        lead l = new Lead();
    	l.LastName='test1';
    l.Company='tata';
    l.Status='Working - Contacted';
    l.LeadSource='web';
    l.ExternalId__c='00Q5i000005Ot9hEAC';
    insert l;
    
       // Lead lead1Id = LeadWebService.upsertLead('Test1', 'Tata', 'Working - Contacted','Web','9999999999',null);
       // // Verify new record was created
       // System.assert(lead1Id != null);
        //Lead lead1 = [SELECT Id,LeadSource FROM Lead WHERE Id=:'00Q5i000005Ot9hEAC'];
        //System.assert(lead1 != null);
       // System.assertEquals(lead1.LeadSource, 'Web');
        // 2. Update status of existing record to Working
        Lead lead2Id = LeadWebService.upsertLead('Test1', 'Tata', 'Open - Not Contacted','Phone Inquiry','9999999999','00Q5i000005Ot9hEAC');
    Lead lead2 = [SELECT Id,Status FROM Lead WHERE ExternalId__c=:'00Q5i000005Ot9hEAC'];
        System.assert(lead2 != null);
        System.assertEquals(lead2.Status, 'Open - Not Contacted');
       
     
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
Ruchi Gupta KeshriRuchi Gupta Keshri
Thanks @sai Praveen