多个AggregateResult查询

问题描述 投票:1回答:1

大家好,我目前正在尝试在同一查询或结果中加入两个对象。我的问题是,是否有可能显示或调试来自两个不同对象的字段A的总和+字段B的总和。这是我正在处理的示例:顺便说一句,我非常感谢您的宝贵时间和提出的意见,如果我犯了一个错误,请告诉我,谢谢。

public static void example() {

   String sQueryOne;
   String sQueryTwo;
   AggregateResult[] objOne;
   AggregateResult[] objTwo;

   //I tried to save the following querys into a sObject List
   List<SObject> bothObjects = new List<SObject>();

   sQueryOne = 'Select Count(Id) records, Sum(FieldA) fieldNA From Lead';
   objOne = Database.query(sQueryOne);
   sQueryTwo = 'Select Count(Id) records, Sum(FieldA) fieldNB From Opportunity';
   objTwo = Database.query(sQueryTwo);

   bothObjects.addAll(objOne);
   bothObjects.addAll(objTwo);

   for(sObject totalRec : bothObjects) {
       //There's a Wrapper(className) I created which contains some variables(totalSum)
       className finalRes = new className();
       finalRes.totalSum = (Integer.valueOf(fieldNA)) + (Integer.valueOf(fieldNB));
       System.debug('The sum is: '+finalRes.totalSum);

例如,如果我使用先前的变量finalRes.totalSum调用系统调试,它只是显示重复的第一个值(fieldNA)。>>

以下调试显示了我想要求和的sObject List的当前值,例如,FIELD0 =来自线索,FIELD0 =来自机会。

Debug bothObjects variable

   }
}

[大家好,我目前正在尝试在同一查询或结果中合并两个对象。我的问题是,是否有可能显示或调试来自两个不同对象的字段A的总和+字段B的总和。 ...

salesforce apex apex-code
1个回答
1
投票

您通过调用AggregateResult访问get('columnAlias')中的列。如果您未指定别名,它们将被SF自动编号为expr0expr1 ...如有疑问,您可以随时使用System.debug(results);

© www.soinside.com 2019 - 2024. All rights reserved.