You need to sign in to do that
Don't have an account?

How we make this trigger test class please help this time cove coverage is 0
/*Date.............12/06/2014,VivekSingh
Discription.......
This trigger is work on the opportunity object this is bulkify trigger update contact role in opportuniy
*/
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));
}
lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
no primaryContact role found at all have make from scratch
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;
}
}
}
}
Discription.......
This trigger is work on the opportunity object this is bulkify trigger update contact role in opportuniy
*/
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));
}
lstOppContactRolePrimary = [select id,contactId,opportunityId from OpportunityContactRole where opportunityId in :setOppId];
no primaryContact role found at all have make from scratch
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;
}
}
}
}
1)First inser a contact record.
2) Insert a opportunity record.
3)insert a opportunitycontctrole record by satisfing the IF condition.
4) retrive the inserted opportunitycontactrole record by writting the query.
5) and now by satisfing the Else condition update the record again.
Thanks,
pradeep
You can run the test class containing all the test data (as told by pradeep)
@isTest
Private class className{
static testMethod void methodName(){
Test.startTest();
// Insert opportunity with contact roles
// Insert opportunity without contact roles
//.......
Test.stopTest();
}
}
Regards,
Veena
i will work on this try but no code coverage please write code