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
Varsha JethwaniVarsha Jethwani 

how can we do late binding of variable in soql query

for(Contact ThisContact: contactList){
        tempGender = ThisContact.Salutation;

        List<npct1_FC_Gender__mdt> FCGenderList =new List<npct1_FC_Gender__mdt>
                                                           ([SELECT npct1_Gender_Pick__c, npct1_Addressee_Salutation__c
                                                            FROM npct1_FC_Gender__mdt
                                                            WHERE npct1_Gender_Pick__c = :tempGender
                                                            ]);
        for(npct1_FC_Building_Block__mdt ThisBuilingBlock :FCBuildingBlockList)
        {
    
           resultString = FCGenderList.get('npct1_Gender_Pick__c') +resultString;
           // further code
        }
 }
I know I can't write list in for loop, what would be another way to implement this scenario where I have to pass the variable value in where clause and I have another for loop where I have to retrieve value from that list accordingly to tempGender value.
 
McCuboMcCubo
Hi Varsha,
your code is incomplete, so I don't fully understand what you are trying to achieve, but I using a Set and a Map variables would help you, something like this:
// Distinct list of salutations
Set<String> salutations = new Set<String>();
for (Contact c : contactList) {
    salutations.add(c.Salutation);
}
Map<String, npct1_FC_Gender__mdt> valuesByGender = new Map<String, npct1_FC_Gender__mdt>();

for (npct1_FC_Gender__mdt fcGender : [SELECT npct1_Gender_Pick__c, npct1_Addressee_Salutation__c FROM npct1_FC_Gender__mdt WHERE npct1_Gender_Pick__c IN :salutations]) {
    valuesByGender.put(fcGender.npct1_Gender_Pick__c, fcGender);
}

for (npct1_FC_Building_Block__mdt builingBlock :FCBuildingBlockList) {
    resultString = valuesByGender.get(builingBlock.PROPERTY).npct1_Addressee_Salutation__c;
}
Let me know if it helps or you need further guidance.