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
smitha george 5smitha george 5 

Inserting records

Hi,
I want to insert Account,contact,User records in single instance.
Can you tell me the process..

Thanks in Advance.
 
Best Answer chosen by smitha george 5
v varaprasadv varaprasad
Hi Smita,

To insert Account, contact, User records in a single instance.
You need to create an external field in parent object in your case account.Through external field, we will pass the external value to the child record.
I have created SLASerialNumber__c is external id field to link contact and account..
Sample code  : 
 
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        
     	User usr = new User(LastName = 'LIVESTON111',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason1111.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US'
                           );

Account acc=new Account(Name='Kdeepthi', SLASerialNumber__c='prasadam');
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(SLASerialNumber__c='prasadam'));
 
insert new List<Sobject>{usr, acc, cont};
    system.debug('usr=='+usr.id+'acc=='+acc.id+'cont=='+cont.id);



Hope this helps!

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com




 

All Answers

srlawr uksrlawr uk
What event triggers this proccess (builder) to happen? 

A user clicking a button, another record activity? Or are you loading data?


The top line of my answer has two of the best options in I would imagine; if it is a record event - you can write an Apex Trigger to do almost anything you want (I'm amazed you want to automate the creation of a user though).

Process Builder can also handle inserting multiple items, have a read up on that here 
https://trailhead.salesforce.com/en/modules/business_process_automation/units/process_builder

Or, if you want to just stuff all that data in, you can use the DataLoader

https://developer.salesforce.com/page/Data_Loader



 
v varaprasadv varaprasad
Hi Smita,

To insert Account, contact, User records in a single instance.
You need to create an external field in parent object in your case account.Through external field, we will pass the external value to the child record.
I have created SLASerialNumber__c is external id field to link contact and account..
Sample code  : 
 
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        
     	User usr = new User(LastName = 'LIVESTON111',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason1111.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US'
                           );

Account acc=new Account(Name='Kdeepthi', SLASerialNumber__c='prasadam');
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(SLASerialNumber__c='prasadam'));
 
insert new List<Sobject>{usr, acc, cont};
    system.debug('usr=='+usr.id+'acc=='+acc.id+'cont=='+cont.id);



Hope this helps!

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com




 
This was selected as the best answer