You need to sign in to do that
Don't have an account?
Raju Mushke 19
i am writing test class for below apex class
i am writing test class for below apex class. i am new to the apex coding.can anyone please help on this.
Apex class:
public class ContactsapexController {
@AuraEnabled
public static List<Account> getAccounts(){
return [select id, Name, description,Owner.Name,Phone from Account];
}
@AuraEnabled
public static List<Contact> getContacts(string accountId){
list<contact> conlist=new list<contact>();
system.debug(accountId);
list<contact> con=[select id, Name, Title, Email, Phone, Account.Owner.Name from Contact where accountId =: accountId];
for(contact c:con){
conlist.add(c);
}
return conlist;
}
@AuraEnabled
public static List<String> deleteRecords(List<contact> lstId){
system.debug('lstId'+lstId);
// for store Error Messages
List<String> oErrorMsg = new List<String> ();
List<Contact> contacts = [select id from Contact where id in : lstId];
Database.DeleteResult[] DR_Dels = Database.delete(contacts, false);
for (Database.DeleteResult dr: DR_Dels){
if (dr.isSuccess()) {
system.debug('successful delete contact');
// Operation was successful
} else {
// Operation failed, so get all errors
for (Database.Error err: dr.getErrors()) {
// add Error message to oErrorMsg list and return the list
oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
}
}
}
return oErrorMsg;
}
}
Apex class:
public class ContactsapexController {
@AuraEnabled
public static List<Account> getAccounts(){
return [select id, Name, description,Owner.Name,Phone from Account];
}
@AuraEnabled
public static List<Contact> getContacts(string accountId){
list<contact> conlist=new list<contact>();
system.debug(accountId);
list<contact> con=[select id, Name, Title, Email, Phone, Account.Owner.Name from Contact where accountId =: accountId];
for(contact c:con){
conlist.add(c);
}
return conlist;
}
@AuraEnabled
public static List<String> deleteRecords(List<contact> lstId){
system.debug('lstId'+lstId);
// for store Error Messages
List<String> oErrorMsg = new List<String> ();
List<Contact> contacts = [select id from Contact where id in : lstId];
Database.DeleteResult[] DR_Dels = Database.delete(contacts, false);
for (Database.DeleteResult dr: DR_Dels){
if (dr.isSuccess()) {
system.debug('successful delete contact');
// Operation was successful
} else {
// Operation failed, so get all errors
for (Database.Error err: dr.getErrors()) {
// add Error message to oErrorMsg list and return the list
oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
}
}
}
return oErrorMsg;
}
}
Try with this code:
I have gone through your problem. Please try the below code.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
Hi
i have wriiten same as per you code.but i am unable cover the code for below two lines.
else {
// Operation failed, so get all errors
for (Database.Error err: dr.getErrors()) {
// add Error message to oErrorMsg list and return the list
oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
}
could you please suggest how to write neagtive senroio for deletemethod.
Thanks
raju Mushke.