You need to sign in to do that
Don't have an account?
vivek singh 26
Hi this is my triger and test class this time tracker code cover 30% how we make cover 100% please help me
TRigger
---------------------------------
trigger OpportunityTrigger on Opportunity (After insert,After update)
{
if(trigger.isAfter )
{
if(trigger.isInsert){
List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
Set<String> setOppId = new Set<String>();
system.debug('!!!!After insert');
List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
for(Opportunity o:trigger.new)
{
setOppId.add(String.valueOf(o.id));
system.debug('@@@@@setOppId '+(String.valueOf(o.id)) );
}
lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
//no primaryContact role found at all have make from scratch
system.debug('lstOppContactRolePrimary '+lstOppContactRolePrimary );
if(lstOppContactRolePrimary.size() > 0)
{
Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
for(OpportunityContactRole conRole:lstOppContactRolePrimary)
{
mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
}
for(String id:setOppId)
{
if(mapOppIdConrole.containsKey(id))
{
Opportunity oppTeamp = trigger.newMap.get(id);
if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
{
//both are equal , we will just have to update the role
OpportunityContactRole tempRole = mapOppIdConrole.get(id);
tempRole.Role = 'Buyer';
lstContactRoletoUpdate.add(tempRole);
}
else
{
//we have the contact role but the contacts are different
OpportunityContactRole tempRole = mapOppIdConrole.get(id);
tempRole.Role = 'Buyer';
tempRole.contactId = oppTeamp.buyer__c;
lstContactRoletoUpdate.add(tempRole);
}
}else
{
// we have to make contact role
System.debug('####Creating absolutely new one 1');
OpportunityContactRole tempRole = new OpportunityContactRole();
tempRole.Role = 'Buyer';
tempRole.contactId = trigger.newMap.get(id).buyer__c;
tempRole.isPrimary = true;
lstContactRoletoUpdate.add(tempRole);
}
}
}else
{
System.debug('####Creating absolutely new one 2');
opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
objopportunityTriggerHandler.createNewContact(trigger.new);
}
if(lstContactRoletoUpdate.size() > 0)
{
upsert lstContactRoletoUpdate;
}
}
}
/*
if(trigger.isBefore)
{
if(trigger.isInsert)
{
system.debug('!!!!Before Insert');
for(Opportunity o:trigger.new)
{
List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
System.debug('!!!!Before insert contact id'+o);
System.debug('!!!!Before insert buyer id'+o.buyer__c);
System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
}
}
}
*/
//opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
//objopportunityTriggerHandler.createNewContact(trigger.new);
//code added by deepak dhingra on 13/6/2014 to update the line items
if(trigger.isAfter && trigger.isUpdate)
{
Set<string> qualifiedOppId = new Set<string>();
for(Opportunity o:Trigger.new)
{
if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
{
qualifiedOppId.add(String.valueOf(o.id).left(15));
}
}
if(qualifiedOppId.size() > 0)
{
List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
lstLineItems = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
if(lstLineItems.size() > 0)
{
update lstLineItems;
}
}
_______________________________
TRACKER CLASSSSSSS
@isTest
public class OpportunityTriggerHandlerTest
{
static testMethod void insertOpportunity()
{
Account objAccount = new Account();
objAccount.Name = 'Vivek';
objAccount.Type = 'Supplier';
objAccount.Phone = '1234';
objAccount.CurrencyIsoCode = 'AUD';
insert objAccount;
Contact objcontact = new contact();
objcontact.LastName = 'Singh';
objcontact.CurrencyIsoCode = 'AUD';
objcontact.Accountid = objAccount.id ;
insert objContact;
opportunity objopportunity= new opportunity ();
objopportunity.Name = 'Hindi2014';
objopportunity.CloseDate = System.Today();
objopportunity.Accountid = objAccount.id;
objopportunity.Buyer__c = objcontact.id;
objopportunity.Vendor__c = objaccount.id;
objopportunity.StageName = 'Prposal Sent';
objopportunity.Billing_Terms__c = 'On Completion';
objopportunity.Type = 'New Business';
insert Objopportunity;
OpportunityContactRole objOpportunityContactRole=new OpportunityContactRole();
objOpportunityContactRole.ContactId = Objopportunity.Buyer__c;
objOpportunityContactRole.OpportunityId = Objopportunity.Id;
objOpportunityContactRole.Role = 'Buyer';
insert objOpportunityContactRole;
List<OpportunityContactRole> lstOpportunityContactRole = new List<OpportunityContactRole>();
//OpportunityContactRole = [select id from OpportunityContactRole where OpportunityContactRole ='Buyer'];
}
static testMethod void updateOpp()
{
opportunity objopportunity2= new opportunity ();
objopportunity2.Name = 'Hindi2015';
//upadte Objopportunity2;
}
}
---------------------------------
trigger OpportunityTrigger on Opportunity (After insert,After update)
{
if(trigger.isAfter )
{
if(trigger.isInsert){
List<OpportunityContactRole> lstContactRoletoUpdate = new List<OpportunityContactRole>();
Set<String> setOppId = new Set<String>();
system.debug('!!!!After insert');
List<OpportunityContactRole> lstOppContactRolePrimary = new List<opportunityContactRole>();
for(Opportunity o:trigger.new)
{
setOppId.add(String.valueOf(o.id));
system.debug('@@@@@setOppId '+(String.valueOf(o.id)) );
}
lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
//no primaryContact role found at all have make from scratch
system.debug('lstOppContactRolePrimary '+lstOppContactRolePrimary );
if(lstOppContactRolePrimary.size() > 0)
{
Map<String,OpportunityContactRole> mapOppIdConrole = new Map<String,OpportunityContactRole>();
for(OpportunityContactRole conRole:lstOppContactRolePrimary)
{
mapOppIdConrole.put(String.valueOf(conRole.opportunityId),conRole);
}
for(String id:setOppId)
{
if(mapOppIdConrole.containsKey(id))
{
Opportunity oppTeamp = trigger.newMap.get(id);
if(oppTeamp.buyer__c == mapOppIdConrole.get(id).contactId)
{
//both are equal , we will just have to update the role
OpportunityContactRole tempRole = mapOppIdConrole.get(id);
tempRole.Role = 'Buyer';
lstContactRoletoUpdate.add(tempRole);
}
else
{
//we have the contact role but the contacts are different
OpportunityContactRole tempRole = mapOppIdConrole.get(id);
tempRole.Role = 'Buyer';
tempRole.contactId = oppTeamp.buyer__c;
lstContactRoletoUpdate.add(tempRole);
}
}else
{
// we have to make contact role
System.debug('####Creating absolutely new one 1');
OpportunityContactRole tempRole = new OpportunityContactRole();
tempRole.Role = 'Buyer';
tempRole.contactId = trigger.newMap.get(id).buyer__c;
tempRole.isPrimary = true;
lstContactRoletoUpdate.add(tempRole);
}
}
}else
{
System.debug('####Creating absolutely new one 2');
opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
objopportunityTriggerHandler.createNewContact(trigger.new);
}
if(lstContactRoletoUpdate.size() > 0)
{
upsert lstContactRoletoUpdate;
}
}
}
/*
if(trigger.isBefore)
{
if(trigger.isInsert)
{
system.debug('!!!!Before Insert');
for(Opportunity o:trigger.new)
{
List<OpportunityContactRole> lstOppContactRolePrimary = [select id,contactId from OpportunityContactRole where opportunityId =: o.id];
System.debug('!!!!Before insert contact id'+o);
System.debug('!!!!Before insert buyer id'+o.buyer__c);
System.debug('!!!!Before lstOppContactRolePrimary = >'+lstOppContactRolePrimary);
}
}
}
*/
//opportunityTriggerHandler objopportunityTriggerHandler = new opportunityTriggerHandler();
//objopportunityTriggerHandler.createNewContact(trigger.new);
//code added by deepak dhingra on 13/6/2014 to update the line items
if(trigger.isAfter && trigger.isUpdate)
{
Set<string> qualifiedOppId = new Set<string>();
for(Opportunity o:Trigger.new)
{
if(o.Agency_Discount_Percentage__c != Trigger.Oldmap.get(o.id).Agency_Discount_Percentage__c)
{
qualifiedOppId.add(String.valueOf(o.id).left(15));
}
}
if(qualifiedOppId.size() > 0)
{
List<OpportunityLineItem> lstLineItems = new List<OpportunityLineItem>();
lstLineItems = [select id from OpportunityLineItem where opportunityid in :qualifiedOppId];
if(lstLineItems.size() > 0)
{
update lstLineItems;
}
}
_______________________________
TRACKER CLASSSSSSS
@isTest
public class OpportunityTriggerHandlerTest
{
static testMethod void insertOpportunity()
{
Account objAccount = new Account();
objAccount.Name = 'Vivek';
objAccount.Type = 'Supplier';
objAccount.Phone = '1234';
objAccount.CurrencyIsoCode = 'AUD';
insert objAccount;
Contact objcontact = new contact();
objcontact.LastName = 'Singh';
objcontact.CurrencyIsoCode = 'AUD';
objcontact.Accountid = objAccount.id ;
insert objContact;
opportunity objopportunity= new opportunity ();
objopportunity.Name = 'Hindi2014';
objopportunity.CloseDate = System.Today();
objopportunity.Accountid = objAccount.id;
objopportunity.Buyer__c = objcontact.id;
objopportunity.Vendor__c = objaccount.id;
objopportunity.StageName = 'Prposal Sent';
objopportunity.Billing_Terms__c = 'On Completion';
objopportunity.Type = 'New Business';
insert Objopportunity;
OpportunityContactRole objOpportunityContactRole=new OpportunityContactRole();
objOpportunityContactRole.ContactId = Objopportunity.Buyer__c;
objOpportunityContactRole.OpportunityId = Objopportunity.Id;
objOpportunityContactRole.Role = 'Buyer';
insert objOpportunityContactRole;
List<OpportunityContactRole> lstOpportunityContactRole = new List<OpportunityContactRole>();
//OpportunityContactRole = [select id from OpportunityContactRole where OpportunityContactRole ='Buyer'];
}
static testMethod void updateOpp()
{
opportunity objopportunity2= new opportunity ();
objopportunity2.Name = 'Hindi2015';
//upadte Objopportunity2;
}
}
Just change the code to this and try reply me if u have further issues Thanks,
pradeep.
There is no need of writting another method for that ur are almost done the job but u have written another method. have u got ur test class.
thanks,
pradeep.