You need to sign in to do that
Don't have an account?
Ramana123
Can anyone help me how to write test class for the below code.
I have four fields account,contact,phone and email on visualforce page. when I search the account name which exists in the data base then those email , phone and contact should update to that account. If not found then create a new account with the following fields.
apex Class
public class CreatingandupdatingaccountcontactRecords {
public string accountName {get;set;}
public string contactName {get;set;}
public string emailId {get;set;}
public string phoneNumber {get;set;}
List<Account> accountList = new List<Account>();
List<Account> upsertAccount = new List<Account>();
List<Contact> contactList = new List<Contact>();
List<Contact> upsertContact = new List<Contact>();
public void checkAccountsandcontacts() {
accountList = [select Name from Account where Name =:accountName];
if(accountList.size() > 0) {
for(Account a : accountList ) {
if(a.Name == accountName) {
a.Emai_id__c =emailId;
a.Phone = phoneNumber;
upsertAccount.add(a);
}
}
}
if(accountList.size() < 0){
Account newAccount = new Account();
newAccount.Name = accountName;
newAccount.Emai_id__c = emailId;
newAccount.Phone = phoneNumber;
upsertAccount.add(newAccount);
}
upsert upsertAccount;
contactList =[select LastName from contact where LastName =:contactName];
if(contactList.size() > 0) {
for(contact con : contactList) {
if(con.LastName == contactName ) {
con.Email = EmailId;
con.Phone = phoneNumber;
for(Account a1 : accountList) {
con.AccountId = a1.Id;
}
upsertContact.add(con);
}
}
}
else{
contact newcontact = new contact();
newcontact.lastName = contactName;
newcontact.phone = phonenumber;
newcontact.Email = EmailId;
for(Account acc:upsertAccount) {
newcontact.AccountId = acc.Id;
}
upsertContact.add(newcontact);
}
upsert upsertContact;
if(accountList.size() > 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully'));
}
if(accountList.size() < 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));
}
}
}
TestClass :
@istest
public class contactAccountcreationpage_TC {
public static testmethod void duplicateEmailerrormessage() {
String accountName = 'testing account';
Account acc = new Account();
acc.Name = 'testing account';
acc.Emai_id__c = 'dinesh@gmail.com';
acc.Phone = '842631791';
insert acc;
Account acc1 = new Account();
acc1.Name = 'testing account';
acc1.Emai_id__c = 'dinesh@gmail.com';
acc1.Phone = '842631791';
insert acc1;
Contact con = new contact();
con.LastName = 'vini';
con.AccountId = acc.Id;
insert con;
CreatingandupdatingaccountcontactRecords obj = new CreatingandupdatingaccountcontactRecords();
obj.checkAccountsandcontacts();
}
}
apex Class
public class CreatingandupdatingaccountcontactRecords {
public string accountName {get;set;}
public string contactName {get;set;}
public string emailId {get;set;}
public string phoneNumber {get;set;}
List<Account> accountList = new List<Account>();
List<Account> upsertAccount = new List<Account>();
List<Contact> contactList = new List<Contact>();
List<Contact> upsertContact = new List<Contact>();
public void checkAccountsandcontacts() {
accountList = [select Name from Account where Name =:accountName];
if(accountList.size() > 0) {
for(Account a : accountList ) {
if(a.Name == accountName) {
a.Emai_id__c =emailId;
a.Phone = phoneNumber;
upsertAccount.add(a);
}
}
}
if(accountList.size() < 0){
Account newAccount = new Account();
newAccount.Name = accountName;
newAccount.Emai_id__c = emailId;
newAccount.Phone = phoneNumber;
upsertAccount.add(newAccount);
}
upsert upsertAccount;
contactList =[select LastName from contact where LastName =:contactName];
if(contactList.size() > 0) {
for(contact con : contactList) {
if(con.LastName == contactName ) {
con.Email = EmailId;
con.Phone = phoneNumber;
for(Account a1 : accountList) {
con.AccountId = a1.Id;
}
upsertContact.add(con);
}
}
}
else{
contact newcontact = new contact();
newcontact.lastName = contactName;
newcontact.phone = phonenumber;
newcontact.Email = EmailId;
for(Account acc:upsertAccount) {
newcontact.AccountId = acc.Id;
}
upsertContact.add(newcontact);
}
upsert upsertContact;
if(accountList.size() > 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully'));
}
if(accountList.size() < 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));
}
}
}
TestClass :
@istest
public class contactAccountcreationpage_TC {
public static testmethod void duplicateEmailerrormessage() {
String accountName = 'testing account';
Account acc = new Account();
acc.Name = 'testing account';
acc.Emai_id__c = 'dinesh@gmail.com';
acc.Phone = '842631791';
insert acc;
Account acc1 = new Account();
acc1.Name = 'testing account';
acc1.Emai_id__c = 'dinesh@gmail.com';
acc1.Phone = '842631791';
insert acc1;
Contact con = new contact();
con.LastName = 'vini';
con.AccountId = acc.Id;
insert con;
CreatingandupdatingaccountcontactRecords obj = new CreatingandupdatingaccountcontactRecords();
obj.checkAccountsandcontacts();
}
}
I did some chages in your Main Ctrl but logic are same. Replace your Ctrl with Following and copy & paste test class in your org. let me know if it helps you and marking it as best.
Thank You
All Answers
I did some chages in your Main Ctrl but logic are same. Replace your Ctrl with Following and copy & paste test class in your org. let me know if it helps you and marking it as best.
Thank You
Hi Anudeep ,
The Highlight lines of code are not covered ,the Updation code is not covered how to cover It
Thanks In Advance.
public class CreatingandupdatingaccountcontactRecords {
public string accountName {get;set;}
public string contactName {get;set;}
public string emailId {get;set;}
public string phoneNumber {get;set;}
List<Account> accountList = new List<Account>();
List<Account> upsertAccount = new List<Account>();
List<Contact> contactList = new List<Contact>();
List<Contact> upsertContact = new List<Contact>();
public void checkAccountsandcontacts() {
accountList = [select Name from Account where Name =:accountName];
if(accountList.size() > 0) {
for(Account a : accountList ) {
if(a.Name == accountName) {
a.Emai_id__c =emailId;
a.Phone = phoneNumber;
upsertAccount.add(a);
}
}
}
if(accountList.size() < 0){
Account newAccount = new Account();
newAccount.Name = accountName;
newAccount.Emai_id__c = emailId;
newAccount.Phone = phoneNumber;
upsertAccount.add(newAccount);
}
upsert upsertAccount;
contactList =[select LastName from contact where LastName =:contactName];
if(contactList.size() > 0) {
for(contact con : contactList) {
if(con.LastName == contactName ) {
con.Email = EmailId;
con.Phone = phoneNumber;
for(Account a1 : accountList) {
con.AccountId = a1.Id;
}
upsertContact.add(con);
}
}
}
else{
contact newcontact = new contact();
newcontact.lastName = contactName;
newcontact.phone = phonenumber;
newcontact.Email = EmailId;
for(Account acc:upsertAccount) {
newcontact.AccountId = acc.Id;
}
upsertContact.add(newcontact);
}
upsert upsertContact;
if(accountList.size() > 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully'));
}
if(accountList.size() < 0){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));
}
}
}