You need to sign in to do that
Don't have an account?
Blake 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;
}
}
@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;
}
}
All Answers
This field "Account_Manager__c" is a mandatory field. Check for this field in the above objects and include this field.
Best Regards,
Vijay
First approach: Second approach:
I hope it will help you.
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
Did you tried my suggested fix? If not then please try and let me know what you are upto afterwards.
Thanks.
//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;
}
}
}
}
Opportunity opp1 = new Opportunity(AccountId = acc.Id, Name = 'testOpp', CloseDate = System.today() ,StageName = 'Customer Won', Amount = 3000 );
Please have a look into below post. I hope that will help you