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
colingunncolingunn 

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!

Best Answer chosen by Admin (Salesforce Developers) 
colingunncolingunn

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:

 

trigger SetDefaultAccount on Contact (before insert, before update) {
  List<Account> listAccount = new list<account>([SELECT Id FROM Account WHERE Name = 'Individual' limit 1]); 
    
  if ( listAccount.size() == 0 ) {
//create Individual account
  Account testAccount = new Account(name='Individual');
  insert testAccount ;
}     
    For (Contact nc:Trigger.new) {
            for (Account yacht :[SELECT Id, Name FROM Account WHERE Name = 'Individual' limit 1]){
    
    string rocket = yacht.Id;
    string perception = rocket.substring(0,15);
    

                If (nc.Account == NULL){
            nc.AccountId = perception;}
            }
    }
}

 

All Answers

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

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 

 

 List<Account> listAccount = new list<account>([SELECT Id FROM Account WHERE Name = 'Individual' limit 1]); 
  if ( listAccount.size() == 0 ) {
//create Individual account
  Account testAccount = new Account(name='Individual');
  insert testAccount ;
} 


Let me know if this works or there are still issues.

 

 

colingunncolingunn

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.

LGordonCRMEidqLGordonCRMEidq

I changed my test Class from the default version ov 27 to 16 (where most of the others were), and my test passed.

colingunncolingunn

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:

 

trigger SetDefaultAccount on Contact (before insert, before update) {
  List<Account> listAccount = new list<account>([SELECT Id FROM Account WHERE Name = 'Individual' limit 1]); 
    
  if ( listAccount.size() == 0 ) {
//create Individual account
  Account testAccount = new Account(name='Individual');
  insert testAccount ;
}     
    For (Contact nc:Trigger.new) {
            for (Account yacht :[SELECT Id, Name FROM Account WHERE Name = 'Individual' limit 1]){
    
    string rocket = yacht.Id;
    string perception = rocket.substring(0,15);
    

                If (nc.Account == NULL){
            nc.AccountId = perception;}
            }
    }
}

 

This was selected as the best answer