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
Jerome ScolaJerome Scola 

Can I Insert a Object with Lookup field to Salesforce

I have a custom object named  Event , the basic fields are  EventId,EventName ect.
Another custom object named  Product, it has a lookup field which has relationship with Event

Now, in my local web application, I want to insert a product to saleForce based on a existing event.  
How can I create the relationship between the Event and Product by local code ?  

in other words, what prameter for Product's lookup field  should I pass ?
Best Answer chosen by Jerome Scola
Deepak Kumar ShyoranDeepak Kumar Shyoran
As you mentioned that you have a parent Object Event (Custom Object) and Product is child which have a lookup to Event. So during creating of new Product records you need to pass the Event Id for Product > Event Lookup Field to related Product with Event.

Id eventId= 'a01XXXXXXXXX' ; // Event Id which you want to related with Product.
Product2 prod = new Product2(Name ='Test product' , EventLookup__c = accId , and other mandatory fields  )
insert prod ;

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
As you mentioned that you have a parent Object Event (Custom Object) and Product is child which have a lookup to Event. So during creating of new Product records you need to pass the Event Id for Product > Event Lookup Field to related Product with Event.

Id eventId= 'a01XXXXXXXXX' ; // Event Id which you want to related with Product.
Product2 prod = new Product2(Name ='Test product' , EventLookup__c = accId , and other mandatory fields  )
insert prod ;
This was selected as the best answer
Jerome ScolaJerome Scola
Thank you for your reply. 

 My event Id is 10, the look up api name is GWCEventId__c

  so Product2 prod = new Product2(Name ='Test product' , GWCEventId__c = 10 ,...) ?

I did like this,it told me the type is wrong. can you give me more detail? thanks.
Jerome ScolaJerome Scola
I solved it, the event id should be the salesForces external id.