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
Chenna4a2Chenna4a2 

Not able to insert contract line items once service contract created with Asset ID

HI All,

 

can anybody help me in this class. Its throwing error message as " Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [PricebookEntryId, Quantity, UnitPrice]: [PricebookEntryId, Quantity, UnitPrice] " when trying to insert service contract lineitems

 

Apex class :

 

public static void createContractLineItems(Set<Id> serviceContacts){
set<ID> assetIDs = new set<ID>();
map<ID,String> ProductsMap = new map<Id,String>();
list<ContractLineItem> contLineitems = new list<ContractLineItem>();
Map<Id,PricebookEntry> priceBookIds = new Map<Id,PricebookEntry>();
Set<Id> priceBooks = new Set<Id>();
List<ServiceContract> serviceCons = [select id ,Asset__c,Pricebook2Id from ServiceContract where id in :serviceContacts];
for(ServiceContract oContract : serviceCons)
{
if(oContract.Asset__c != null)
{
assetIDs.add(oContract.Asset__c);
}
if(oContract.Pricebook2Id != null)
priceBooks.add(oContract.Pricebook2Id);
}
if(assetIDs != null && assetIDs.size() > 0){
map<Id,List<Id>> assetMap = new map<Id,List<Id>>();
Set<Id> productIds = new Set<Id>();
List<id> procutsList ;
Map<Id,Asset__c> assetsMap = new Map<Id,Asset__c>([SELECT ID,SAP_SODO__c,SAP_SODO__r.Service_Product__c FROM Asset__c WHERE ID IN: assetIDs]);
for(ServiceContract oContract : serviceCons){
if(oContract.Asset__c != null && assetsMap.containsKey(oContract.Asset__c) && assetsMap.get(oContract.Asset__c) != null){
Asset__c asset = assetsMap.get(oContract.Asset__c) ;
ContractLineItem lineItem = new ContractLineItem(ServiceContractId = oContract.id);
// lineItem.PricebookEntryId = priceBookIds.get(oContract.Pricebook2Id).id ;
// lineItem.Quantity = 1 ;
// lineItem.UnitPrice = priceBookIds.get(serviceProdId).UnitPrice ;
contLineitems.add(lineItem);
system.debug ( '***********' + contLineitems);

}
}
if(contLineitems != null && contLineitems.size() > 0)
insert contLineitems ;
}
}

}

Best Answer chosen by Admin (Salesforce Developers) 
Tim BarsottiTim Barsotti

A pricebook is different than a pricebookentry. These are two seperate tables. Your contract line items need a pricebookentry. 

 

This line below is not pulling a PriceBookEntry ID, it is pulling a PriceBook ID. You will need to pull back the correct PriceBookEntry ID from the PriceBookEntry table. 

 

// lineItem.PricebookEntryId = priceBookIds.get(oContract.Pricebook2Id).id ; 

 

All Answers

Tim BarsottiTim Barsotti

You will need to specify the PriceBookEntry, quantiy and unit price.

 

PriceBook is different than PriceBookEntry. A PriceBookEntry has a PriceBook associated to it, a currency code, and a product. 

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_pricebookentry.htm

 

PBE is a seperate table you will need to query to find the appropriate entry.  

PriceBookEntry []pbe=[select Id, CurrencyIsoCode, Name from PricebookEntry];

 

 

 

Chenna4a2Chenna4a2

I am querying that too. But we are using Asset ID & creating the Service contract with line items . So i think we are getting different Pricebook for Service Contract & for line items.

 

How can we resolce ????

Tim BarsottiTim Barsotti

A pricebook is different than a pricebookentry. These are two seperate tables. Your contract line items need a pricebookentry. 

 

This line below is not pulling a PriceBookEntry ID, it is pulling a PriceBook ID. You will need to pull back the correct PriceBookEntry ID from the PriceBookEntry table. 

 

// lineItem.PricebookEntryId = priceBookIds.get(oContract.Pricebook2Id).id ; 

 

This was selected as the best answer