+ Start a Discussion
sfdevnicksfdevnick 

Create Dummy Account Record Every time Contact is saved with AccountID Field Empty

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!

JeremyKraybillJeremyKraybill

If your goal is to make contacts who are not attached to an account always globally visible, I would just create an after update/after insert trigger on contact which looks if AccountId == null, and if so, and if this contact doesn't already have a global share on it, create a ContactShare which gives the entire org sharing on that contact. See "Sharing a record using Apex" in the Apex dev guide for code.

 

HTH

 

Jeremy Kraybill

Austin, TX

sfdevnicksfdevnick

Jeremy,

 

Thank you for your quick response. I apologize for not responding sooner, I have been off the grid tied up working on an unexpected deadline.

 

I looked up the section in the Apex guide as advised. From the chapter "Understanding Sharing" I gleaned that when determining a user's permission to a record the system looks to the sharing object for that record and the Org-wide defaults and chooses the most permissive. The general help states "Contacts that are not linked to an Account (personal contacts) are always private, regardless of your organization’s sharing model. Sharing rules do not apply to private contacts". The guide states: "All implicit sharing added by Force.com managed sharing cannot be altered directly using the Salesforce user interface, Force.comWeb Services API, or Apex". I wonder what "Implicit Sharing" means? What does it cover? I wonder if it includes this Contact privatization that sharing rules cannot overide? If so, it sounds like Apex cannot overide it?

 

If Apex *can* overide this, then it looks like I should follow the example code for "Force.com Managed Sharing"?

(because it states that "Apex Managed Sharing" is only available on custom objects)

 

[As an aside for future reference, it looks like the only difference between "Creating User Managed Sharing Using Apex" versus "Creating Apex Managed Sharing" is that in the former if the owner of the record changes, the sharing is automatically deleted. Regarding the latter, the guide states "This type of sharing is similar to Force.com managed sharing (aka "Creating User Managed Sharing Using Apex") only the application developer manages this sharing using Apex". I am confused by this since "Creating User Managed Sharing Using Apex" uses Apex too. How else does the user "manage" the sharing "created" by Apex beside changing owner? Or is that it?]

 

Thanks again,
James

Message Edited by sfdevnick on 02-22-2009 08:47 PM
Message Edited by sfdevnick on 02-22-2009 08:57 PM
Gautam Kumar 118Gautam Kumar 118
nscbc