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

First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please add Primary contact role: []
Hi Team,
This is the Class and when i run the class it through the error....
This is the Class and when i run the class it through the error....
@isTest public class LeadOpportunityTest { static testMethod void LeadTestmethod() { RecordType LeadRecordType = [SELECT Id FROM RecordType WHERE SobjectType = 'Lead' AND Name = 'IPL' LIMIT 1]; Lead leadVar=new Lead(LastName='Singla',FirstName='Dinesh',Company='Infosys',State__c='New Delhi',Origin_State_1__c='Tamil Nadu',LeadSource='Web',Status='Open',Home_Team__c='RR',Email='dinesh.singla24@gmail.com', RR_vs_CSK__c=true,RR_vs_RCB__c=true,RR_vs_KXIP__c=true,RR_vs_MI__c=true,Recordtypeid=LeadRecordType.id); insert leadVar; Lead leadVar1=new Lead(LastName='kumar',FirstName='Raj',Company='invoice',State__c='New Delhi',Origin_State_1__c='Tamil Nadu',LeadSource='Web',Status='Open',Home_Team__c='MI',Email='dinesh.singla24@gmail.com',MI_vs_RR__c=true ,MI_vs_DD__c=true,MI_vs_KXIP__c=true,MI_vs_KKR__c=true,MI_vs_RCB__c=true,MI_vs_CSK__c=true,Recordtypeid=LeadRecordType.id); insert leadVar1; Lead leadVar2=new Lead(LastName='Farhan',FirstName='akhtar',Company='wipro',State__c='New Delhi',Origin_State_1__c='Tamil Nadu',LeadSource='Web',Status='Open',Home_Team__c='CSK',Email='dinesh.singla24@gmail.com',CSK_vs_RR__c=true,CSK_vs_DD__c=true ,CSK_vs_KXIP__c=true,CSK_vs_KKR__c=true,CSK_vs_RCB__c=true,CSK_vs_SRH__c=true,CSK_vs_MI__c=true,Recordtypeid=LeadRecordType.id); insert leadVar2; Lead leadVar3=new Lead(LastName='Prasad',FirstName='Madhvan',Company='wipro',State__c='New Delhi',Origin_State_1__c='Tamil Nadu',LeadSource='Web',Status='Open',Home_Team__c='KXIP',Email='dinesh.singla24@gmail.com',KXIP_vs_DD__c=true ,KXIP_vs_KKR__c=true,KXIP_vs_RR__c=true,Recordtypeid=LeadRecordType.id); insert leadVar3; Lead leadVar4=new Lead(LastName='Rohirt',FirstName='sharma',Company='wipro',State__c='New Delhi',Origin_State_1__c='Tamil Nadu',LeadSource='Web',Status='Open',Home_Team__c='DD',Email='dinesh.singla24@gmail.com',DD_vs_RR__c=true,DD_vs_RCB__c=true ,DD_vs_KXIP__c=true,DD_vs_MI__c=true,DD_vs_KKR__c=true,Recordtypeid=LeadRecordType.id); insert leadVar4; } static testMethod void OpportunityTestmethod() { Account accVar=new Account(name='Infosys',Destination_State__c='Tamil Nadu'); insert accVar; Contact conVar=new Contact(LastName='Singla',FirstName='Dinesh',Email='dinesh.singla24@gmail.com',AccountId=accVar.id); insert conVar; RecordType oppDiabetesRecordType = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = 'IPL' LIMIT 1]; Opportunity oppVar=new Opportunity(name='Infosys',Accountid=accVar.id,Home_Team__c='DD',CloseDate=Date.Today(),StageName='Qualified',LeadSource='Web',recordtypeid=oppDiabetesRecordType.id); insert oppVar; Product2 proVar=new Product2(Name='test',IsActive=true,Family='Sports',Boxes_Type__c='Corporate Box/Hospitality/Gene',Event_Date__c=Date.Today()+1,Home_Team__c='RR',Match_Between__c='RR vs KKR'); insert proVar; PriceBook2 priceBook = [Select Id from PriceBook2 where IsStandard=true]; //insert priceBook; OpportunityContactRole oppContactRole=new OpportunityContactRole(OpportunityId=oppVar.id,ContactId=conVar.id,IsPrimary=true); //OpportunityId,ContactId From OpportunityContactRole where IsPrimary=true insert oppContactRole; PriceBookEntry pbe = new PriceBookEntry(Pricebook2Id=priceBook.Id, Product2Id=proVar.Id, UnitPrice=5, IsActive=true); insert pbe; OpportunityLineItem oppLineItemVar=new OpportunityLineItem(OpportunityId = oppVar.Id, PricebookEntryId=pbe.Id, Quantity=1, UnitPrice=5); insert oppLineItemVar; oppVar.StageName='Payment Received'; oppVar.Payment_Received__c=true; oppVar.Transaction_Reference_No__c='transaction'; oppVar.Customer_Payment_Date__c=Date.Today(); oppVar.Booking_Ids__c='bookingid'; update oppVar; system.debug('....update opportunity.........'+oppVar); } static testMethod void OpportunityTestmethodPaymentConfirm() { Account accVar=new Account(Name='Infosys',Destination_State__c='Tamil Nadu'); insert accVar; Contact conVar=new Contact(LastName='Singla',FirstName='Dinesh',Email='dinesh.singla24@gmail.com',AccountId=accVar.id); insert conVar; RecordType oppRecordType = [SELECT Id FROM RecordType WHERE SobjectType = 'Opportunity' AND Name = 'IPL' LIMIT 1]; Opportunity oppVar=new Opportunity(Name='Infosys',Accountid=accVar.id, Home_Team__c='DD',CloseDate=Date.Today(),StageName='Qualified',LeadSource='Web',recordtypeid=oppRecordType.id); insert oppVar; Product2 proVar=new Product2(Name='test',IsActive=true,Family='Sports',Boxes_Type__c='Corporate Box/Hospitality/Gene',Event_Date__c=Date.Today()+1,Home_Team__c='RR',Match_Between__c='RR vs KKR'); insert proVar; PriceBook2 priceBook = [Select Id from PriceBook2 where IsStandard=true]; //insert priceBook; OpportunityContactRole oppContactRole=new OpportunityContactRole(OpportunityId=oppVar.id,ContactId=conVar.id,IsPrimary=true); //OpportunityId,ContactId From OpportunityContactRole where IsPrimary=true insert oppContactRole; PriceBookEntry pbe = new PriceBookEntry(Pricebook2Id=priceBook.Id, Product2Id=proVar.Id, UnitPrice=5, IsActive=true); insert pbe; OpportunityLineItem oppLineItemVar=new OpportunityLineItem(OpportunityId = oppVar.Id, PricebookEntryId=pbe.Id, Quantity=1, UnitPrice=5); insert oppLineItemVar; oppVar.StageName='Payment Confirmation'; oppVar.Customer_Payment_Confirmed__c=true; oppVar.Checked_By__c=UserInfo.getUserId(); oppVar.Payment_Received_on__c=Date.Today(); oppVar.Amount_Received__c=55555; oppVar.Booking_Ids__c='bookingid'; update oppVar; } }
trigger emailnotificationOpportunity on Opportunity (before insert,after update) {
if(Trigger.isInsert && Trigger.isBefore) {
for(Opportunity obj :trigger.new) {
if(obj.Lead_Recordtype__c!=''&& obj.Lead_Recordtype__c!=null) {
if(obj.Lead_Recordtype__c =='IPL') {
obj.RecordTypeid=Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('IPL').getRecordTypeId();
}
else if(obj.Lead_Recordtype__c=='Movies') {
obj.RecordTypeid=Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Movies').getRecordTypeId();
}
}
}
}
if(trigger.isupdate && trigger.isAfter) {
//Public list<Opportunity> Opportunitylistnew=trigger.new;
map<id,OpportunityContactRole> oppContactmap=new map<id,OpportunityContactRole>();
oppContactmap=new map<id,OpportunityContactRole>();
set<id> oppid=new set<id>();
oppid=new set<id>();
for(Opportunity temp:trigger.new)
{
oppid.add(temp.id); // Creating list of opportunity id
}
boolean primarycontact=true;
// querying oppcontactRole. So that to check wheather the Primary contact is there is not
List<OpportunityContactRole> oppContactlist=[Select OpportunityId,ContactId From OpportunityContactRole where IsPrimary=true and OpportunityId in :oppid];
if(oppContactlist.size()>0)
{
for(OpportunityContactRole temp:oppContactlist)
{
oppContactmap.put(temp.OpportunityId,temp);
}
}
for(Opportunity temp:trigger.new)
{
if(oppContactmap.get(temp.id)==null)
{
temp.addError('Please add Primary contact role'); // generating the error
primarycontact=false;
}
}
// if error is there then not executing this loop
if(trigger.isUpdate && trigger.isAfter && primarycontact){
RecordType oppRecordType = [SELECT Id
FROM RecordType
WHERE SobjectType = 'Opportunity' AND Name = 'IPL'
LIMIT 1];
list<Opportunity> oppListNew = new list<Opportunity>();
list<Opportunity> oppListold = new list<Opportunity>();
system.debug('........'+trigger.new);
system.debug('........'+trigger.old);
for(Opportunity o : trigger.new){
if(o.RecordTypeId == oppRecordType.Id){
oppListNew .add(o);
}
}
for(Opportunity o : trigger.old){
if(o.RecordTypeId == oppRecordType.Id){
oppListold.add(o);
}
}
leadopportunityemailnotification Opportunitynotificationvarpayment=new leadopportunityemailnotification();
Opportunitynotificationvarpayment.sendemailopportunity(oppListNew ,oppListold);
Opportunitynotificationvarpayment.paymentrecievednotchanged(oppListNew ,oppListold);
}
}
}
While inserting Opporunity
Class.ClosedOpportunityTriggerTest.OpportunityTestmethod: line 39, column 1
Class.ClosedOpportunityTriggerTest.OpportunityTestmethodPaymentConfirm: line 72, column 1
After opportunity insertion.Insert OpportunityContactRole and check once.
Hope it helps you.
Thanks
Varaprasad
Please debug your code and check update is happening due to some Trigger or Workflow
It was throughing the error while insering the Opportunity....Class.ClosedOpportunityTriggerTest.OpportunityTestmethod: line 39, column 1
For Your Reference....