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
pcave_jrpcave_jr 

Create account object from contact fields

I'm pretty new to salesforce so I was wondering if the following is possible. When a new contact is being created, is it possible to use some custom fields on that contact object to generate a new account and then relate the contact to it. I'm guessing this could be done using an Apex trigger. If anyone has any pointers or sample code, I would apprecaite it.

 

Thanks,

Phillip

IanRIanR

Hi,

 

I wrote this on creating triggers in visualforce.com...

 

 

It'll be something like:

 

 

trigger CreateAccount on Contact (after insert) {

Map<Id, Account> accountMap = new Map<Id, Account>();

Set<Id> contactIds = new Set<Id>();

for (Contact c : trigger.new) {

contactIds.add(c.Id);

}

List<Contact> allContacts = [SELECT Id, FirstName, LastName FROM Contact WHERE Id in: contactIds];

for (Contact c : allContacts) {

Account a = new Account();

// Set account properties here

a.Name = c.FirstName + ' ' + c.LastName;

accountMap.put(c.Id, a);

}

insert accountMap.values();

 

for (Contact c : allContacts) {

Account a = accountMap.get(c.Id);

if (a != null)

c.AccountId = a.Id;

}

update allContacts;

}

 

 

HTH,  Ian