• Gautam Kumar 118
  • NEWBIE
  • 0 Points
  • Member since 2020

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

I need a trigger to create a Dummy Account record every time a Contact is saved with its AccountID field empty (so that Contact is not marked Private and thereby inaccesssible to all beside Owner and Admin). Would the following work? Do I need to add something so that it ONLY creates a dummy record when the AccountID field is empty?

 

trigger create_dummy_accnt on Contact (before insert) {

 

  // create a container to bulk insert accounts
  List<Account> accountsToInsert = new List<Account>(); 

 

  // loop through trigger records
  for (int i=0; i<Trigger.new.size(); i++)
  {
    {
      Account CreateAccnt = new Account();
      CreateAccnt.Name = 'Dummy Account';
      accountsToInsert.add( CreateAccnt );
    }
  }

  // bulk insert accounts
  if (!accountsToInsert.isEmpty()) {
    insert accountsToInsert;
  }
}

 

Also, can anyone think of a way to keep the resulting dummy accounts off the recently viewed items list?

 

Ideally I'd like to drop this workaround and somehow prevent the system from marking Contacts with blank Account ID private - there's no way to do that programatically I don't suppose?

 

The only other workaround I know is to make everyone a system admin. Yikes!