apex Classpublic 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 : @istestpublic 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(); }}
hi Ramana, 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.
//Main 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);
}
}
}
else{
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'));
}
}
}
//Test Class
@istest
public class CreatingandupdatingaccountcontactRecordsTest {
@istest
public static void UnitTest() {
Account Acc =New Account();
Acc.Name = 'test';
Acc.Phone = '9898989809';
insert Acc;
Contact Con =New Contact();
Con.LastName = 'testCon';
Con.Phone = Acc.Phone;
insert Con;
CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
oCreatingandupdatingaccountcontactRecords.accountName = Acc.Name;
oCreatingandupdatingaccountcontactRecords.contactName = Con.LastName;
oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
oCreatingandupdatingaccountcontactRecords.phoneNumber =Acc.Phone;
oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
}
@istest
public static void UnitTest2() {
CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
oCreatingandupdatingaccountcontactRecords.accountName = 'test';
oCreatingandupdatingaccountcontactRecords.contactName ='tescon';
oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
oCreatingandupdatingaccountcontactRecords.phoneNumber ='09098798678';
oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
}
}
let me know if it helps you and marking it as best.
Thank You