-2

Can any one help me out with this error :

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId]: [OpportunityId] Class.TestCreateSubscriptionOpptyClone.TestCreateSubscription: line 58, column 1 line 58 is :insert opptyConRolelist;

All the mandatory fields are added in the test code but it still throws an error.

Test Class

@isTest
public class TestCreateSubscriptionOpptyClone 
{

static testMethod void TestCreateSubscription()
{
//Create Account 

Account acc1 = new Account();
acc1.Name='Testing SubscriptionClone';
acc1.BillingStreet='Banjara hills';
acc1.BillingCity='Hyd';
acc1.BillingState='TS';
acc1.BillingPostalCode = '500084';
acc1.BillingCountry = 'India';
acc1.Phone = '100';
acc1.Industry = 'Banking';         
acc1.Type = 'Paid';
acc1.Customer_Type__c = 'Customer';
acc1.Customer_List__c = true;
insert acc1 ;

//Create Contact

contact cc = new contact();
cc.FirstName ='Test Contact Subscription';
cc.LastName ='Opptyclone';
cc.Role__c='Subscription Administrator';
cc.AccountId=acc1.Id;
insert cc;

//Create Opportunity

Opportunity opty = new Opportunity();
opty.Name = 'Test SubscriptionOppty';
opty.StageName ='Proposal/Price Quote';
opty.Probability =60;
opty.LeadSource = 'Subscription Renewal';
opty.CloseDate = System.Today();                
opty.Type = 'Existing Customer';
opty.Assigned_Sales_Engineer__c = null;
opty.Payment_Type__c = 'Order Form';
opty.Update_Complete__c = true;
opty.Partner__c = 'None';
opty.Anchor__c='Non Anchor';
opty.AccountId = acc1.Id;
opty.StageName = 'Closed Won';
//insert opty;

//Create Opportunity Contact Role 
 list<OpportunityContactRole> opptyConRolelist =  new list<OpportunityContactRole>();   
OpportunityContactRole opptyConRole =  new OpportunityContactRole();   
opptyConRole.ContactId = cc.Id;
opptyConRole.OpportunityId = opty.Id;   
opptyConRole.IsPrimary = false;
opptyConRole.Role = 'Subscription Administrator';
opptyConRolelist.add(opptyConRole);
insert opptyConRolelist;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;

//Add oppty line items
  string stdpdId = '01s500000001e6k';
//Create OpportunityLineitem 

 List<OpportunityLineItem> ooliList = new List<OpportunityLineItem>();

//Create Product

product2 p2 = new product2();
p2.Name ='Postgres Plus Enterprise Edition Unicores';
p2.Product_Name1__C='Product - Subscription';
p2.Product_Group__c='PPAS';
p2.IsActive = true;
p2.Description='Postgres Plus Enterprise Edition Unicores';

insert p2;

//Create PriceBookEntry 

PricebookEntry pbey = new PricebookEntry();
 pbey.Product2ID=p2.id;
 pbey.Pricebook2ID=stdpdId;
 pbey.UnitPrice=50; 
 pbey.isActive=true;
insert pbey;

Opportunitylineitem ooli = new Opportunitylineitem();
ooli.OpportunityId = opty.Id;    
ooli.PricebookEntryId = pbey.Id;
ooli.Quantity =4;
//ooli.Product_Code__c='PREE-UCC-001';
//ooli.Unit_of_Measure__c='Unicore';
ooli.Start_Date__c = System.today();
ooli.End_Date__c =  System.today() + 100;
ooli.Type_of_Contract__c='None';
ooli.Term__c =12;
ooli.Type_Of_Product__c='Single Year Subscription';
ooli.Type_of_ARR__c= 'New Addition ARR';
ooliList.add(ooli);
insert ooliList;

opty.UPDATE_COMPLETE__c = true;
update opty;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;

 //Create Product

product2 pr2 = new product2();
pr2.Name ='Postgres Plus Standard Edition Unicores';
pr2.Product_Name1__C='Product - Subscription';
pr2.Product_Group__c='PPAS';
pr2.IsActive = true;
pr2.Description='Postgres Plus Standard Edition Unicores';

insert pr2;

//Create PriceBookEntry 

PricebookEntry pbety = new PricebookEntry();
 pbety.Product2ID=pr2.id;
 pbety.Pricebook2ID=stdpdId;
 pbety.UnitPrice=50; 
 pbety.isActive=true;
insert pbety;

Opportunitylineitem ooli1 = new Opportunitylineitem();
ooli1.OpportunityId = opty.Id;    
ooli1.PricebookEntryId = pbety.Id;
ooli1.Quantity =4;
//ooli.Product_Code__c='PREE-UCC-001';
//ooli.Unit_of_Measure__c='Unicore';
ooli1.Start_Date__c = System.today();
ooli1.End_Date__c =  System.today() + 100;
ooli1.Type_of_Contract__c='None';
ooli1.Term__c =12;
ooli1.Type_Of_Product__c='Single Year Subscription';
ooli1.Type_of_ARR__c= 'New Addition ARR';
ooliList.add(ooli1);
insert ooliList;

opty.UPDATE_COMPLETE__c = true;
update opty;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;


 //update contact roles - to test the subscription product type
        for(OpportunityContactRole r1: opptyConRolelist)
        {
            r1.Role = 'Subscription Administrator';                                              
        }
        update opptyConRolelist;                
        opty.UPDATE_COMPLETE__c = true;                
        update opty;               

        opty.UPDATE_COMPLETE__c = false;
        opty.Contracts_Processed__c =  true;
        update opty;

        opty.UPDATE_COMPLETE__c =  true;           
        update opty;  

}
static testMethod void TestCreateSubscription1()
{
//Create Account 

Account acc1 = new Account();
acc1.Name='Testing SubscriptionClone';
acc1.BillingStreet='Banjara hills';
acc1.BillingCity='Hyd';
acc1.BillingState='TS';
acc1.BillingPostalCode = '500084';
acc1.BillingCountry = 'India';
acc1.Phone = '100';
acc1.Industry = 'Banking';         
acc1.Type = 'Paid';
acc1.Customer_Type__c = 'Customer';
acc1.Customer_List__c = true;
insert acc1 ;

//Create Contact

contact cc = new contact();
cc.FirstName ='Test Contact Subscription';
cc.LastName ='Opptyclone';
cc.Role__c='Subscription Administrator';
cc.AccountId=acc1.Id;
insert cc;

//Create Opportunity

Opportunity opty = new Opportunity();
opty.Name = 'Test SubscriptionOppty';
opty.StageName ='Stage 4:Proposal Delivery';
opty.Probability =60;
opty.LeadSource = 'Subscription Renewal';
opty.CloseDate = System.Today();                
opty.Type = 'Existing Customer';
opty.Assigned_Sales_Engineer__c = null;
opty.Payment_Type__c = 'Order Form';
opty.Partner__c = 'None';
opty.Anchor__c='Non Anchor';
opty.Update_Complete__c = true;
opty.AccountId = acc1.Id;
//opty.StageName = 'Closed Won';
//insert opty;

//Create Opportunity Contact Role 
 list<OpportunityContactRole> opptyConRolelist =  new list<OpportunityContactRole>();   
OpportunityContactRole opptyConRole =  new OpportunityContactRole();   
opptyConRole.ContactId = cc.Id;
opptyConRole.OpportunityId = opty.Id;   
opptyConRole.IsPrimary = false;
opptyConRole.Role = 'Subscription Administrator';
opptyConRolelist.add(opptyConRole);
insert opptyConRolelist;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;

//Add oppty line items
  string stdpdId = '01s500000001e6k';
//Create OpportunityLineitem 

 List<OpportunityLineItem> ooliList = new List<OpportunityLineItem>();

//Create Product

product2 p2 = new product2();
p2.Name ='Postgres Plus Enterprise Edition Unicores';
p2.Product_Name1__C='Product - Subscription';
p2.Product_Group__c='PPAS';
p2.IsActive = true;
p2.Description='Postgres Plus Enterprise Edition Unicores';

insert p2;

//Create PriceBookEntry 

PricebookEntry pbey = new PricebookEntry();
 pbey.Product2ID=p2.id;
 pbey.Pricebook2ID=stdpdId;
 pbey.UnitPrice=50; 
 pbey.isActive=true;
insert pbey;

Opportunitylineitem ooli = new Opportunitylineitem();
ooli.OpportunityId = opty.Id;    
ooli.PricebookEntryId = pbey.Id;
ooli.Quantity =4;
//ooli.Product_Code__c='PREE-UCC-001';
//ooli.Unit_of_Measure__c='Unicore';
ooli.Start_Date__c = System.today();
ooli.End_Date__c =  System.today() + 100;
ooli.Type_of_Contract__c='None';
ooli.Term__c =12;
ooli.Type_Of_Product__c='Single Year Subscription';
ooli.Type_of_ARR__c= 'New Addition ARR';
ooliList.add(ooli);
insert ooliList;

opty.UPDATE_COMPLETE__c = true;
update opty;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;

 //Create Product

product2 pr2 = new product2();
pr2.Name ='Postgres Plus Standard Edition Unicores';
pr2.Product_Name1__C='Product - Subscription';
pr2.Product_Group__c='PPAS';
pr2.IsActive = true;
pr2.Description='Postgres Plus Standard Edition Unicores';

insert pr2;

//Create PriceBookEntry 

PricebookEntry pbety = new PricebookEntry();
 pbety.Product2ID=pr2.id;
 pbety.Pricebook2ID=stdpdId;
 pbety.UnitPrice=50; 
 pbety.isActive=true;
insert pbety;

Opportunitylineitem ooli1 = new Opportunitylineitem();
ooli1.OpportunityId = opty.Id;    
ooli1.PricebookEntryId = pbety.Id;
ooli1.Quantity =4;
//ooli.Product_Code__c='PREE-UCC-001';
//ooli.Unit_of_Measure__c='Unicore';
ooli1.Start_Date__c = System.today();
ooli1.End_Date__c =  System.today() + 100;
ooli1.Type_of_Contract__c='None';
ooli1.Term__c =12;
ooli1.Type_Of_Product__c='Single Year Subscription';
ooli1.Type_of_ARR__c= 'New Addition ARR';
ooliList.add(ooli1);
insert ooliList;

opty.UPDATE_COMPLETE__c = true;
update opty;

 opty.UPDATE_COMPLETE__c = false;
 opty.Contracts_Processed__c =  false;
 update opty;


 //update contact roles - to test the subscription product type
        for(OpportunityContactRole r1: opptyConRolelist)
        {
            r1.Role = 'Subscription Administrator';                                              
        }
        update opptyConRolelist;                
        opty.UPDATE_COMPLETE__c = true;                
        update opty;               

        opty.UPDATE_COMPLETE__c = false;
        opty.Contracts_Processed__c =  true;
        update opty;

        opty.UPDATE_COMPLETE__c =  true;           
        update opty;  

}
}
  • OpportunityId is null at that point - opptyConRolelist, you havent inserted opty – kurunve Feb 23 '16 at 10:12
  • Do yourself (and other SE users) a favour and extract your object creation to methods. It's not easy to debug a 100+ line method, especially when you have no line numbering – mkorman Feb 23 '16 at 18:10
