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
Merry SMerry S 

Trigger to create new custom object works - but not in mass upload (no errors)

I have a trigger that has to meet these requirements.
If a new rating (custom object) is submitted and is the type of 'overall', has a score of 9 or 10, and the contact on the rating does not have a reference proflle (custom object) create the reference profile. Rating has the contact as a master, Reference Profle has the Account as a master with a lookup to the contact - both fields are required.

The trigger works. However, I did a test where I mass uploaded a bunch of different ratings (types and scores) and it only created one new reference profile (the first one on the list that matched the criteria). There was no error - debug log shows a success. I have worked hours on this trigger (just took an APEX class last month) and I am just not sure why it would not create the reference profile for all contacts if the criteria is a match. 

Here is the trigger:
trigger CreateReferenceProfilefromNewRating on Rating__c (after insert) {
	
	Set<String> ratIdsSet = new Set<String>(); {
		for (Rating__c r: trigger.new) {
			if (r.rating_type__c == 'overall' &&(r.Rating__c == '9' || r.Rating__c == '10')) 
			ratIdsSet.add(r.Contact__c);
			{
		
	List<Reference_profile__c> reftoInsert = new List<Reference_profile__c>([Select  Contact__c 	From Reference_profile__c
	WHERE Contact__c IN:ratIdsSet]);
	
		Reference_profile__c n = new Reference_profile__c ();
      if(r.contact__c != n.contact__c && r.rating_type__c == 'overall' &&(r.Rating__c == '9' || r.Rating__c == '10')){ 
	String ConName = r.Contact_Name_Text__c;
	String AccName = r.Account_Name_Text__c;

		n.contact__c = r.contact__c;  
		n.account__c = r.account__c;
		n.name = AccName + ' - ' + ConName;
		n.reference_status__c = 'candidate';
		n.created_from_rating__c = true;

		reftoInsert.add(n);
	} else if(r.contact__c == n.contact__c && r.rating_type__c == 'overall' &&(r.Rating__c == '9' || r.Rating__c == '10')){
		
	}
	try {
		insert reftoInsert;	
	} catch (system.Dmlexception e) {
		system.debug (e);
	}
	}
		}
	}
	
}


bob_buzzardbob_buzzard
I think there's a few issues here:

1. You are iterating the trigger.new list and each time through, adding the contact id to the set and retrieving all contacts from the set - is this your intention? I would have thought you'd iterate and add all contacts, then retrieve them all in a single query.
2. You retrieve the existing reference profiles into a list but then just append new record to it - I would expect you to store these values into a map and then check if one exists for the contact and if it doesn't insert a new one.
3. You create a new reference profile and add it to the list regardless of whether one already exists in the database or the list.  
4. You insert the list that contains records you have retrieved from the database, which will throw an exception as you can't insert with an existing id.  However, you are catching the exception and generating a debug statement, which means the transaction will appear to complete successfully. Do you see errors in your debug log?


Merry SMerry S
Bob, Thanks for the reply. Surprisingly this trigger works as I expected, except when mass inserting. As for the debug log, I saw no errors. I have a lot to learn. I will research each of your points and update the trigger. I appreciate the guidance.