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
Blake Miller 11Blake Miller 11 

When I try to test my trigger I get this error: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account_Manager__c]: [Account_Manager__c]

This is my test class: 

@IsTest(SeeAllData=true) 
public with sharing class TestInsertDefaultLineItems {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc', Description='testdesc');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
 
        Opportunity opp1 = new Opportunity (AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today());
            insert opp1;
        
       /* PricebookEntry priceBookEntryNew = new PricebookEntry ();
        Product2 product = new Product2 ();
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook', Description='test');
        insert pb2;      
       List pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true ];*/
        
     PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List <PriceBook2> pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true];
        PriceBook2 pricebooktest = new PriceBook2 ();
        if (pricebookList !=null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true; 
        insert priceBookEntryNew;

        OpportunityLineItem oli = new OpportunityLineItem
            (OpportunityId = opp1.Id,
             PricebookEntryId = priceBookEntryNew.Id,
             Quantity = 1,
             UnitPrice = priceBookEntryNew.UnitPrice,
             
             ServiceDate = System.today()
            );
        insert oli;
    }
}
Best Answer chosen by Blake Miller 11
Amit Chaudhary 8Amit Chaudhary 8
You need to set "Account_manager__c" field on account like below
@IsTest(SeeAllData=true) 
public with sharing class TestInsertDefaultLineItems {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc', Description='testdesc',Account_Manager__c='testcon');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
 
        Opportunity opp1 = new Opportunity (AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today());
            insert opp1;
        
       /* PricebookEntry priceBookEntryNew = new PricebookEntry ();
        Product2 product = new Product2 ();
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook', Description='test');
        insert pb2;      
       List pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true ];*/
        
     PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List <PriceBook2> pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true];
        PriceBook2 pricebooktest = new PriceBook2 ();
        if (pricebookList !=null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true; 
        insert priceBookEntryNew;

        OpportunityLineItem oli = new OpportunityLineItem
            (OpportunityId = opp1.Id,
             PricebookEntryId = priceBookEntryNew.Id,
             Quantity = 1,
             UnitPrice = priceBookEntryNew.UnitPrice,
             
             ServiceDate = System.today()
            );
        insert oli;
    }
}

 

All Answers

Vijaya Kumar RegantiVijaya Kumar Reganti
HI Blake,

This field "Account_Manager__c" is a mandatory field. Check for this field in the above objects and include this field.

Best Regards,
Vijay
Muhammad WasimMuhammad Wasim
I think Account_manager__c is a field in account and it will requires a ContactID. So first of all you would need to insert a contact for Account manager and then passes out its Id as the value of Account_manager__c and insert your account. Or if you want to utilize the single contact then you can update the account's Account_manager__c after inserting it.

First approach:
Contact conObj1 = new Contact();
conObj.lastname = 'Account manager';
insert conObj1;

Account acc= new Account(Name = 'testAcc', Description='testdesc', Account_manager__c=conObj1.id);
insert acc;

Contact conObj = new Contact();
conObj.lastname = 'testcon';
conObj.AccountId = acc.id;
insert conObj;
Second approach:
Account acc= new Account(Name = 'testAcc', Description='testdesc');
insert acc;

Contact conObj = new Contact();
conObj.lastname = 'testcon';
conObj.AccountId = acc.id;
insert conObj;

acc.Account_manager__c = conObj.id;
update acc;

I hope it will help you.

 
Amit Chaudhary 8Amit Chaudhary 8
It look like you have "Account_Manager__c" field which is required field . To Resolve the issue you need to add value for "Account_Manager__c" field.

Please let us know on which object you have "Account_Manager__c" field and what is the data type so that we can help you

 
Blake Miller 11Blake Miller 11
Hello everyone, sorry for the long response time. What I was able to find was Account_Manager_ID__c within the Accounts Object wiht its data type being text(121). 
Muhammad WasimMuhammad Wasim
Hi Blake

Did you tried my suggested fix? If not then please try and let me know what you are upto afterwards. 

Thanks.
Amit Chaudhary 8Amit Chaudhary 8
You need to set "Account_manager__c" field on account like below
@IsTest(SeeAllData=true) 
public with sharing class TestInsertDefaultLineItems {
    static testMethod void validateTrigger() {
       Account acc= new Account(Name = 'testAcc', Description='testdesc',Account_Manager__c='testcon');
       insert acc;

        //Case record in Test method. 
        Contact conObj = new Contact();
        conObj.lastname = 'testcon';
        conObj.AccountId = acc.id;
        insert conObj;
 
        Opportunity opp1 = new Opportunity (AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today());
            insert opp1;
        
       /* PricebookEntry priceBookEntryNew = new PricebookEntry ();
        Product2 product = new Product2 ();
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook', Description='test');
        insert pb2;      
       List pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true ];*/
        
     PricebookEntry priceBookEntryNew = new PricebookEntry();
        Product2 product = new Product2(); 
        PriceBook2 pb2 = new PriceBook2 (Name='Standard priceBook',Description = 'test');
        insert pb2;
        List <PriceBook2> pricebookList = [SELECT Id FROM PriceBook2 WHERE IsStandard = true];
        PriceBook2 pricebooktest = new PriceBook2 ();
        if (pricebookList !=null && pricebookList.size()>0)
            pricebooktest = pricebookList.get(0);
        product.name = 'Test';
        insert product;
        
        priceBookEntryNew.Product2Id = product.Id;
        priceBookEntryNew.PriceBook2Id = pricebooktest.Id;
        priceBookEntryNew.UnitPrice = 20.00;
        priceBookEntryNew.UseStandardPrice = false;
        priceBookEntryNew.isactive = true; 
        insert priceBookEntryNew;

        OpportunityLineItem oli = new OpportunityLineItem
            (OpportunityId = opp1.Id,
             PricebookEntryId = priceBookEntryNew.Id,
             Quantity = 1,
             UnitPrice = priceBookEntryNew.UnitPrice,
             
             ServiceDate = System.today()
            );
        insert oli;
    }
}

 
This was selected as the best answer
Blake Miller 11Blake Miller 11
Yes Muhammad I tried your fix. I got the same error as before after running the test again. 
 
Muhammad WasimMuhammad Wasim
Can you share your trigger code I'd like to test on my dev org.
Blake Miller 11Blake Miller 11
Amit that worked, but now I am getting an error for [StageName]
 
Blake Miller 11Blake Miller 11
trigger InsertDefaultLineItems on OpportunityLineItem (before insert) {
    //System.debug('InsertDefaultLineItems triggered');
    Map<Id, Map<Id, Pricing_Agreement_Line_Item__c>> listItemValueFromContract = new Map<Id, Map<Id, Pricing_Agreement_Line_Item__c>>();
    for (OpportunityLineItem opportunityLineItem : Trigger.new){
        Opportunity opportunity = [SELECT Id, ContractId
                                   FROM Opportunity
                                   WHERE Id=:opportunityLineItem.OpportunityId][0];
        
        
    Id contractId = opportunity.ContractId;
    //Id contractId = opportunity.ContractId;
    if(ContractId != null){
        Map<Id, Pricing_Agreement_Line_Item__c> lineItemMap;
        if(listItemValueFromContract.containsKey(ContractId)){
            lineItemMap = listItemValueFromContract.get(ContractId);
        }else{
            lineItemMap = new Map<Id, Pricing_Agreement_Line_Item__c>();
            listItemValueFromContract.put(ContractId, lineItemMap); 
}
        for(Pricing_Agreement_Line_Item__c lineItem : [SELECT Id, Sales_Price__c, Discount__c, Product__c
                                                        FROM Pricing_Agreement_Line_Item__c
                                                       WHERE contract__c=:ContractId]){
                                                           lineItemMap.put(lineitem.product__c, lineItem);
                                                       }
        if (opportunityLineItem.Discount == null && lineItemMap.containsKey(OpportunityLineItem.Product2Id)){
         //opportunityLineItem.Discount = lineItemMap.get(OpportuntiyLineItem.Product2Id).Discount__c;   
        }
        if (opportunityLineItem.UnitPrice == null && lineItemMap.containsKey(OpportunityLineItem.Product2Id)){
         //opportunityLineItem.UnitPrice = LineItemMap.get(OpportunityLineItem.Product2Id).Sales_Price__c;   
        }
    }
}
}
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- You need to pass value in all required field. like below


       Opportunity opp1 = new Opportunity(AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today() ,StageName = 'Customer Won',                             Amount = 3000 );
 
Blake Miller 11Blake Miller 11
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Only Sellable Products may be added to a Pricebook.: [] This is the next error
 
Amit Chaudhary 8Amit Chaudhary 8
It look like you have some validation rule on PriceBook please update the test data in your test class according to your org.
Please have a look into below post. I hope that will help you
 
Blake Miller 11Blake Miller 11
I have code coverage now! Thank you all