2

Why insert opty is commented ? // Might be you are in middle of debugging and forgot to uncomment it

Opportunity opty = new Opportunity();
opty.Name = 'Test SubscriptionOppty';
opty.StageName ='Proposal/Price Quote';
opty.Probability =60;
opty.LeadSource = 'Subscription Renewal';
opty.CloseDate = System.Today();                
opty.Type = 'Existing Customer';
opty.Assigned_Sales_Engineer__c = null;
opty.Payment_Type__c = 'Order Form';
opty.Update_Complete__c = true;
opty.Partner__c = 'None';
opty.Anchor__c='Non Anchor';
opty.AccountId = acc1.Id;
opty.StageName = 'Closed Won';
//insert opty;

I think opty doesn't have an Id since it is not inserted into db and hence the error.

Please remove the comment from below line and test

//insert opty;

  • :Thanks for your response.When i insert the opportunity ,system started throwing an error as :System.DmlException: Upsert failed. First exception on row 0; first error:CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Channelinsight.CI_SFOpportunityTrigger: execution of AfterInsert caused by: System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Channelinsight.CI_OpportunityTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject (Channelinsight) – SalesforceCRM AccountCRM Feb 23 '16 at 10:19
  • Channelinsight might be some custom object coming from a Managed package and the code internally assumes there will be atleast one Channelinsight. I am not sure about the code but I think it will be like List<Channelinsight> channelInsight = [Select Id from ChannelInsight]; but its not returning anything. Can you verify the initial set of Channelinsight data required for your org ? – karthikselva Feb 23 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.