You need to sign in to do that
Don't have an account?
sam_Admin
Test class help for 2 triggers
I have the below triggers and test class for them, Trigger-1 has 85% coverage and Trigger-2 has 66%, can anyone please help me in improving code coverage? Highlighted lines in bold which are not covering
Trigger-1:
trigger AccountName_Opp on Opportunity (before insert, before update ){
Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
for(Opportunity opp:Trigger.new)
{
OppMap.put(opp.Account_Name__c, opp);
}
List<Account> accLst = [SELECT ID, Name FROM Account where Name in :OppMap.keySet()];
for(Account acct : accLst){
OppMap.get(acct.Name).AccountId = acct.ID;
}
}
Trigger-2:
trigger AccountName_Contact on Contact (before insert, before update ){
Set<String> conaccounts = new Set<String>();
for(Contact con:Trigger.new)
{
conaccounts.add(con.Account_Name__c);
}
List<Account> accLst = [SELECT ID, Name FROM Account where Name in :conaccounts];
System.debug(accLst);
for(Account acct : accLst){
for(Contact co : Trigger.new){
if(co.Account_Name__c == acct.Name)
co.accountid = acct.id;
}
}
}
Testclass:
@istest
public class FFCNAccountName
{
static testmethod void myTest()
{
Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
Account acc = new Account(Name = 'test', phone = '1234567890');
insert acc;
Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id, Amount = 50, CurrencyIsoCode = 'USD');
insert opp;
Contact con = new Contact( LastName = 'Test2', Email = 'test.user@gmail.com', AccountId = acc.Id );
insert con;
}
}
Trigger-1:
trigger AccountName_Opp on Opportunity (before insert, before update ){
Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
for(Opportunity opp:Trigger.new)
{
OppMap.put(opp.Account_Name__c, opp);
}
List<Account> accLst = [SELECT ID, Name FROM Account where Name in :OppMap.keySet()];
for(Account acct : accLst){
OppMap.get(acct.Name).AccountId = acct.ID;
}
}
Trigger-2:
trigger AccountName_Contact on Contact (before insert, before update ){
Set<String> conaccounts = new Set<String>();
for(Contact con:Trigger.new)
{
conaccounts.add(con.Account_Name__c);
}
List<Account> accLst = [SELECT ID, Name FROM Account where Name in :conaccounts];
System.debug(accLst);
for(Account acct : accLst){
for(Contact co : Trigger.new){
if(co.Account_Name__c == acct.Name)
co.accountid = acct.id;
}
}
}
Testclass:
@istest
public class FFCNAccountName
{
static testmethod void myTest()
{
Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
Account acc = new Account(Name = 'test', phone = '1234567890');
insert acc;
Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id, Amount = 50, CurrencyIsoCode = 'USD');
insert opp;
Contact con = new Contact( LastName = 'Test2', Email = 'test.user@gmail.com', AccountId = acc.Id );
insert con;
}
}
Try to update your test class like below Let us know if this will help you
All Answers
Try to update your test class like below Let us know if this will help you
I just found this Blog Post (https://automationchampion.com/2015/03/13/getting-started-with-process-builder-part-10-auto-forward-records-to-a-connection/) which uses an out of box Process Builder approach to automatically share records to a Salesforce to Salesforce connection without having the need to write code. You might want to give it a try since that would help you save all the effort and pain of writing test code for the earlier written Apex Trigger during your production deployment.