0

So I have the below code:

Map<Id, String> sMap = new Map<Id, String>();

for(CKSW_BASE__Service__c s : Trigger.new)
{
    if(s.VS_Site_Survey__c != null) sMap.put(s.VS_Site_Survey__c, 'VS_Site_Survey__c');
    if(s.VS_Permit__c != null && s.CKSW_BASE__Account__c == '') sMap.put(s.VS_Permit__c, 'VS_Permit__c');
    if(s.VS_Installation__c != null && s.CKSW_BASE__Account__c == '') sMap.put(s.VS_Installation__c, 'VS_Installation__c');
    if(s.Post_Install__c != null && s.CKSW_BASE__Account__c == '') sMap.put(s.Post_Install__c, 'Post_Install__c');
}

The result is a map that contains the ID of a record to query, and the name of the sobject to look for the record in.

Eg:   (the denoted string is the sobject we plan to query from)
01ZA0000000A001, VS_Site_Survey__c 
01ZA0000000A002, VS_Permit__c
01ZA0000000A003, VS_Installation__c
01ZA0000000A004, VS_Permit__c
01ZA0000000A005, VS_Post_Install__c
01ZA0000000A006, VS_Site_Survey__c
01ZA0000000A007, VS_Post_Install__c

However, I cannot figure out if it's even possible to query using a map built in this way. Is it possible to build a dynamic soql query which, as it passes through the list, can query properly using a map like this?

I'm trying to return the value of one field from each of the above.

This below query is not formatted right, but I'm hoping it communicates clearly what I am to create:

The ID in this next map is the ID of the CKSW_BASE__Service__c we are planning to update.

Map<Id, AccountID> = SELECT Account__c FROM objectDenotedInMap WHERE Id in : sMap.keySet();

I cannot figure out the 'objectDenotedInMap' part of the query.

The goal was to only use a single SOQL query instead of four individual ones. I could populate four individual sets of ID's and then do four queries after populating the sets, and then iterate through lists returned by those sets, but I was hoping to not have to do that (also I wanted to try to make the code as refined as possibly hopefully).

Is my approach even doable? What should the proper approach here be if not?

I found this: Why I can not use Map in dynamic query

So I suspect it's possible, but I'm just don't know HOW to build it yet. Either that or I misunderstood what the answer stated.

  • Are all of these queries targeting the same object in the database? – battery.cord Oct 5 '16 at 16:42
  • @battery.cord Each query targets whatever object is denoted by the string in the map. So it won't always be the same. – Genko Oct 5 '16 at 16:44
  • 3
    You can only query one object per soql query, so you'll need to split them into multiple queries. The only exception might be if theyre all related, you would be able to create some nested soql queries, but that would likely add a level of complexity which would not be desirable. – battery.cord Oct 5 '16 at 16:47
  • @battery.cord I was worried that would be the case. I'll do the multiple queries and then iterate through each. Thanks! – Genko Oct 5 '16 at 16:58
1

Answer:

Per https://salesforce.stackexchange.com/users/10382/battery-cord :

What I am trying to do is not possible.

You can only query one object per soql query, so you'll need to split them into multiple queries. The only exception might be if theyre all related, you would be able to create some nested soql queries, but that would likely add a level of complexity which would not be desirable.

  • If you're going to post an answer that only summarizes comments, you should probably mark it as a wiki. – Adrian Larson Oct 5 '16 at 22:41
  • 1
    @AdrianLarson - noted, and I updated it to be marked as wiki – Genko Oct 7 '16 at 18:58

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.