function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Diari DieyeDiari Dieye 

Faulty Apex Trigger?

We have an Apex Trigger set up so that Intake Records generate and populate new Participant Records (Contacts) given certain criteria. Lately, the newly created Contact Records have been also overwriting other Contacts Records when generated, sometimes 3 or 4 at a time! Help?
prateek jainprateek jain
Hi 
Please can you provide your code
thanks
Diari DieyeDiari Dieye
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 trigger AddContact on Intake__c (after insert, after update) { // Field mappings list from Custom Metadata Type List fieldMappings = [Select DeveloperName, MasterLabel, Intake_field__c, Participant_field__c From IntakeToParticipantFieldMapping__mdt]; Map intakeParticipantMap = new Map(); // Creates a map with fields mapping for(IntakeToParticipantFieldMapping__mdt mapping : fieldMappings){ intakeParticipantMap.put(mapping.Intake_field__c, mapping.Participant_field__c); } // Recordtypes mappings list from Custom Metadata Tye List rtMappings = [ Select DeveloperName, MasterLabel, Intake_Recordtype_Id__c, Intake_Recordtype_Name__c, Participant_Recordtype_Id__c, Participant_Recordtype_Name__c From IntakeToParticipantRecordTypeMapping__mdt]; // Map to store Intake RecordType Id ==> Partcipant RecordType Id Map intkRtIDParticipantRtIDMap = new Map(); // Creates a map with recordTypes mapping for(IntakeToParticipantRecordTypeMapping__mdt recordTypes : rtMappings){ intkRtIDParticipantRtIDMap.put(recordTypes.Intake_Recordtype_Id__c, recordTypes.Participant_Recordtype_Id__c); } if(Trigger.isInsert){ List participants = new List(); for(Integer i = 0; i < Trigger.new.size(); i++){ Intake__c intake = Trigger.new[i]; if(intake.Enrolled__c == 'Yes' && intake.Eligible__c == 'Yes'){ if(!intakeParticipantMap.isEmpty() && !intkRtIDParticipantRtIDMap.isEmpty()){ Contact c = new Contact(); c.Status__c = 'Active'; c.unique_ID__c = intake.unique_ID__c; // Loop over fieldMappingsMap and gets the value for the key intakefield for(String intakeField : intakeParticipantMap.keyset()){ c.put(intakeParticipantMap.get(intakefield), intake.get(intakefield)); } // Assigns recordtypeId to Participant depending on the recordtype id of the intake c.put('RecordTypeId', intkRtIDParticipantRtIDMap.get(intake.RecordTypeId)); participants.add(c); } } } if(participants.size() > 0){ insert participants; } } if(Trigger.isUpdate){ Set participantsToUpdate = new Set(); List participantsToInsert = new List(); for(Intake__c intk : Trigger.new){ // If previously the intake record has Enrolled and Eligible field = NO if(Trigger.oldMap.get(intk.Id).Enrolled__c == 'No' || Trigger.oldMap.get(intk.Id).Eligible__c == 'No'){ if(intk.Enrolled__c == 'Yes' && intk.Eligible__c == 'Yes'){ if(!intakeParticipantMap.isEmpty() && !intkRtIDParticipantRtIDMap.isEmpty()){ Contact c = new Contact(); c.Status__c = 'Active'; // Loop over fieldMappingsMap and gets the value for the key intakefield for(String intakeField : intakeParticipantMap.keyset()){ c.put(intakeParticipantMap.get(intakefield), intk.get(intakefield)); } // Assigns recordtypeId to Participant depending on the recordtype id of the intake c.put('RecordTypeId', intkRtIDParticipantRtIDMap.get(intk.RecordTypeId)); participantsToInsert.add(c); } } } else { if(intk.Enrolled__c == 'Yes' && intk.Eligible__c == 'Yes'){ // Means we have to update the participant participantsToUpdate.add(intk.unique_ID__c); } } } if(participantsToInsert.size() > 0){ insert participantsToInsert; } // ##### FINISH INSERTING NEW PARTICIPANTS #### \\ // ##### UPDATES PARTICIPANTS THAT WERE PREVIOUSLY CREATED ##### if(participantsToUpdate.size() > 0){ String fieldsForQuery = ''; for(String field : intakeParticipantMap.values()){ if(String.isEmpty(fieldsForQuery)){ fieldsForQuery = field; } else { fieldsForQuery = fieldsForQuery + ',' + field; } } String theQuery = 'Select Id, unique_ID__c, ' + fieldsForQuery + ' From Contact Where unique_ID__c IN ( \''; for(String participantUniqueId : participantsToUpdate){ theQuery = theQuery + participantUniqueId; } theQuery = theQuery + '\' )'; // Get the participants to update List participantsList = Database.query(theQuery); List contacts = (List)participantsList; List contactsToUpdate = new List(); for(Intake__c intk : Trigger.new){ for(Contact c: contacts) { if(intk.unique_ID__c == c.unique_ID__c){ // Loop over fieldMappingsMap and gets the value for the key intakefield for(String intakeField : intakeParticipantMap.keyset()){ c.put(intakeParticipantMap.get(intakefield), intk.get(intakefield)); } // Assigns recordtypeId to Participant depending on the recordtype id of the intake c.put('RecordTypeId', intkRtIDParticipantRtIDMap.get(intk.RecordTypeId)); contactsToUpdate.add(c); } } } update contactsToUpdate; } } } Diari Dieye Program Associate ■ Strong Fathers, Stronger Families Seedco ■ 22 Cortlandt Street, 33rd Floor ■ New York, NY 10007 Tel: (917) 338-9257 ■ Fax: (212) 813-3179 ■ www.seedco.org