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
LIM AI KHOONLIM AI KHOON 

Code Coverage dynamic SObject

Hi, anyone can help me with how to have 100% code coverage for these lines?
 
public static void submitFinalApprovalNo(Id recordId) {
        sObject record = recordId.getSObjectType().newSobject(recordId);
        record.put('Final_Approved__c',false);
        update record;
    }

 
Best Answer chosen by LIM AI KHOON
Maharajan CMaharajan C
Hi Lim,

Don't pass the record type id to method. It's expecting the record Id.

In test class you have to insert the Account record and need to pass the record Id to your actual class. Add if there is any other fields are required to create account in below test class.

I hope you have the Final_Approved__c  custom field in Account Object.  If it's not account object then you have to insert that particular object in below test class.
 
@isTest
public class LCC_App_rej_Return_shipment_cApexTest {
    @isTest static void testsubmitFinalApprovalNo(){
        // Add if there is any other fields are required to create account 
        Account acc = new Account(Name = 'test Account');
        insert acc;
        Test.startTest();
        LCC_App_rej_Return_shipment_cApex.submitFinalApprovalNo(acc.id);
        Test.stopTest();
    }
}

Thanks,
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Lim,

Please find the test class with 100% coverage.

@isTest
public class SubmitTest {
 
      public static testMethod void submitFinalApprovalNoTest(){
       Id record=Schema.SObjectType.Account.RecordTypeInfosByName.get('Student').RecordTypeId;
                  Test.startTest();
                   SubmitFinal.submitFinalApprovalNo(record);
                    Test.stopTest();
      }
}

You have to get record type Id As the below

Id record=Schema.SObjectType.Account.RecordTypeInfosByName.get('Student').RecordTypeId;

here sobject is Account so replace your Sobject and Recordtype Name is Student so replace student with your subject record type Name.

Please let me know it is working or not.

Please mark it as the Best answer so that other people would take reference from it.

Thank You

LIM AI KHOONLIM AI KHOON
SubmitFinal.submitFinalApprovalNo(record);

I got error in this line
Suraj Tripathi 47Suraj Tripathi 47

please show me the error.

Have you used your class name of this method submitFinalApprovalNo()

this is my class name SubmitFinal

LIM AI KHOONLIM AI KHOON
This is the code:
@isTest
    public static void submitFinalApprovalNoTest(){
        Id record=Schema.SObjectType.Account.RecordTypeInfosByName.get('Account - Potential').RecordTypeId;
        Test.startTest();
        LCC_App_rej_Return_shipment_cApex.submitFinalApprovalNo(record);
        Test.stopTest();
        
        
    }
I get this error:
System.SObjectException: Invalid field Final_Approved__c for RecordType
In these lines:
LCC_App_rej_Return_shipment_cApex.submitFinalApprovalNo(record);
        Test.stopTest();


 
Suraj Tripathi 47Suraj Tripathi 47

Account - Potential is label name of record type Right??

please do system.debug(record) and check there is data inside record or not.

 

Suraj Tripathi 47Suraj Tripathi 47
User-added image
LIM AI KHOONLIM AI KHOON
User-added image 
Record type name right?
LIM AI KHOONLIM AI KHOON
User-added image
Suraj Tripathi 47Suraj Tripathi 47
"System.SObjectException: Invalid field Final_Approved__c for RecordType"
 
It might be possible, your account does not has this field Final_Approved__c
if account has this field then your record type Account - Potential does not has this field Final_Approved__c
So correct it.
if you replace Final_Approved__c with another standard field then it will give 100% coverage
like record.put('Name','Test');
 
LIM AI KHOONLIM AI KHOON
If the form/ sObject doesn't have a record type, how to do it?. Yes in account we do not have Final_Approved__c field. But we have record type there. In other forms we have Final_Approved__c but dont have record type.

we cannot change to another standard field, because that class we want to change data in Final_Approved__c to false. Final_Approved__c is a check box type field
Suraj Tripathi 47Suraj Tripathi 47

GO TO OBJECT MANAGER=>ACCOUNT=>FIELD=>NEW =>Final Approved

DATA TYPE=PICKLIST or Text

if pickList then enter the below values

VALUES=>

True

Flase

Maharajan CMaharajan C
Hi Lim,

Don't pass the record type id to method. It's expecting the record Id.

In test class you have to insert the Account record and need to pass the record Id to your actual class. Add if there is any other fields are required to create account in below test class.

I hope you have the Final_Approved__c  custom field in Account Object.  If it's not account object then you have to insert that particular object in below test class.
 
@isTest
public class LCC_App_rej_Return_shipment_cApexTest {
    @isTest static void testsubmitFinalApprovalNo(){
        // Add if there is any other fields are required to create account 
        Account acc = new Account(Name = 'test Account');
        insert acc;
        Test.startTest();
        LCC_App_rej_Return_shipment_cApex.submitFinalApprovalNo(acc.id);
        Test.stopTest();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer