0

I'm trying to create a query that returns the Account's Id, Name, BillingState and Phone from an Opportunity however when I do so, I get either:

Illegal assignment from List to Account

Or

Illegal assignment from List to List

This is the Apex code that I'm using

public with sharing class MyController {
    @AuraEnabled
    public static Account getAccountFromOppotunity(Id opportunityId) {
        Account acct = [SELECT Accountid, Account.Name, Account.BillingState, Account.Phone FROM Opportunity WHERE Id = :opportunityId];
        return acct;
    }
}

If I use the above code I get the first error message, and if I try to set it to a List <Account> accts = [...] I get the 2nd one (changing return type as well)

How can I resolve this? This is my first time using Apex so I'm kinda lost and it looks fine according to the docs

1 Answer 1

2

The query is on Opportunity, so the result will be an opportunity object, not account.

@AuraEnabled
public static Account getAccountFromOppotunity(Id opportunityId) {
    Opportunity opp = [SELECT Accountid, Account.Name, Account.BillingState, Account.Phone FROM Opportunity WHERE Id = :opportunityId];
    return opp.Account;
}
0

You must log in to answer this question.

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