3

I have created two maps of two different Objects i need to check if the ids in obj2 are in obj1 and if they are missing i need to create that value in the database ( the last part we have a function which will do this) my problem is getting the two objects to check the ids and realize which ones are missing from obj1.

this is what ive got so far..

   List <obj1__c> CheckFieldValue = [SELECT ID, ProductOrder__c, obj2__r.ProductField__r.FieldName__c FROM obj1__c WHERE ProductOrder__c IN: productOrderIds];
   List<obj2__c> CheckFieldLinkValues = [SELECT ID, OrderID__c FROM  obj2__c];

   Map<String, obj1__c> CheckFieldValueLinks = new Map<String, ProductOrderComponentFieldValueLink__c>();
   Map<String, obj2__c> CheckFieldLink = new Map<String, obj2__c>();

   for(obj1__c PCFVL : CheckFieldValue){
        CheckFieldValueLinks.put(PCFVL.obj2__c, PCFVL);
   }

   for(obj2__c PCFL : CheckFieldLinkValues){
       CheckFieldLink.put(PCFL.id, PCFL);
   }
1
  • Are you trying to find what entries exist in CheckFieldLink but not in CheckFieldValueLinks?
    – BarCotter
    Jan 14 '15 at 11:45
4

Use the code below, i have added my code to check map,

 List <obj1__c> CheckFieldValue = [SELECT ID, ProductOrder__c, obj2__r.ProductField__r.FieldName__c FROM obj1__c WHERE ProductOrder__c IN: productOrderIds];
       List<obj2__c> CheckFieldLinkValues = [SELECT ID, OrderID__c FROM  obj2__c];

       Map<String, obj1__c> CheckFieldValueLinks = new Map<String, ProductOrderComponentFieldValueLink__c>();
       Map<String, obj2__c> CheckFieldLink = new Map<String, obj2__c>();

       for(obj1__c PCFVL : CheckFieldValue){
            CheckFieldValueLinks.put(PCFVL.obj2__c, PCFVL);
       }

       for(obj2__c PCFL : CheckFieldLinkValues){
           CheckFieldLink.put(PCFL.id, PCFL);
       }

    //Iterate through 1st Map
    for(String keyId: CheckFieldValueLinks.Keyset()){

           //if 2nd map does not have that key
           if(!CheckFieldLink.keyset().contains(keyId)){
                //Your code here to create that value in the database
           }

    }

Dont forget to accept answer if it was useful. Tia :)

1
  • 1
    Used something very similar to this which worked thank you. Jan 14 '15 at 11:57
1

Not sure about the First for loop but you can avoid second for loop. This will solve lot of CPU processing. After that you can check for missing Id:

   List <obj1__c> CheckFieldValueLinksValues = [SELECT ID, ProductOrder__c, obj2__r.ProductField__r.FieldName__c FROM obj1__c WHERE ProductOrder__c IN: productOrderIds];

   Map<String, obj1__c> CheckFieldValueLinks = new Map<String, ProductOrderComponentFieldValueLink__c>();
   Map<id, obj2__c> CheckFieldLink = new Map<Id,obj2__c>([SELECT ID, OrderID__c FROM  obj2__c]);

   for(obj1__c PCFVL : CheckFieldValueLinksValues){
        CheckFieldValueLinks.put(PCFVL.obj2__c, PCFVL);
   }

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.