You need to sign in to do that
Don't have an account?
Srinivas223
comparing set and list
I have a set which contains some contact IDs and I have a List which contains Contact records, I want to check the list of contacts that are present in the set and do some logic. I am doing it but I feel what I am doing is wrong. Here is the code snippet. I appreciate your help
mvssArray in the code contains contact IDs and lstcon holds the data of all contact IDs present in mvssArray.
map<id, contact> mapMvssCon = new map<id, contact>();
for (contact con : lstcon){
mapMvssCon.put(con.id, con);
for(id conID : mvssArray) {
if (mapMvssCon.containsKey(conID)){
this is what I am doing in the code to map the values. Please suggest me if I am missing something.
Thank you
trigger IMRORevisionTrigger on iMROrevision__c (before insert, before update, after insert, after update) { set<id> mvssArray = new set<id>(); set<id> NomvssACArray = new set<id>(); set<id> serviceCenterID = new set<id>(); set<id> aircraftIds = new set<id>(); set<id> ImrevisionID = new set<id>(); public datetime Add30Days; public list<contact> lstcon; public list<contact> lstcon1; public list<case> lstCase; public list<aircraft__c> lstAir; if(trigger.isUpdate && (trigger.isafter || trigger.isbefore)){ for(iMROrevision__c lstimr : trigger.new){ if(lstimr.aircraft__c != null) aircraftIds.add(lstimr.aircraft__c); } } case cs = new case(); lstCase = new list<case>(); if(aircraftIds.size()>0){ lstAir = [select id, (select id, MVSS_Contact__c, status__c from Aircraft_Relationships__r where status__c = 'Delivered'),ServiceCenter__c from aircraft__c where id in: aircraftIds]; lstcon1 = new list<contact>(); for(aircraft__c ac : lstAir ){ system.debug('the aircraft id----' + ac.id); for(Aircraft_Relationship__c acRel : ac.Aircraft_Relationships__r){ system.debug('the mvss con 0000' + acRel.MVSS_Contact__c ); if(acRel.MVSS_Contact__c != null) mvssArray.add(acRel.MVSS_Contact__c); if(acRel.MVSS_Contact__c == null ){ NomvssACArray.add(ac.id); } } } } if(mvssArray.size()>0) lstcon = [select name,id,DateMXSurveySent__c from contact where id in : mvssArray ]; if(trigger.isUpdate && trigger.isafter){ for(iMROrevision__c i : trigger.new){ if(i.NumofInvoices__c == 1 && (!i.IsInternational_ServiceCenter__c)){ if(mvssArray.size()>0){ system.debug('777777777' + i.ServiceCenter__c); map<id, contact> mapMvssCon = new map<id, contact>(); for (contact con : lstcon){ mapMvssCon.put(con.id, con); for(id conID : mvssArray) { if (mapMvssCon.containsKey(conID)){ system.debug('---------' + mapMvssCon); system.debug('list contains contact'); if(con.DateMXSurveySent__c != null){ Add30Days = con.DateMXSurveySent__c.addDays(30); system.debug('30 days --' + Add30Days); if( Add30Days<System.now() || con.DateMXSurveySent__c == null ){ system.debug('the sent date ----- ' + con.DateMXSurveySent__c); con.SendMXSurveyToContact__c = true; con.SurveyServiceCenter__c = i.ServiceCenter__c; con.SurveyAircraft__c = i.Aircraft__c; system.debug('the checkbox survey---' + con.SendMXSurveyToContact__c); if(i.Cbadge__c != null ) con.CSRid__c = i.Cbadge__c; // lstcon.add(con); } } } } update lstcon; } // update lstcon; if(NomvssACArray.size()>0){ // create a case if there is no MVSS Contact list<aircraft__c> NoMVssConlstAc = [select id, (select id,ServiceCenter__r.name, ServiceCenter__c,Cbadge__c,NumofInvoices__c from iMROrevisions__r),ServiceCenter__c from aircraft__c where id in: NomvssACArray]; for(aircraft__c acr :NoMVssConlstAc){ // for(iMROrevision__c Imr : acr.iMROrevisions__r){ Id RecordTypeIdCase = Schema.SObjectType.case.getRecordTypeInfosByName().get('Post MX Survey').getRecordTypeId(); cs.Case_Name__c = 'Populate MVSS Contact'; cs.Launch_Date__c = system.today(); cs.Team_Req__c = 'Aftermarket'; cs.Status = 'New'; cs.Business_Focus__c = 'Service'; cs.Communication_Category__c = 'Promotion'; cs.Type = 'Other'; cs.recordTypeid = RecordTypeIdCase; cs.Subject = 'Please Add MVSS Contact for AirCraft Relationships' ; cs.ServiceCenterLookup__c = i.ServiceCenter__c; cs.Aircraft__c = i.Aircraft__c; system.debug('-----------' + cs); lstCase.add(cs); insert cs; system.debug('case records info'+ lstCase); } //insert cs; } } } // if(!lstCase.isempty()) // insert lstCase; } } }
mvssArray in the code contains contact IDs and lstcon holds the data of all contact IDs present in mvssArray.
map<id, contact> mapMvssCon = new map<id, contact>();
for (contact con : lstcon){
mapMvssCon.put(con.id, con);
for(id conID : mvssArray) {
if (mapMvssCon.containsKey(conID)){
this is what I am doing in the code to map the values. Please suggest me if I am missing something.
Thank you
You can either use Map constructor or use the putAll() Map method which accepts an array of SObjects to generate a corresponding Map<Id, Sobject> type. This will eliminate the need for unnnecessary List iteration and calling a put() for every List element individually to populate the Map.
Using the Map constructor the code becomes.
Using the putAll() method is equivalent to invoking the Map constructor and using a SOQL query to populate it directly. Using the putAll() method the code becomes.
You can use either of the above techniques to avoid an additional iteration while populating the mapMvssCon Map. Refer the Apex Developer Guide (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf)on understanding more about Map constructors and putAll() methods.
All Answers
You can either use Map constructor or use the putAll() Map method which accepts an array of SObjects to generate a corresponding Map<Id, Sobject> type. This will eliminate the need for unnnecessary List iteration and calling a put() for every List element individually to populate the Map.
Using the Map constructor the code becomes.
Using the putAll() method is equivalent to invoking the Map constructor and using a SOQL query to populate it directly. Using the putAll() method the code becomes.
You can use either of the above techniques to avoid an additional iteration while populating the mapMvssCon Map. Refer the Apex Developer Guide (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf)on understanding more about Map constructors and putAll() methods.
One more thing is that your business logic, should execute only on specific trigger events however, there is no clear segregation of these events and hence you have the logic executing on every event, which could lead to uncontrolled behaviour.
Hence its best you encapsulate the business logic within a method of a separate Apex class and then invoke them within your trigger code. This helps in the following ways.
- The Apex trigger is not bloated with unnecessary code.
- You exactly understand and know the sequence of execution of your business logic.
- The Apex logic is maintainable and reusable since the same method can be called at multiple places. Moreover if you intend to add more method calls in future, it will cleaner and easier.
Refer the code below.Apex Trigger - IMRORevisionTrigger
Apex Trigger Handler - IMRORevisionTriggerHandler