You need to sign in to do that
Don't have an account?
Ammar P 9
I need 80 % code coverage for my apex class? Please anyone help me for test class.
Apex Trigger:
trigger ContactTrigger on Contact (before insert,after insert) {
if(Trigger.isInsert && Trigger.isAfter){
ContactTriggerHelper.UpdateOpportunity(Trigger.New);
}
}
trigger ContactTrigger on Contact (before insert,after insert) {
if(Trigger.isInsert && Trigger.isAfter){
ContactTriggerHelper.UpdateOpportunity(Trigger.New);
}
}
Can You Please Share Helper Class Code Aswell
public class ContactTriggerHelper {
public static void UpdateOpportunity(List<Contact> ContactList){
system.debug('ContactList................'+ContactList);
set<id> conid=new set<id>();
for(Contact co:ContactList){
conid.add(co.AccountId);
}
List<Opportunity> opplist=[Select id,PrimaryContact__c,ContactRole__c,Number__c,AccountId from opportunity where AccountId=:conid];
for(Opportunity op:opplist)
{
for(Contact conlist : ContactList )
{
if((conlist.IsPrimaryContact__c==true ))
{
system.debug('iniff................');
op.PrimaryContact__c=conlist.LastName;
op.ContactRole__c=conlist.Title;
//op.AccountId=conlist.AccountId;
//opp.Number__c=conlist.Phone;
}
}
}
system.debug('opplist............'+opplist);
update opplist;
}
}
@istest
public class ContactTriggerHelper_Test {
@testSetup public static void setup()
{
Account acc = new Account();
acc.Name='John';
insert acc;
contact con = new contact();
con.IsPrimaryContact__c=true;
con.lastname='Doe';
con.Title='This is new contact';
con.AccountId=acc.Id;
con.Phone=9893283378;
insert con.
Opportunity opp = new Opportunity();
opp.PrimaryContact__c='xyz';
opp.ContactRole__c='This is new opportunity';
opp.AccountId=acc.Id;
opp.Number__c=9897234353;
insert opp;
}
@istest public static void test()
{
List<Opportunity> opplist=[Select id,PrimaryContact__c,ContactRole__c,Number__c,AccountId from opportunity];
List<Contact> conlist=[Select Id,IsPrimaryContact__c,lastname,Title,AccountId,Phone from contact];
test.startTest();
ContactTriggerHelper.UpdateOpportunity(conlist);
test.stopTest();
}
}
I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi