function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hramanihramani 

Iterating over a Map and generating a SOQL query.

Iterating over a Map and generating a SOQL query.

    private class WrapperMergeFieldsPreviewCampaign{
        string objectRelationShipName;
        string objectRelationshipType;
        string fieldName;
        string fieldValue;
    }

    Map<String, MergeFieldsPreviewCampaign> mergeFieldDetailsMap =  new Map<String, MergeFieldsPreviewCampaign>();
    MergeFieldsPreviewCampaign mfp = new MergeFieldsPreviewCampaign();


    mergeFieldDetailsMap.put('Account.Id',mfp.objectRelationShipName = 'Account');
    mergeFieldDetailsMap.put('Account.Id',mfp.objectRelationshipType = 'Parent');
    mergeFieldDetailsMap.put('Account.Id',mfp.fieldName = 'Id');
    mergeFieldDetailsMap.put('Account.Id',mfp.fieldValue = null);

    mergeFieldDetailsMap.put('Account.Name',mfp.objectRelationShipName = 'Account');
    mergeFieldDetailsMap.put('Account.Name',mfp.objectRelationshipType = 'Parent');
    mergeFieldDetailsMap.put('Account.Name',mfp.fieldName = 'Name');
    mergeFieldDetailsMap.put('Account.Name',mfp.fieldValue = null);

    mergeFieldDetailsMap.put('Contact.Id',mfp.objectRelationShipName = 'ReportsTo');
    mergeFieldDetailsMap.put('Contact.Id',mfp.objectRelationshipType = 'Parent');
    mergeFieldDetailsMap.put('Contact.Id',mfp.fieldName = 'Id');
    mergeFieldDetailsMap.put('Contact.Id',mfp.fieldValue = null);

    mergeFieldDetailsMap.put('Contact.LastName',mfp.objectRelationShipName = 'ReportsTo');
    mergeFieldDetailsMap.put('Contact.LastName',mfp.objectRelationshipType = 'Parent');
    mergeFieldDetailsMap.put('Contact.LastName',mfp.fieldName = 'Id');
    mergeFieldDetailsMap.put('Contact.LastName',mfp.fieldValue = null);

    mergeFieldDetailsMap.put('Contact_Child__c.Id',mfp.objectRelationShipName = 'ReportsTo');
    mergeFieldDetailsMap.put('Contact_Child__c.Id',mfp.objectRelationshipType = 'Child');
    mergeFieldDetailsMap.put('Contact_Child__c.Id',mfp.fieldName = 'Id');
    mergeFieldDetailsMap.put('Contact_Child__c.Id',mfp.fieldValue = null);

    mergeFieldDetailsMap.put('Contact_Child__c.Name',mfp.objectRelationShipName = 'Contact_Child__c');
    mergeFieldDetailsMap.put('Contact_Child__c.Name',mfp.objectRelationshipType = 'Child');
    mergeFieldDetailsMap.put('Contact_Child__c.Name',mfp.fieldName = 'Name');
    mergeFieldDetailsMap.put('Contact_Child__c.Name',mfp.fieldValue = null);

    OUTPUT OF SYSTEM.DEBUG(mergeFieldDetailsMap);

    Account.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=Account, objectRelationshipType=Parent]
    Account.Name=MergeFieldsPreviewCampaign:[fieldName=Name, fieldValue=null, objectRelationShipName=Account, objectRelationshipType=Parent]
    Contact.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=ReportsTo, objectRelationshipType=Parent]
    Contact.LastName=MergeFieldsPreviewCampaign:[fieldName=LastName, fieldValue=null, objectRelationShipName=ReportsTo, objectRelationshipType=Parent]
    Contact_Child__c.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=Contact_Child__c, objectRelationshipType=Child]
    Contact_Child__c.Name=MergeFieldsPreviewCampaign:[fieldName=Name, fieldValue=null, objectRelationShipName=Contact_Child__c, objectRelationshipType=Child]

    There is a wrapper where I have four string fields and a Map<String, MergeFieldsPreviewCampaign> where I have allocated values for Key and Value in the above code.

    I would like to run this map (mergeFieldDetailsMap) through a loop and generate a query like below. For Contact and Parent of Contact (something like Account) , we could generate a single query and store it in a String something like below.
  • Select Id,Name,Account.Name,AccountId from Contact   
  • Select Id,Name from Contact_Child__c
    Please help.
 
hramanihramani
I’m actually not hardcoding. I’m dynamically generating these values. For understandablity, I hardcoded these values here.
One thing I’m struggling with is generating the query String using the values from the map.
Assuming that the Map variable (mergeFieldDetailsMap) contains the below output.
From the below result, I want to generate the query as shown below. Please help.

OUTPUT OF SYSTEM.DEBUG(mergeFieldDetailsMap);

    Account.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=Account, objectRelationshipType=Parent]

    Account.Name=MergeFieldsPreviewCampaign:[fieldName=Name, fieldValue=null, objectRelationShipName=Account, objectRelationshipType=Parent]

    Contact.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=ReportsTo, objectRelationshipType=Parent]

    Contact.LastName=MergeFieldsPreviewCampaign:[fieldName=LastName, fieldValue=null, objectRelationShipName=ReportsTo, objectRelationshipType=Parent]

    Contact_Child__c.Id=MergeFieldsPreviewCampaign:[fieldName=Id, fieldValue=null, objectRelationShipName=Contact_Child__c, objectRelationshipType=Child]

    Contact_Child__c.Name=MergeFieldsPreviewCampaign:[fieldName=Name, fieldValue=null, objectRelationShipName=Contact_Child__c, objectRelationshipType=Child]

QUERIES TO BE GENERATED : 

    Select Id,Name,Account.Name,AccountId from Contact
    Select Id,Name from Contact_Child__c