Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 5. Create a field on Account Named (Client Contact lookup to Contact). Once an Account is inserted a Contact will create with the name of the Account and that Contact will be the Client Contact on the Account.
- ===========\\\\\\\\\===============
- > when we want to create a field with lookup relation we want to create some empty list and Map
- > list for contact to be updated
- > list for account to be updated
- > set for account to get the Id's from trigger.new
- > map for checking the lookup relation between contact and account.
- ++++++++++++\\\\\\\\\+++++++++++++++
- Trigger contactOnAccountTrigger on Account (After Insert){
- switch on Trigger.OperationType{
- when AFTER_INSERT{
- contactOnAccountHelper.createContactOnAccount(Trigger.new);
- }
- }
- }
- Public with sharing class contactOnAccountHelper{
- public static void createContactOnAccount(List<Account> accList){
- if(accList != null){
- Set<Account> accSet = new Set<Account>();
- for(Account acc : accList){
- accSet.add(acc.Id);
- }
- List<Contact> conInserted = new contact();
- for( Account accContact : accList){
- contact con = new contact();
- con.LastName = accContact.name;
- con.accountId = accContact.Id;
- conInserted.add(con);
- accSet.add(con.AccountId);
- }
- if(conInserted.size() > 0 && conInserted != null ){
- INSERT conInserted;
- }
- }
- List<Account> accLkp = [SELECT id, name , Client_Contact__C from account where Id=: accSset];
- Map<Id, Account> accMap = new Map<Id, Account>();
- List<Account> accUpdated = new List<Account>();
- if(accLkp.size() > 0){
- for(Account acc: accLkp){
- accMap.put(acc.Id, acc);
- }
- }
- if(conInserted.size() > 0){
- for(Contact con: conInserted){
- if(accMap.containsKey(con.AccountId)){
- Account aObj = accMap.get(con.AccountId);
- accUpdated.add(aObj);
- }
- }
- }
- // check for the new account list to update.
- if(accUpdated.size() > 0 ){
- UPDATE accUpdated;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement