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
Mr.Mittal_2617Mr.Mittal_2617 

Creating Apex Trigger

Hi,

I have to write a trigger that when new contact is being created to populate the account domain field value in to contact domain field

Domain is a field created on account and contact field.

Please help.
Best Answer chosen by Mr.Mittal_2617
ravi soniravi soni
hy ,
try below trigger and I'm sure It will help you.
trigger updateContactFieldBasedOnAccountField on Contact (before insert,before update) {
   set<Id> setAccIds  = new set<id>();
    if(trigger.IsBefore && (Trigger.IsInsert || trigger.isUpdate)){
            for(Contact Con : Trigger.new){
                if(Con.AccountId != null){
                    setAccIds.add(Con.AccountId);
                }
            }
    
    Map<Id,Account> idWiseAccMap = new  Map<Id,Account>([Select Id,Domain_Field__c From Account 
                                                         where Id IN :setAccIds And Domain_Field__c != null ]); 
        system.debug('idWiseAccMap : ' + idWiseAccMap);

	for(Contact Con : Trigger.new){
                if(Con.AccountId != null){
                    if(idWiseAccMap.containsKey(Con.AccountId )){
					Con.Domain_Field__c = idWiseAccMap.get(Con.AccountId ).Domain_Field__c;
                }
				}
            }
			
	
}
}

don't forget to mark it as best answer and If you need any assistance then mention here.
Thank you
​​​​​​​

All Answers

CharuDuttCharuDutt
Hii Mr Mittal
Try Below Trigger
You Can Also Try Formula Field For This
trigger TestTrigger on Contact (after insert ) {
    set<Id> lstid  = new set<id>();
    if(trigger.IsAfter && Trigger.IsInsert){
            for(Contact Con : Trigger.new){
                if(Con.AccountId != null){
                    lstid.add(Con.AccountId);
                }
            }
        
    
    list<Account > lstbc = [Select Id,Domain_Field__c,(Select id,AccountId,Domain_Field__c From Contacts) From Account where Id IN :lstid ];
    for(Account Acc : lstbc){
        for(Contact Con2 : Acc.Contacts){
            Con2.Domain_Field__c = Acc.Domain_Field__c;
        }
            }
        if(lstbc.size()>0){
        update lstbc;
        }
}
Please Mark It As Best Answer If It Helps
Thank You!
ravi soniravi soni
hy ,
try below trigger and I'm sure It will help you.
trigger updateContactFieldBasedOnAccountField on Contact (before insert,before update) {
   set<Id> setAccIds  = new set<id>();
    if(trigger.IsBefore && (Trigger.IsInsert || trigger.isUpdate)){
            for(Contact Con : Trigger.new){
                if(Con.AccountId != null){
                    setAccIds.add(Con.AccountId);
                }
            }
    
    Map<Id,Account> idWiseAccMap = new  Map<Id,Account>([Select Id,Domain_Field__c From Account 
                                                         where Id IN :setAccIds And Domain_Field__c != null ]); 
        system.debug('idWiseAccMap : ' + idWiseAccMap);

	for(Contact Con : Trigger.new){
                if(Con.AccountId != null){
                    if(idWiseAccMap.containsKey(Con.AccountId )){
					Con.Domain_Field__c = idWiseAccMap.get(Con.AccountId ).Domain_Field__c;
                }
				}
            }
			
	
}
}

don't forget to mark it as best answer and If you need any assistance then mention here.
Thank you
​​​​​​​
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Mr. Mittal,

Use this Code, it is working with your requirements :
trigger triggerOnContact1 on Contact (after insert) {
    set<id> accountId_Set = new set<id>();
    for(contact con:trigger.new){
        if(con.AccountId !=null){
            accountId_Set.add(con.AccountId);
        }
    }
    List<account> accList = new List<account>();
    accList = [select Id,Name,Domain_field__c,(Select id,LastName,accountId, Domain_Field__c  from Contacts) from account where Id In:accountId_Set];
    List<Contact> conList = new List<Contact>();
    for(account acc : accList){
        for(contact con: acc.contacts){
        System.debug('In triggerOnContact-->'+acc.Domain_field__c);
            if(con.AccountId == acc.id){
                System.debug('con.AccountId -->'+con.AccountId);
                con.Company__c= acc.Domain_field__c ;
               conList.add(con);
            }
        }
    }
    if(conList.size() > 0){
    update conList;
        }
}

If you find your Solution then mark this as the best answer to close this question

Thank you!

Regards,
Suraj Tripathi