List<Opportunity> op = [
    SELECT Id,AccountId, External_Id__c, StageName, Sales_Office__c,
           Held_or_Purchased_Model_Lookup__c, Subdivision__c, Lot__c,
           Hold_Expiry_Date__c, Sales_Agent_Lookup__c, Real_Estate_Agent__c
    FROM Opportunity
    WHERE External_Id__c =: externalId

Purchased_Home__c purchasedHome = new Purchased_Home__c (
    Account_Name__c  = op.AccountId,                                                           
    External_ID__c  = op.External_Id__c,                                                                   
    Sales_Office__c  = op.Sales_Office__c,                                                                      
    Model__c  = op.Held_or_Purchased_Model_Lookup__c,                                                                       
    Subdivision__c = op.Subdivision__c,                                                                         
    Lot__c  = op.Lot__c,                                                                             
    Sales_Agent__c  = op.Sales_Agent_Lookup__c,                                                                                
    Real_Estate_Agent__c = op.Real_Estate_Agent__c                                                                               

    insert purchasedHome;

code above works, but below doesn't work...

List <Co_Buyer__c> cobuyer = [
    SELECT First_Name__c, Last_Name__c, Home_Email__c, External_Id__c,Opportunity__c , Purchased_Home__c   
    FROM Co_Buyer__c where id =: op.id
for(Co_Buyer__c c : cobuyer){
    c.id = purchasedHome.Id;     
update cobuyer;

record of Purchased_Home__c is created from opportunity record, when certain conditions are met.

Co_Buyer__c is related list of op, and also needs to be created as purchasedhome's related list when purchasedHome record is created.

I'm having problem with related list part. When PurchasedHome record is created, cobuyers related list does not create with the purchased home record.

Thank you in advance.

  • Does this compile? You seem to be using op.FieldName whereas op is declared as a List<Opportunity>.
    – Jayant Das
    Oct 31, 2018 at 22:13
  • yes it does compile, and purchased home record creates with op's fields
    – Logan H
    Oct 31, 2018 at 22:18
  • It seems you don't have the correct code here then. It's very clear that you have a List<Opportunity> op and then you are using op.Id, op.External_Id__c, etc. elsewhere in your code, which will not compile. Can you make sure this is the exact code?
    – Jayant Das
    Oct 31, 2018 at 22:23
  • List<Opportunity> op is in the same class but different method from purchasedhome and cobuyer
    – Logan H
    Nov 1, 2018 at 1:58

1 Answer 1


I see few issues here.

  1. You have the variable op declared as List<Opportunity> and that you are using it else here as op.Id, op.External_Id__c, which will not compile at first place as the variable is of type List. Assuming that is a typo and that you are using the first element from the list, say as op[0].Id, etc., your first part of code will work fine.

  2. Your query of fetching the Co_Buyer__c is wrong. You have condition specified as Id = op.Id, which will never work. You need to specify the respective Opportunity field on the object on where clause to be able to fetch the record. The record’s Id will never match that to the Opportunity Id.

  3. In the later part of your code, where you are trying to update the Co_Buyer__c records, you are trying this:

    c.id = purchasedHome.Id;

    which won't work because you are trying to update a record's Id field. You can never update a record's Id.

    I think you are trying to link this record to the newly created Purchased_Home__c record, in that scenario you need to use something as below, and then update the records.

    c.Purchased_Home__c = purchasedHome.Id;
  • I tried c.Purchased_Home__c = purchasedHome.Id; before, but didn't do the job...
    – Logan H
    Nov 1, 2018 at 1:44
  • Are you trying to update the record or create? Your question’s heading says update and your code too, but then the question body says create. It’s a bit confusing.
    – Jayant Das
    Nov 1, 2018 at 1:49
  • update the record. Co_Buyer__c only have op info and purchasedHome__c = null. I want to add PurchasedHome__c field and update it.
    – Logan H
    Nov 1, 2018 at 1:57
  • I added details in my answer. You definitely need to review and fix your code here to make it work.
    – Jayant Das
    Nov 1, 2018 at 2:03
  • for(Co_Buyer__c c : cobuyer){ c.Purchased_Home__c = purchasedHome.Name; } update cobuyer;
    – Logan H
    Nov 1, 2018 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.