0

i am inserting 40000+ records one time in my organization. suppose as per my requirement 19543 record is satisfied to my condition. but 1 to 19542 records are cross checked(unsatisfied ) in my condition. so it will take time. how to reduce that time, how to find satisfied records with less time and how can i optimise the my code, here is my code :

    list<emp__c> agsdt= [select id,name,Email__c,empid__c from emp__c];         
   list<org__c> ordt= [select id,name, orgid__c from org__c where id=:Resid];  

     for(emp__C ags:agsdt){          
         for(org__c res:ordt){  
            if(ags.empid__c ==res.orgid__c){

                if(ags.Email__C !=null){                       

                   Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
                   mail.setOrgWideEmailAddressId(owdId);
                   mail.setTargetObjectId(conid);
                   mail.setTreatTargetObjectAsRecipient(false);                       
                   mail.setWhatId(Res.id);
                   mail.setTemplateId(et.id);
                   mail.setToAddresses(new String[] {ags.Email__c}); 
                   mail.setBccSender(false);
                   mail.setUseSignature(false);
                   mail.setReplyTo('test@gmail.com');
                   mail.setSaveAsActivity(false);
                   allmsg.add(mail);

                 }

                  if(ags.Email__C ==null){                     
                        ----

                    }
  • Is there any relationship between emp__c and org__c – umithuckan Nov 10 '16 at 6:47
  • no relationship @umithuckan – Sfdcprgmr Nov 10 '16 at 6:59
  • Then below(sfdcfox) is the best solution :) – umithuckan Nov 10 '16 at 7:53
1

You need to create a map and populate it. Here's what that would look like:

Map<Id, Org__c[]> orgs = new Map<Id, Org__c[]>();
for(Org__c record: ordt) {
    if(orgs.containKey(record.orgid__c)) {
        orgs.get(record.orgid__c).add(record);
    } else {
        orgs.put(record.orgid__c, new Org__c[] { record });
    }
}
for(Emp__c ags: agsdt) {
    if(orgs.containsKey(record.Empid__c)) {
        for(Org__c res: orgs.get(record.Empid__c)) {
             // ags.EmpId__c does equal res.OrgId__c here
        }
    }
}

This method will allow you to iterate over Org__c records that are related to the current Emp__c record in the outer loop.

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.