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
Dastagiri BashaDastagiri Basha 

Hello Developer's, i have one requirement like have Active(checkbox) field on Both account and Contact, While creating a contact, If Account is active Then Conatct should insert with Active. how can achive this with trigger's, can help me with logic.

CharuDuttCharuDutt
Hii Dastagiri
Try Below Trigger
trigger Contacttrigger on Contact (before insert) {
set<Id> AccountId = new set<Id>();
    if(trigger.IsBefore && trigger.IsInsert){
        for(Contact oCon : trigger.new){
            if(oCon.AccountId != Null){
				AccountId.Add(oCon.AccountId);            
			}
        }
    }
	map<Id,Account> mappAccounts = [Select Id,Active__c from Account Where Id In : AccountId];
	
	if(trigger.IsBefore && trigger.IsInsert){
        for(Contact oCon : trigger.new){
            if(oCon.AccountId != Null){
				Account Acc = mappAccounts.get(oCon.AccountId);
					if(Acc.Active__c == true){
						oCon.Active__c = true;
					}else{
						oCon.AddError.AccountId('Please Select Active Account');
					}
			}
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!