function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shruthi NarsiShruthi Narsi 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId__c]: [OpportunityId__c]

I have written test class to sync quote. Test class failed. Below is the error

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId__c]: [OpportunityId__c]


@isTest
public class QuoteLineItemTestClass {

@TestSetup private static void setUpData(){

        Quotes__c a = New Quotes__c(Name='Test');
        insert a;

        Opportunities__c o = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');

        insert o;

        //....Create Products, pricebookentries, etc needed for the QLI and the OLI

        OpportunityLineItem__c oli = New OpportunityLineItem__c();

        insert oli;

        Quotes__c q = New Quotes__c();

        insert q;

        QuoteLineitem__c qli = New QuoteLineitem__c();

        insert qli;

    }

    static testMethod void  basicTest() {

         QuoteLineitem__c qli = [Select Id,Name,QuotesId__c From QuoteLineitem__c]; //We know we created one and only one

        qli.QuotesId__c = 'Test2'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];

        system.assertEquals('check',oli.Product2Id__c, 'The field was not updated when it should have been');

    }

    static testMethod void  basicTest_NoProductMatch() {
        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New  QuoteLineitem__c();
        insert qli;

        qli.QuotesId__c = 'Test2'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];

        system.assertNotEquals('True',oli.Name,'The field was updated when the product did not match the updated QLI');

    }


}
Foram Rana RForam Rana R
Hi Shruthi,

oli.OpportunityId__c =  o.Id;
Please above line before OpportunityLineItem__c insert.

Thanks,
Foram Rana
Shruthi NarsiShruthi Narsi
@isTest
public class QuoteLineItemTestClass {

@TestSetup private static void setUpData(){

        Quotes__c a = New Quotes__c(Name='Test');
        insert a;

        Opportunities__c o = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');

        insert o;

        //....Create Products, pricebookentries, etc needed for the QLI and the OLI

        
       OpportunityLineItem__c oli = New OpportunityLineItem__c();
         oli.OpportunityId__c = o.Id;
        insert oli;
    
        Quotes__c q = New Quotes__c();

        insert q;

        QuoteLineitem__c qli = New QuoteLineitem__c();

        insert qli;

    }

    static testMethod void  basicTest() {

         QuoteLineitem__c qli = [Select Id,Name,QuotesId__c From QuoteLineitem__c]; //We know we created one and only one

        qli.QuotesId__c = 'Test2'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];

        system.assertEquals('check',oli.Product2Id__c, 'The field was not updated when it should have been');

    }

    static testMethod void  basicTest_NoProductMatch() {
        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New  QuoteLineitem__c();
        insert qli;

        qli.QuotesId__c = 'Test2'; //This is what your code looks to compare if it should run;

        update qli; //This should fire your code

        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];

        system.assertNotEquals('True',oli.Name,'The field was updated when the product did not match the updated QLI');

    }



}
Foram Rana RForam Rana R
still getting the same error  ?

 
Shruthi NarsiShruthi Narsi
User-added image
Shruthi NarsiShruthi Narsi
I have updated the code but getting the errors in the above screen shot
@isTest
public class QuoteLineItemTestClass {
    
    @TestSetup private static void setUpData(){
        
        Quotes__c a = New Quotes__c(Name='Test');
        insert a;
        
        Opportunities__c o = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');
        
        insert o;
        
        //....Create Products, pricebookentries, etc needed for the QLI and the OLI
        
        
        OpportunityLineItem__c oli = New OpportunityLineItem__c();
        oli.Name = 'Test';
        oli.OpportunityId__c = o.Id;
        insert oli;
        
        Quotes__c q = New Quotes__c();
        q.Name = 'Test';
        insert q;
        
        QuoteLineitem__c qli = New QuoteLineitem__c();
        qli.Name ='Test';
        qli.QuotesId__c = q.Id;
        insert qli;
        
    }
    
    static testMethod void  basicTest() {
        Quotes__c quo = New Quotes__c();
        quo.Name = 'Test';
        insert quo;
        
        QuoteLineitem__c qli = [Select Id,Name,QuotesId__c From QuoteLineitem__c]; //We know we created one and only one
        
        qli.QuotesId__c = quo.Id; //This is what your code looks to compare if it should run;
        
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];
        
        
    }
    
    static testMethod void  basicTest_NoProductMatch() {
        
        Quotes__c quo1 = New Quotes__c();
        quo1.Name = 'Test';
        insert quo1;
        
        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New  QuoteLineitem__c();
        qli.Name = 'test';
        insert qli;
        
        qli.QuotesId__c = qou1.Id; //This is what your code looks to compare if it should run;
        
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];
        
    }
}
Foram Rana RForam Rana R
Please Use Below :
 
