You need to sign in to do that
Don't have an account?
Nagarjuna 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.
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.
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.
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
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.
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
Thank you for your reply,now it works , thnaks for remainding me that we should not perform dml operations in test classes.
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();
}
}
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;
}
}
Hi All,
I am writing the code to insert the same record multiple time in particular object?
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:
Schedubale Apex:-
Calling scheduble class in Anonymous block:-
Output:-
After calling to scheduble class then we go to scheduble jobs.