You need to sign in to do that
Don't have an account?
Rahul
Getting only 30% code coverage for the following code. Need Help
trigger updateFundraiser on Account (before update) {
set<id> conIds = new set<id>();
for(account a1: trigger.new) {
if(a1.Contact_Associated__c != null) {
conIds.add(a1.Contact_Associated__c);
}
}
system.debug('conIds::'+conIds);
map<id,contact> conMap = new map<id,contact>([select id,Name, Beneficiary_Name__c,Email, MobilePhone from contact where id IN :conIds]);
system.debug('conMap::'+conMap);
for(account a1:trigger.new){
system.debug('a1.Contact_Associated__c::'+a1.Contact_Associated__c);
system.debug('!!!!!!'+trigger.oldMap.get(a1.id).Contact_Associated__c);
//if(a1.Contact_Associated__c != trigger.oldMap.get(a1.id).Contact_Associated__c) {
system.debug('herere!!!');
a1.Mobile_Number__c = conMap.get(a1.Contact_Associated__c).MobilePhone;
a1.Created_By_Email__c = conMap.get(a1.Contact_Associated__c).Email;
a1.Name = conMap.get(a1.Contact_Associated__c).Name;
system.debug('herere!!!');
system.debug('a1'+a1);
//}
}
}
set<id> conIds = new set<id>();
for(account a1: trigger.new) {
if(a1.Contact_Associated__c != null) {
conIds.add(a1.Contact_Associated__c);
}
}
system.debug('conIds::'+conIds);
map<id,contact> conMap = new map<id,contact>([select id,Name, Beneficiary_Name__c,Email, MobilePhone from contact where id IN :conIds]);
system.debug('conMap::'+conMap);
for(account a1:trigger.new){
system.debug('a1.Contact_Associated__c::'+a1.Contact_Associated__c);
system.debug('!!!!!!'+trigger.oldMap.get(a1.id).Contact_Associated__c);
//if(a1.Contact_Associated__c != trigger.oldMap.get(a1.id).Contact_Associated__c) {
system.debug('herere!!!');
a1.Mobile_Number__c = conMap.get(a1.Contact_Associated__c).MobilePhone;
a1.Created_By_Email__c = conMap.get(a1.Contact_Associated__c).Email;
a1.Name = conMap.get(a1.Contact_Associated__c).Name;
system.debug('herere!!!');
system.debug('a1'+a1);
//}
}
}
All Answers
Please try this code:
@isTest
public class UpdateFundraiserTest {
@isTest
static void updateAccountTest() {
Contact contactAssociated = new Contact (FirstName = 'Test', LastName = 'Associated');
contactAssociated.MobilePhone = '(111) 111-1111';
contactAssociated.Email = 'associatec@test.com';
insert contactAssociated;
Account account = new Account (Name = 'Test Account', Contact_Associated__c = contactAssociated);
insert account;
account.Name= 'New Name';
update account;
Account updatedAccount = [SELECT Id, Name, Mobile_Number__c,Created_By_Email__c FROM Account];
System.assert(updatedAccount.Name == contact.Name);
System.assert(updatedAccount.Mobile_Number__c == contact.MobilePhone);
System.assert(updatedAccount.Created_By_Email__c == contact.Email);
}
}