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
Nagarjuna Reddy.PNagarjuna Reddy.P 

Test class for simple apex class that inserts a record

Hi All,
           I have a simple method that inserts record into account object through lightning component.My apex class method is as follows.
 
 @AuraEnabled
     public static Account saveAccount(account acc){
       insert acc;
       return acc;
     }
My test class for this is

@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
        try{
             insert acc;
        }catch(Exception e){
            system.debug(e);
        }
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }
My Test coverage is 66% and return statement from saveAccount method is not covered for coverage,how to cover this return statement.
Any help is appreciated.

Thank you.
Best Answer chosen by Nagarjuna Reddy.P
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nagarjuna,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }

Please note that you don't have to insert the account in a test class.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Nagarjuna,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
@isTest
private class testClassExample {
    @isTest static void createAcc(){
        Account acc=new Account();
        acc.Name = 'Test Account';
        acc.Phone = '12345';
        acc.Account_Type__c = 'Insurance';
                  
        Account a = example.saveAccount(acc); 
        integer count=[select count() from account];
        system.assertEquals( 1,count);
    }

Please note that you don't have to insert the account in a test class.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Nagarjuna Reddy.PNagarjuna Reddy.P
Hi Khan,
                Thank you for your reply,now it works , thnaks for remainding me that we should not perform dml operations in test classes.
Nagarjuna SadhuNagarjuna Sadhu

Write a apex class to insert the same record in different objects?

/* Description:- Write a apex class, To insert the same record in different objects at a time.(Account,Opportunity * Contact).
Author:- Nagarjuna Sadhu.
Created On: 19/04/2022.
*/ 
Code Here:

public class CreatingRecordMultipleObject {
    public void InsertingRecords(){
        List<Account> acclist = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Balakrishna';
        acc.Type = 'Prospect';
        acc.Industry = 'Bankking';
        acc.Phone = '6598785456';
        //acc.CreatedDate =system.Date;
        acclist.add(acc);
        insert acclist;
        System.debug('Account Record is successfully inserted on Account object'+acclist);
        
        Opportunity opp = new Opportunity();
        Opp.Name = acc.Name;
        opp.Type = acc.Type;
        opp.StageName = acc.Industry;
        opp.closedate = date.today();
        insert opp;
        System.debug('Then automatically changed opportunity recods successfully'+opp);
        
        Contact con = new Contact();
        con.LastName = acc.Name;
        insert con;
        System.debug('contact record is inserted successfully'+con);
        
        
    }
}

Test Class:-


@isTest  // @isTest -> Test class annotation.
public class TestCreatingRecordMultipleObjects {
    public testmethod static void CreatingRecordMultipleObject(){
       CreatingRecordMultipleObject mm = new CreatingRecordMultipleObject();
        mm.InsertingRecords();
    }
}


 

Nagarjuna SadhuNagarjuna Sadhu
1. Deleting the record with the help of SOQL (Salesforce Object Query Language)?
 Author Name:- Nagarjuna Reddy Sadhu.


Public class DeleteRecordAccount{
    public void Deletemethod(){
        List<Account> accList = [select id,name from account where Name= 'pooja'];
        for(Account ac:acclist){
            ac.Name = 'pooja';
        }
        delete acclist;
    }
}  
Nagarjuna SadhuNagarjuna Sadhu

Hi All,
I am writing the code to insert the same record multiple time in particular object?

User-added image

User-added image

Nagarjuna SadhuNagarjuna Sadhu

Hi All,
Q) Write a Batch apex which runs for every one hour and updates the account with number of it's contacts associaited to it ?

Batch Apex:

User-added image

Schedubale Apex:-

User-added image

Calling scheduble class in Anonymous block:-

User-added image

Output:-
After calling to scheduble class then we go to scheduble jobs.

User-added image