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
MJRDeveloperMJRDeveloper 

Insert failing

I am getting thefollowing error when trying to insert a new custom object called bid__c. bid__c is essentially a collection of objects called bid_item__c. A master-detail relationship exists between bid and bid_item. New bid items require a reference to a bid. That is the only required field. Any help is appreciated.

 

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

Class.bid_ext.additem: line 27, column 29 External entry point 

 

 

public pagereference additem(){ bid_item__c b = new bid_item__c(); b.setbid__c(bid); insert b; return null; }

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Try changing this:

 

b.bid__r = bid;

 

to

 

b.bid__c = bid.id;

 

All Answers

bob_buzzardbob_buzzard
Can you show us some more of your code? E.g. where bid is initialised?  
MJRDeveloperMJRDeveloper

Here is the entire extension:

 

 

public with sharing class bid_ext { public bid__c bid; public bid_ext(ApexPages.StandardController controller) { bid = (bid__c) controller.getRecord(); } public void setbid(bid__c b){bid = b;} public bid__c getbid(){ if (bid.id == null){ bid = new bid__c();} else{ List<bid__c> b = [select name, id, account__c, Contact__c from bid__c where id = :bid.id limit 1]; bid = b[0]; } return bid; } public List<bid_item__c> items; public List<bid_item__c> getItems() { return items; } public pagereference additem(){ bid_item__c b = new bid_item__c(); b.bid__r = bid; b.name = bid.name; if(b.bid__r.id == bid.id){ insert b;} return null; } public pagereference updatebid(){ update bid; return null; } public PageReference ViewData() { items = [Select id, name FROM bid_item__c WHERE bid_item__c.bid__r.id = :bid.id order by Name]; return null; } public PageReference UpdateRecords() { update items; return null; } }

 

 

 

bob_buzzardbob_buzzard

Try changing this:

 

b.bid__r = bid;

 

to

 

b.bid__c = bid.id;

 

This was selected as the best answer