@isTest
public class QuoteLineItemTestClass {
    
    @TestSetup private static void setUpData(){
        
        Quotes__c a = New Quotes__c(Name='Test');
        insert a;
        
        Opportunities__c o = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');
        
        insert o;
        
        //....Create Products, pricebookentries, etc needed for the QLI and the OLI
        
        
        OpportunityLineItem__c oli = New OpportunityLineItem__c();
        oli.Name = 'Test';
        oli.OpportunityId__c = o.Id;
        insert oli;
        
        Quotes__c q = New Quotes__c();
        q.Name = 'Test';
        insert q;
        
        QuoteLineitem__c qli = New QuoteLineitem__c();
        //qli.Name ='Test';
        qli.QuotesId__c = q.Id;
        insert qli;
        
    }
    
    static testMethod void  basicTest() {
        Quotes__c quo = New Quotes__c();
        quo.Name = 'Test';
        insert quo;
        
        QuoteLineitem__c qli = [Select Id,Name,QuotesId__c From QuoteLineitem__c]; //We know we created one and only one
        
        qli.QuotesId__c = quo.Id; //This is what your code looks to compare if it should run;
        
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];
        
        
    }
    
    static testMethod void  basicTest_NoProductMatch() {
        
        Quotes__c quo1 = New Quotes__c();
        quo1.Name = 'Test';
        insert quo1;
        
        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New  QuoteLineitem__c();
        //qli.Name = 'test';
        insert qli;
        
        qli.QuotesId__c = quo1.Id; //This is what your code looks to compare if it should run;
        
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];
        
    }
}

mark it as best answer if you resolve the issue.

Thanks,
Foram rana
 
Shruthi NarsiShruthi Narsi
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId__c]: [OpportunityId__c]
Shruthi NarsiShruthi Narsi
again im getting the same issue
Foram Rana RForam Rana R
line number ?
Shruthi NarsiShruthi Narsi
Line 15 I am getting this error

@isTest
public class QuoteLineItemTestClass {
    
    @TestSetup private static void setUpData(){
        
        
  Opportunities__c o = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');
         insert o;
       
        Quotes__c a = New Quotes__c(Name='Test');
        
        insert a;
       
       OpportunityLineItem__c oli = New OpportunityLineItem__c();
        oli.Name = 'Test';
        oli.OpportunityId__c = o.Id;
        insert oli;
        
        //....Create Products, pricebookentries, etc needed for the QLI and the OLI
        
        Quotes__c q = New Quotes__c();
        q.Name = 'Test';        
        insert q;
        
        QuoteLineitem__c qli = New QuoteLineitem__c();
        qli.QuotesId__c = q.Id;
        insert qli;        
    }
    
    static testMethod void  basicTest() {
        Quotes__c quo = New Quotes__c();
        quo.Name = 'Test';
        insert quo;
    
        Opportunities__c o1 = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');
        insert o1;
        
        QuoteLineitem__c qli = [Select Id,Name,QuotesId__c From QuoteLineitem__c]; //We know we created one and only one
        
        qli.QuotesId__c = quo.Id; //This is what your code looks to compare if it should run;
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was updated appropriatly
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];       
        
    }
    
    static testMethod void  basicTest_NoProductMatch() {
        
        Quotes__c quo1 = New Quotes__c();
        quo1.Name = 'Test';
        insert quo1;
        
        Opportunities__c o1 = New Opportunities__c(Stage__c = 'prospecting',Close_Date__c = date.today(),Name = 'test');
        insert o1;

        //Negative Use case
        //Create a QLI that does not have a corresponding OLI with same product
        QuoteLineitem__c qli = New  QuoteLineitem__c();
       qli.QuotesId__c = quo1.Id;
        insert qli;
        
        qli.QuotesId__c = quo1.Id; //This is what your code looks to compare if it should run;
        update qli; //This should fire your code
        
        //Query for OLI and assert the field was NOT updated since Product match was not found (quoery requirement in your code)
        OpportunityLineItem__c oli = [Select Name,OpportunityId__c From OpportunityLineItem__c];
        
    }
}