private static void validateBeforeUpdateLockDown(Map<Id,Med_Question_Section_Mapper__c> newMQSecMapperMap, List<Med_Question_Section_Mapper__c> oldMQSecMapperList) {
    for(Med_Question_Section_Mapper__c cObject : oldMQSecMapperList) {
        Med_Question_Section__c parent = [SELECT External_Id__c, Transaction_ID__c FROM Med_Question_Section__c WHERE ID =: cObject.Med_Question_Section__c];
        String errorMsg = trip_ValidationService.validateLockDown(parent.External_Id__c, parent.Transaction_ID__c)
  • Can you please confirm newMQSecMapperMap and oldMQSecMapperList will have same Ids? – manish porwal Sep 13 at 11:40
  • yes manish these are IDs – Kid Sep 13 at 11:44

Following Changes i did in your code

  • Create a separate Set<ID> of ids to store cObject.Med_Question_Section__c
  • Write SOQL to get the records using a set of Ids.
  • Create a map to store Ids, with record Map<Id,Med_Question_Section__c> mapOfIdToRecord = new Map<Id,Med_Question_Section__c>();
  • At the last for loop you will get the record from Map.

Set<Id> setOfIds = new Set<Id>();

    for(Med_Question_Section_Mapper__c cObject : oldMQSecMapperList) {

    Map<Id,Med_Question_Section__c> mapOfIdToRecord = new Map<Id,Med_Question_Section__c>();

    for(Med_Question_Section__c parent = [SELECT Id,External_Id__c, Transaction_ID__c FROM Med_Question_Section__c WHERE ID =: cObject.Med_Question_Section__c]){

    for(Med_Question_Section_Mapper__c obj : oldMQSecMapperList){
        Med_Question_Section__c parent = mapOfIdToRecord.get(obj.Med_Question_Section__c);
  • thanks Sarvesh this is helpful. – Kid Sep 13 at 11:50
  • If this answer solved the question, please mark the answer solved to close the question. thanks! – Sarvesh Sep 13 at 11:53
  • @sarveshkumar Begging for votes and accepted answers is considered very rude. – Derek F Sep 13 at 13:42

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.