• colingunn
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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!

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!