You need to sign in to do that
Don't have an account?
Anna Suganthi
how to write test case for updating Account status?
Hi all,
Am a beginner to Salesforce development and Apex . I have a scenario where i need to create a trigger to update the account status to "Ëngaged" whenever the contact with Event subject "Initial Meeting" changes to "Sales Qualified". I have written the trigger for it. But now, stuck with the test class. Here is my trigger,
trigger contactSetEngagedAccount on contact (after update){
Map<ID, Contact> contactsToCheck = new Map<ID, Contact>();
for (Contact updatedContact : System.Trigger.new){
Contact oldContact = System.Trigger.oldMap.get(updatedContact.Id);
//If the contact status is updated to "Sales Qualified" then update the Map
if(oldContact.contact_status__c != updatedContact.contact_status__c && updatedContact.Contact_Status__c.compareTo('Sales Qualified') == 0){
contactsToCheck.put(updatedContact.Id,updatedContact);
}
// Check for events with Initial Meeting
//
List<Event> eventList=([select WhoId, subject From Event where WhoId IN :contactsToCheck.keySet() and (subject = 'Initial Meeting')]);
Set<ID> contactIds = new Set<ID>();
for(Event eventId:eventList) {
contactIds.add(eventId.WhoId);
}
// get the list of accounts
List<Account> accountList =
[select id, Status__c from Account
where Status__c IN ('Prospect', '')
and Id in (select AccountId from Contact where Id IN :contactIds)];
// change the account status to Engaged
for (Account oneAccount : accountList ) {
oneAccount.Status__c = 'Engaged';
}
// Update accounts
update accountList;
}
}
Can anyone please help me with the test class. It will be very helpful!!! TIA.
Am a beginner to Salesforce development and Apex . I have a scenario where i need to create a trigger to update the account status to "Ëngaged" whenever the contact with Event subject "Initial Meeting" changes to "Sales Qualified". I have written the trigger for it. But now, stuck with the test class. Here is my trigger,
trigger contactSetEngagedAccount on contact (after update){
Map<ID, Contact> contactsToCheck = new Map<ID, Contact>();
for (Contact updatedContact : System.Trigger.new){
Contact oldContact = System.Trigger.oldMap.get(updatedContact.Id);
//If the contact status is updated to "Sales Qualified" then update the Map
if(oldContact.contact_status__c != updatedContact.contact_status__c && updatedContact.Contact_Status__c.compareTo('Sales Qualified') == 0){
contactsToCheck.put(updatedContact.Id,updatedContact);
}
// Check for events with Initial Meeting
//
List<Event> eventList=([select WhoId, subject From Event where WhoId IN :contactsToCheck.keySet() and (subject = 'Initial Meeting')]);
Set<ID> contactIds = new Set<ID>();
for(Event eventId:eventList) {
contactIds.add(eventId.WhoId);
}
// get the list of accounts
List<Account> accountList =
[select id, Status__c from Account
where Status__c IN ('Prospect', '')
and Id in (select AccountId from Contact where Id IN :contactIds)];
// change the account status to Engaged
for (Account oneAccount : accountList ) {
oneAccount.Status__c = 'Engaged';
}
// Update accounts
update accountList;
}
}
Can anyone please help me with the test class. It will be very helpful!!! TIA.
Thanks for the reply!
Will try and let you know.
// case 0 update contact status
contact2.contact_status__c ='Sales Qualified';
contact2.AccountId = account1.Id;
update contact2;
Contact queryContact = [select contact_status__C from Contact where Id = :contact2.id];
System.assert([SELECT count() FROM Account WHERE Id = :account1.Id and status__c = 'Engaged'] == 1);
Also, my trigger is not fired when i run this test case. my code coverage is still None. it is not referring to my test case. :(
Figured out and everything is working fine and yes your code works!!!
Thanks a ton! :) :)