You need to sign in to do that
Don't have an account?
Trigger to set default Account for new Contact not working when deployed
Hey developers. This is my first time posting, and the first Trigger I've put together. I've scoured the boards and documentation, so please forgive me if I'm missing something obvious; I just hit a wall. Here's the trigger I ended up with to try to assign new Contacts to the Account "Individual" if that field is left blank during entry:
trigger SetDefaultAccount on Contact (before insert) { Account[] testAccount = [SELECT Id FROM Account WHERE Name = 'Individual' limit 1]; if ( testAccount.size() == 0 ) { //create Individual account testAccount = new Account[1]; testAccount [0] = new Account(name='Individual'); insert testAccount ; } Account yacht = [SELECT Id FROM Account WHERE Name = 'Individual' limit 1]; For (Contact nc:Trigger.new) { If (nc.AccountId == NULL) nc.AccountId = yacht.Id;} }
It passes the following test, both in the Developer edition where I was working on it, and on the client's Enterprise edition where I deployed it:
public class testcontact { static testmethod void mytest1() { Contact c = new Contact(LastName='test lname'); insert c; } }
When I add a new Contact in the Developer edition, the Trigger works great. However, when I tried adding a new Contact in the client's edition, I still received the error that the Account field had not been filled in. Any advice y'all have would make my day. Thanks!
Well, I put some time into learning more about how to use the Developer Console to examine exceptions (though it took a lot longer than a few days to get around to it). Unfortunately, what I learned is that the trigger worked all along. The problem was that the Account field was still marked as required on the page layout. Tuba noise. Anyway, here's what I ended up with in the end:
All Answers
Have you checked what exception is thrown in Client's Org?
Most like this line with throw exception if there is no account found for your query -
Account[] testAccount = [SELECT Id FROM Account WHERE Name = 'Individual' limit 1];
Please change this to
Let me know if this works or there are still issues.
Thanks for your response. I made the changes that you suggested, but am unforunately still receiving the error message when I try to enter a new Contact. The test Class passes as it did before; the first query returns a list with no lines, the account is added, and the second query finds it.
Because I am still learning how to use the Developer Console, I do not yet know how to find out what exception is being thrown. I will respond with more information in the next few days. Thanks again for your help.
I changed my test Class from the default version ov 27 to 16 (where most of the others were), and my test passed.
Well, I put some time into learning more about how to use the Developer Console to examine exceptions (though it took a lot longer than a few days to get around to it). Unfortunately, what I learned is that the trigger worked all along. The problem was that the Account field was still marked as required on the page layout. Tuba noise. Anyway, here's what I ended up with in the end: