You need to sign in to do that
Don't have an account?

Issue with Trigger using Maps
Hi all,
I have created a trigger to check that a combination of 2 fields (document type and document number) in the Contact object is unique. To avoid the "Too many SOQL queries" for bulk inserts I tried the following approach:
trigger contactUniqueDocument on Contact (before insert, before update) { // Create a Map that holds all contacts in Salesforce and another one to hold the ones being inserted Map<String, Contact> ContactosExistentes = new Map<String, Contact> (); Map<String, Contact> ContactosNuevos = new Map<String, Contact> (); for(Contact storedContacts : [SELECT Id, documentType__c, documentNumber__c FROM Contact]) { String documentId = storedContacts.documentType__c; documentId += storedContacts.documentNumber__c; ContactosExistentes.put(documentId, storedContacts); } // Go through the list of Contacts created / updated to find issues for(Contact Contacto: Trigger.new) { String NewdocumentId = Contacto.documentType__c; NewdocumentId += Contacto.documentNumber__c; if(ContactosExistentes.containsKey(NewdocumentId)) { Contacto.addError('There is already a contact on SF with this Doc Type & Num'); } if(ContactosNuevos.containsKey(NewdocumentId)) { Contacto.addError('There is already a contact on this list with this Doc Type & Num'); } else { ContactosNuevos.put(NewdocumentId , Contacto); } } }
The problem I am facing is that Maps have a limit of 1,000 elements and we have more than 9,000 contacts, so I am getting errors when trying to upload +1,000 records with Dataloader.
Unfortunately I can´t use Sets (same limitation) or Lists (unable to search for values, only return elements by position).
But there must be an easier way of doing this.... Any ideas?
Thanks,
J
Got it working in the end. Thanks anyways
All Answers
Got it working in the end. Thanks anyways
Sure. I created a formula field that was the combination of the 2 fields (not visible). I then used a Set to insert the bulk records being updated / inserted (to insert the combo of the 2 fields I mean) and finally did a bulk SELECT using the IN clause to check for any existing Contacts where the formula field equalled any of the elements in the Set.
J