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
Laurie DrewLaurie Drew 

Help with a test class

Hi there, I am new to apex and hoping someone can point me in the right direction with my test class, it shows coverage for the 'insert' but not for the return portion.  Can someone tell me what more I need to add?

Here is my function:

public static id SaveUpdateAccountProfileFormDetails(Account accountForm ){
         
        insert accountForm;
        return accountForm.id;

My test class so far:

static testmethod void SaveUpdateAccountProfileFormDetailsTest()
    {
        Account accountForm = new Account(Name='TestAccount',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US',FEIN__c='123456',BusinessOrganization__c='LLC');
        
        test.startTest();
        
                SaveUpdateAccountDetails.SaveUpdateAccountProfileFormDetails(accountForm);
        
        
        test.stopTest();
    }

Thank you in advance for any assistance you can provide!
Best Answer chosen by Laurie Drew
Deepali KulshresthaDeepali Kulshrestha
Hi Laurie,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved)
-------------- Apex Class------- -----

public class SaveUpdateAccountDetails {
public static id SaveUpdateAccountProfileFormDetails(Account accountForm ) {

insert accountForm;
return accountForm.id;
}
}

----------------Test Class-------------------

@isTest public class SaveUpdateAccountProfileFormDetailsTest {

    static testmethod void SaveUpdateAccountProfileFormDetailsTest()
        {
            Account accountForm = new Account(Name='TestAccount',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US',FEIN__c='123456',BusinessOrganization__c='LLC');

            test.startTest();

            Id AccId=SaveUpdateAccountDetails.SaveUpdateAccountProfileFormDetails(accountForm);
            System.debug('AccId===>'+AccId);
            System.assertEquals(true,accountForm.Id!=null);

            test.stopTest();
        }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Laurie,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved)
-------------- Apex Class------- -----

public class SaveUpdateAccountDetails {
public static id SaveUpdateAccountProfileFormDetails(Account accountForm ) {

insert accountForm;
return accountForm.id;
}
}

----------------Test Class-------------------

@isTest public class SaveUpdateAccountProfileFormDetailsTest {

    static testmethod void SaveUpdateAccountProfileFormDetailsTest()
        {
            Account accountForm = new Account(Name='TestAccount',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US',FEIN__c='123456',BusinessOrganization__c='LLC');

            test.startTest();

            Id AccId=SaveUpdateAccountDetails.SaveUpdateAccountProfileFormDetails(accountForm);
            System.debug('AccId===>'+AccId);
            System.assertEquals(true,accountForm.Id!=null);

            test.stopTest();
        }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
This was selected as the best answer
Laurie DrewLaurie Drew
That did it, thank you so much!
Ajay K DubediAjay K Dubedi
Hi Laurie,
Try below code it covers 100% code:
//Class code
public class InsertAccount {
    public static Id accountInsertRecord(Account accountForm){
        insert accountForm;
        return accountForm.Id;
    }
}

//Test Class
@isTest
public class InsertAccountTest {
    @isTest
    static void accountInsertRecord(){
        Account accountForm = new Account(Name='TestAccount',billingStreet='123 Test Street',billingCity='Springfield',billingpostalcode='12345',billingcountry='US');
        
        test.startTest();
        String AccId=InsertAccount.accountInsertRecord(accountForm);
        System.debug('AccId===>'+AccId);
        System.assertNotEquals(String.isNotEmpty(AccId),false);
        test.stopTest();
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi