0

i have the following method:

public static AccountForBIView getAssets() {
    List<AggregateResult> assets = 
            [select License__r.Account__c licenseAccountId,License__r.Account__r.Name licenseAccountName, Product2.family productFamily , sum(limit_1_Value__C) sumLimit
                from Asset 
                Where Limit_1_Name__C = 'Users'
                AND License__r.Account__c != null
                AND product2Id != null
                AND product2.Family != 'NONE'
                group by License__r.Account__c,License__r.Account__r.Name,Product2.family 
                LIMIT 100
                ];

    AccountForBIView acc = new AccountForBIView();
    if(!assets.isEmpty())
    {           
        acc.accId = assets[0].License__r.Account__c;
    }
    return acc;
}

i get an error on the line

acc.accId = assets[0].License__r.Account__c;

the error is

Variable does not exist: License__r

how can i insert the value of License__r.Account_c to an object?

Can i use the alias name to insert to an object? cause i don't know how to insert sum(limit_1_Value__C) to an object either

2

As others have commented, aggregate queries require the data values to be accessed differently. This is because the aggregate results can contain entirely new values such as your sum(limit_1_Value__C) that there needs to be some way to identity. While automatic alias names are generated, it is best to define meaningful names as you have done.

To grab the values you have defined the aliases for, the syntax is very much the same as grabbing values from a map. The get method has this signature:

public Object get(String alias);

so it is up to you to know both the alias name and the type in your code for each value. The type is coded by adding a cast: the type in round brackets. Essentially you are telling the compiler to expect that type.

So for your example:

acc.accId = (Id) assets[0].get('licenseAccountId');

A separate point is that you are only using one of the possibly 100 values returned in the query so there is probably a logical problem in the code. If you know there is only ever zero or 1 values limit 1 would make more sense or if there are multiple values you may be able to select the correct one my adding an order by as well as the limit 1. Or by making the where term more specific.

0

You can access aggregate values like below

acc.accId = assets[0].get('licenseAccountId');

  • You will need an (Id) cast too. – Keith C Dec 21 '17 at 9:59
0

acc.accId = assets[0].(Id).get('licenseAccountId'); Do this please.

  • Please explain why you are giving an answer rather than just a one liner. See How to Answer – Dave Humm Dec 21 '17 at 11:04

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.