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
agieragier 

Moving SOQL query outside For statement

Would appreciate any advice on how to best re-write this trigger so I don't hit the SOQL limits on bulk loading. It works well for creating individual records. Thanks! I apologize the browser/message board won't allow me to format my posting so I am pasting it in text... 12345678 trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {for (Contact c : Trigger.new){Contact[] contacts= [select id from Contact where NameOffice_Combo__c=:c.NameOffice_Combo__c and NameOffice_Combo__c != ''];if (contacts.size() > 0) {c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');} }}
Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

Below should do it.

 

trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {
	List<String> nameOfficeList = new List<String>();
	Set<String> lookupList = new Set<String>();
	for (Contact c : Trigger.new) {
		nameOfficeList.add(c.NameOffice_Combo__c);
	}

	for (Contact cl : [SELECT NameOffice_Combo__c FROM Contact WHERE NameOffice_Combo__c in :nameOfficeList AND NameOffice_Combo__c != ''])
		lookupList.add(cl.NameOffice_Combo__c);
	
	for (Contact c : Trigger.new) {
		if (lookupList.contains(c.NameOffice_Combo__c)) {
			c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');
		}
	}
}

 

 

 

All Answers

gtindugtindu

Below should do it.

 

trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {
	List<String> nameOfficeList = new List<String>();
	Set<String> lookupList = new Set<String>();
	for (Contact c : Trigger.new) {
		nameOfficeList.add(c.NameOffice_Combo__c);
	}

	for (Contact cl : [SELECT NameOffice_Combo__c FROM Contact WHERE NameOffice_Combo__c in :nameOfficeList AND NameOffice_Combo__c != ''])
		lookupList.add(cl.NameOffice_Combo__c);
	
	for (Contact c : Trigger.new) {
		if (lookupList.contains(c.NameOffice_Combo__c)) {
			c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');
		}
	}
}

 

 

 

This was selected as the best answer
agieragier

Wow - thanks! I really appreciate it. I'm sure this is relatively simple but I am not skilled in Apex. I will try this out. One more question - how do I get the existing trigger deleted or deactivated if it is already in production?

EIE50EIE50

Hi,

 

You can delete the trigger from prodcution using eclipse ide. Just create a new project in eclipse ide with your production credentials, choose the required triggers via add or remove metadata components. Then, after you are done with creating a project, in the ide drill down to the triggers and the trigger that you want to delete, right click on the trigger that you want to delete and choose delete option. It would prompt you ' are you sure you want to delete file blahblah.trigger' press ok and it would again prompt you weather to delete the file locally or on the server as well. choose the one you want and it would take solid amount of time to get deleted (time is around 10-25mins) for each trigger individually.

 

Thanks.

gtindugtindu

If you are replacing it with this - just update the code in the sandbox and push to production.

agieragier

Thank you!

agieragier

I just got to testing this new trigger and received the following error...

Compile Error: unexpected token: 'List' at line 3 column 33 
gtindugtindu

Sorry was a typo... i'll amend my post above - try the following:

trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {
	List<String> nameOfficeList = new List<String>();
	Set<String> lookupList = new Set<String>();
	for (Contact c : Trigger.new) {
		nameOfficeList.add(c.NameOffice_Combo__c);
	}

	for (Contact cl : [SELECT NameOffice_Combo__c FROM Contact WHERE NameOffice_Combo__c in :nameOfficeList AND NameOffice_Combo__c != ''])
		lookupList.add(cl.NameOffice_Combo__c);
	
	for (Contact c : Trigger.new) {
		if (lookupList.contains(c.NameOffice_Combo__c)) {
			c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');
		}
	}
}

 

 

agieragier

Thank you very much!! I've been able to use this successfully and modify it to address 6 other triggers I had written! I have one more that is written a little differently and am not sure how to modify this one...

trigger ContactDuplicateTriggerAcct on Contact (before insert) {
for (Contact c : Trigger.new){
Contact[] contacts= [select id from Contact where 
    (FirstName = :c.FirstName and LastName = :c.LastName and AccountID=:c.AccountID)];
if (contacts.size() > 0) {
c.LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');
} 
}
}

 

gtindugtindu

NP, give this a try

 

trigger ContactDuplicateTriggerAcct on Contact (before insert) {
	Map<String, Contact> contactLookup = new Map<String, Contact>();
	List<Id> accountIds = new List<Id>();
	List<String> firstNames = new List<String>();
	List<String> lastNames = new List<String>();
	for (Contact c : Trigger.new) {
		accountIds.add(c.AccountId);
		firstNames.add(c.FirstName);
		lastNames.add(c.LastName);
		contactLookup.put(c.AccountId + ':' + c.FirstName + ':' + c.LastName, c);
	}

	for (Contact c : [SELECT Id, AccountId, FirstName, LastName FROM Contact WHERE (FirstName in :firstNames and LastName in :lastNames) AND AccountId in :accountIds] {
		if (contactLookup.containsKey(c.AccountId + ':' + c.FirstName + ':'  + c.LastName)) {
			contactLookup.get(c.AccountId + ':' + c.FirstName + ':' + c.LastName).LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');
		} 
	}
}
agieragier

Thanks for your help! I couple errors came up:

 

Compile Error: unexpected token: '{' at line 13 column 167 

 

i thought perhaps it was an extra bracket but when I remove it, then i get:

Compile Error: unexpected token: 'if' at line 14 column 8 
gtindugtindu

It was missing a close parentheses...

 

trigger ContactDuplicateTriggerAcct on Contact (before insert) {
	Map<String, Contact> contactLookup = new Map<String, Contact>();
	List<Id> accountIds = new List<Id>();
	List<String> firstNames = new List<String>();
	List<String> lastNames = new List<String>();
	for (Contact c : Trigger.new) {
		accountIds.add(c.AccountId);
		firstNames.add(c.FirstName);
		lastNames.add(c.LastName);
		contactLookup.put(c.AccountId + ':' + c.FirstName + ':' + c.LastName, c);
	}

	for (Contact c : [SELECT Id, AccountId, FirstName, LastName FROM Contact WHERE (FirstName in :firstNames and LastName in :lastNames) AND AccountId in :accountIds]) {
		if (contactLookup.containsKey(c.AccountId + ':' + c.FirstName + ':'  + c.LastName)) {
			contactLookup.get(c.AccountId + ':' + c.FirstName + ':' + c.LastName).LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');
		} 
	}
}
agieragier

Awesome! Thanks again for all your help!!