1

Recently I worked on a requirement where I had to return List after adding results from 2 different queries in specific format because the managed class method overriding only returns list of objects.

List<AggregateResult> R1=[SELECT Count(name) C1 from Sobject1__c];

List<AggregateResult> R2=[SELECT Count(name) C2 from Sobject2__c];

I want to return results as below

(AggregateResult:{C1=5},{C2=6})

If I merge both then result returned is (AggregateResult:{C1=5},AggregateResult:{C2=6}) and second part is ignored while tool processes response.

At the moment I am achieving this by writing 2 separate classes and processing separately in my configurator tool. I am looking for options if this can be done.

PS: I can't change it to map or something else as this overridden method does only returns list of object which I'm using in a managed tool in UI.

Let me know if more details are needed.

  • 2
    I'm not sure I quite understand the difference between the result that you're getting, and the result that you want. They both look like a List<AggregateResult> to me. Can you go into more detail about what the difference is, and what your other "tool" is? I'm particularly interested in why your "tool" is ignoring part of your input. – Derek F Apr 25 '18 at 12:38
  • I'm trying to explain in brief about whole scenario. CPQ tool which has a functionality of APEX based lookups, using which I can dynamically pass some attribute from Cart UI and fetch the results. I have 2 UI attributes which I need to populate from different aggregate results C1, C2. Challenge here is if I use same Query in set value rule then rule action is something like: Set Value of Attr1= Lookup(ApexClass):C1 Set Value of Attr2= Lookup(ApexClass):C2 Now the configurator is expecting one object to be returned with specified format hence ignoring the second from the merged list. – shukla yogesh Apr 25 '18 at 13:24
  • @YogeshShukla Do they need to be AggregateResults? If so, do they need to have specific field names or do they just need to be part of the same object? – IllusiveBrian Apr 25 '18 at 14:02
  • @IllusiveBrian No, Not necessarily AggregateResult, But they should be list of object in specified format ie (ABC:{C1=5},{C2=6}) should be fine. Also name could be anything same I can use in rules. I used aliases earlier to distinguish expr0 outputs from both queries in merged result. – shukla yogesh Apr 25 '18 at 14:10
1

You may be able to create a holding object to pass to the library that has the information you want:

public class Data
{
    public integer C1;
    public integer C2;
}

List<AggregateResult> R1=[SELECT Count(name) C1 from Sobject1__c];
List<AggregateResult> R2=[SELECT Count(name) C2 from Sobject2__c];

Data datum = new Data();
datum.C1 = integer.valueOf(R1[0].get('C1'));
datum.C2 = integer.valueOf(R2[0].get('C2'));
lib.invoke(datum); 
  • I didn't get what R1[0].put('ID', R2[0].get('C2')); will do. Though I tried and got error: EXCEPTION_THROWN [49]|System.SObjectException: Illegal assignment from Integer to Id – shukla yogesh Apr 25 '18 at 14:45
  • 1
    @YogeshShukla You may need to convert it to a string. Anyway, I took out that section since I think using a custom class is the right answer here. – IllusiveBrian Apr 25 '18 at 14:47
  • Thanks for inputs @IllusiveBrian! Actually in my class I'm overriding managed Class method which should have this logic and return me the List of objects. Not sure how to implement this logic in my case. It seems it is quite difficult to achieve. I can continue with my two separate class approach. – shukla yogesh Apr 25 '18 at 14:54
  • 1
    @YogeshShukla Custom classes work the same way as other objects, so you can put them in a List if you need to. If you have another question, you can feel free to ask another question on the site. – IllusiveBrian Apr 25 '18 at 14:59
  • I will give a try of this and see. Thanks for help. – shukla yogesh Apr 25 '18 at 15:00

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.