0

I have written a trigger helper method that converts to contact and associates the contact with an existing account. The contact owner becomes the owner of the account as shown in this snippet

Now I dont know how to cover this assignment: c.OwnerId = c.Account.OwnerId;

I am getting the error :

Error occured while converting the Lead to a Contact in convertLeadToContact methodSObject row was retrieved via SOQL without querying the requested field: Contact.Account

Any ideas?

Thanks

// contains the list of created contacts after conversion
contList =  [select Id, OwnerId from Contact where Id IN :cidList]; 
            
//update contact owner with related account owner
if(cidList.size() > 0){
    for(Contact c : contList){
          c.OwnerId = c.Account.OwnerId;
    }
    update contList;
}
2
  • Have you read through this canonical QA and its linked resources? If so and you still are stuck, please edit your question to add the test code you have so far - including any pertinent test data setup.
    – Moonpie
    Commented Dec 30, 2021 at 17:16
  • After re-reading, I am unsure what you are asking. Are you asking, as your question title indicates, how to cover that line of code from your test class? Or are you asking, as the body of your question more indicates, about an error in your original code that makes your trigger not functional?
    – Moonpie
    Commented Dec 30, 2021 at 17:46

1 Answer 1

1

To answer the error question...

This is one of those cases where the error correctly tells you what the issue is.

...row was retrieved via SOQL without querying the requested field: Contact.Account

From Apex Developer Guide > Working with SOQL and SOSL Query Results https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_working_with_results.htm :

SOQL and SOSL queries only return data for sObject fields that are selected in the original query. If you try to access a field that was not selected in the SOQL or SOSL query (other than ID), you receive a runtime error, even if the field contains a value in the database.

To rectify:

  1. Find where you have SOQL in your code snippet:

    contList =  [select Id, OwnerId from Contact where Id IN :cidList];
    
  2. Find everywhere you reference fields using the variable which holds the results returned from your query:

    c.OwnerId
    c.Account.OwnerId
    
  3. Compare the items in #2 to what you queried in #1:

    OwnerId
    

    Did I query for OwnerId? YES

    ...

    Account.OwnerId
    

    Did I query for Account.OwnerId? NO

  4. Add Account.OwnerId to your query.

3
  • Thanks for that. I managed to update the test based on 3.
    – Pauline
    Commented Jan 2, 2022 at 10:18
  • Great! | I was wondering how #3 helped you, when I realized that from the Q/A screen that the last two numbered items both appeared as "3." But when I am in edit mode for this answer: A) the last numbered item reads as "4." and B) the 2nd item in #3 lines up with the indention of the 1st item. Somehow after re-saving, the last numbered item now appears (for me) correctly as "4."; but I can't get the other items to line up.
    – Moonpie
    Commented Jan 2, 2022 at 11:05
  • Removed the horizontal line and things line up vertically now. Sorry - that just bothered the heck out of me.
    – Moonpie
    Commented Jan 2, 2022 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .