You need to sign in to do that
Don't have an account?
inbox outbox 7
I need help with refactoring this code?
Below code is not following the best practices, governor limits, readability and also maintainability. Also I don't have complete requirement details.
Please help.
I have two methods below.
1st Method:
This method is meant to find Contacts associated with the Institution (Account) on an Event record, then create Support Staff and/or Primary Support Staff records to link those Contacts to the Event
Second Method:
This method checks that a user has registered for a parent Event prior to registering for a child Event. If they have not, an error is returned on the child Event Registration record.
Please help, it is urgent.
Please help.
I have two methods below.
1st Method:
This method is meant to find Contacts associated with the Institution (Account) on an Event record, then create Support Staff and/or Primary Support Staff records to link those Contacts to the Event
public static void AddPrimaryContacts(Map<Id, Event__c> oldTrgMap, Map<Id, Event__c> newTrgMap) { Map<Id,Event__c> eventsMap = new Map<Id,Event__c>(); List<Support_Staff__c> SSList = new List<Support_Staff__c> (); List<Support_Staff__c> SSListFinal = new List<Support_Staff__c> (); for (Event__c e : newTrgMap.values()) { if (e.Institution__c != '' && e.Institution__c != oldTrgMap.get(e.Id).Institution__c) { eventsMap.put(e.Institution__c, e); } } if (!eventsMap.isEmpty()) { List<Contact> PrimaryContactsList = [ SELECT Id, hed__Primary_Organization__c FROM Contact WHERE hed__Primary_Organization__c IN :eventsMap.keySet() ]; for (Contact c : PrimaryContactsList) { Event__c e = eventsMap.get(c.hed__Primary_Organization__c); if (e != null) { Support_Staff__c SS = new Support_Staff__c( Contact__c = c.Id, Event__c = e.Id, RecordTypeId = ExternalRecordtypeId ); SSlist.add(SS); } } for (Support_Staff__c staff : SSlist) { Boolean aloneSS = true; for (Support_Staff__c SS1 : SSlist) { if (staff.Event__c == SS1.Event__c) { aloneSS = false; } } if (!aloneSS) { staff.Primary__c = true; SSListFinal.add(staff); } else { SSListFinal.add(staff); } } try { insert(SSListFinal); } catch (Exception ex) { System.debug(' :: UBC_EventTriggerHelper - AddPrimaryContacts :: ' + ex.getMessage() + ' Trace ' + ex.getStackTraceString()); } } }
Second Method:
This method checks that a user has registered for a parent Event prior to registering for a child Event. If they have not, an error is returned on the child Event Registration record.
public static void ValidateParentRegBreakout(List<Event_Registration__c> newTrgLst, Map< ID , Event_Registration__c> newTrgMap){ Id BreakoutSessionRID = UBC_Utility.getRecordTypeMap(EVENT_OBJ).get(EVENT_RT_BRE_SES).Id; list<Event_Registration__c> ParentEventsRegList = new list<Event_Registration__c>(); list<id> ParentEventsList = new list<id>(); list<id> ContactsList = new list<id>(); List<Event_Registration__c> EventsRegList0 = [ Select id, Contact__c, Contact__r.id, Event__c, Event__r.Parent_Event__c, Event__r.RecordTypeId from Event_Registration__c where Event__r.RecordTypeId =: BreakoutSessionRID and Id in :newTrgLst ]; for (Event_Registration__c er : EventsRegList0){ ParentEventslist.add(er.Event__r.Parent_Event__c); Contactslist.add(er.Contact__r.id); } if (EventsRegList0!=null && !EventsRegList0.isEmpty()){ for (Event_Registration__c er : [ Select id, Contact__c, Contact__r.id, Event__c, Event__r.Parent_Event__c, Event__r.RecordTypeId from Event_Registration__c where Event__r.RecordTypeId =: BreakoutSessionRID and Id in :newTrgLst]) { boolean error=True; for (Event_Registration__c er2 : [ Select Id, Event__c, Contact__c from Event_Registration__c where Contact__c in:Contactslist and Event__c in :ParentEventslist]) { if (er.Event__r.Parent_Event__c == er2.Event__c && er.Contact__c == er2.Contact__c){ error=False; } } if (error==True){ newTrgMap.get(er.id).Event__c.adderror('Please register at the Parent Event of this Event.'); } } } } }
Please help, it is urgent.