0
Set<ID> folderIDS =  new Set<ID>();
Map<ID,Custom_Folder__c> foldersToUpdate1 = new Map<ID,Custom_Folder__c>();
Map<ID,Custom_Folder__c> foldersToUpdate2 = new Map<ID,Custom_Folder__c>();

folderIDS.add('a0t63xxxxxxxxxxxx');
folderIDS.add('a0t63xxxxxxxxxxxy');
folderIDS.add('a0t63xxxxxxxxxxxz');

foldersToUpdate1 = getMapFoldersById(folderIDS);
foldersToUpdate2 = getMapFoldersById2(folderIDS);

private static Map<ID,Custom_Folder__c> getMapFoldersById(Set<Id> tmpFolderIDS){
    return new Map<ID,Custom_Folder__c> ([SELECT Id, Student__c From Custom_Folder__c where id in :tmpFolderIDS ]);
}

private static Map<ID,Custom_Folder__c> getMapFoldersById2(Set<Id> tmpFolderIDS){
    return new Map<ID,Custom_Folder__c> ([SELECT Id, Business_Type__c From Custom_Folder__c where id in :tmpFolderIDS ]);
}

From the above code I am getting two different map with same object in the trigger helper but there are two methods in 1st method foldersToUpdate1 is updated and in 2nd method foldersToUpdate2 is updated. I want to merge two map into one so that I can do only one update if the trigger is invoked.

When I debug it is overwriting the map and the output is removing this field Student__c in that map

  • 1
    Why do you even have two separate queries? Just include both fields in one query. – Adrian Larson Aug 27 '18 at 21:16
  • Basically the set of ids as parameters are different in these two methods. How to handle this situation? – Prateek Kumar Aug 27 '18 at 21:25
  • 2
    Your code example contradicts your comments. Please edit your post to better match your scenario if your connections are actually different. – Adrian Larson Aug 27 '18 at 21:29
  • I'd say it looks like you are probably running two different operations on those lists because of the queries returning different fields. Do yourself a favor and just have one query that gets both fields, then split/join your records after. This way you wont "lose" fields when merging maps/sets. – cricketlang Aug 27 '18 at 22:22
1

Apex Sets support a union operation, via the addAll(anotherSet) method.

Rather than performing two queries, you should combine your Id sets into a single set using this method. You can then perform one SOQL query with an Id IN :combinedSet clause. You should include all of the fields you require in your SELECT clause.

You can then iterate over your single Map and implement whatever logic and update mechanic you require.

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.