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.