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
Rahul  37Rahul 37 

Can you Tell me why My code is working ?

When Primary Contact  is True then only selected Contact address is same as Account address

 

trigger UpdateAddress on Contact (after update) {
    Set<Id> contactIds = new Set<Id>();
    for (Contact a : Trigger.new) {
        Contact old = Trigger.oldMap.get(a.Id);
        if (a.BillingStreet != old.BillingStreet || ...) {
            contactIds.add(a.Id);
        }
    }
    if (contactIds.size() > 0) {
        Account[] updates = [
                select Id, AccountId
                from Account
                where AccountId in :contactIds
                ];
        for (Account c : updates) {
            Contact a = Trigger.newMap.get(c.ContactId);
            c.BillingStreet = a.BillingStreet;
            ...
        }
        update updates;
    }
}

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Rahul,

Are you trying to copy the address from Account to Contact or contact to Account. The logic looks like updating the contact address with Account address  and written the logic on Contact After context which would be wrong.

Thanks,
 
Rahul  37Rahul 37

Thanks for the update

 

Contact to Account  // while Updates or deleted

Rahul  37Rahul 37

Hi Sai,

 

I am new in Salesforce

Could you please tell me the exact code

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Rahul,

Can you check the below trigger on contact object. Here i am updating MailingStreet from contact to billingstreet on account. Same way you can update what ever field you need.

 
trigger UpdateAddress on Contact (after update) {
    Map<id,Contact> contmap= new Map<id,Contact>();
    for(Contact ord:Trigger.new){
        
        Contact oldorder= Trigger.oldmap.get(ord.id);
        if(ord.MailingStreet !=oldorder.MailingStreet){
            contmap.put(ord.AccountId,ord);
        }
        
    }
    
    List<Account> acclist=[select id,BillingCity from Account where id in :contmap.keySet() ];
    List<Account> tobeupdate= new List<Account>();
    For(Account acc:acclist){
        Contact ordervalue=contmap.get(acc.id);
        acc.billingstreet= ordervalue.MailingStreet;
        tobeupdate.add(acc);
    }
update tobeupdate;
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
mukesh guptamukesh gupta
Hi Rahul,

Please use below code:-
 
trigger UpdateAddress on Contact (after update) {
    Set<Id> accIds = new Set<Id>();
    for (Contact con : Trigger.new) {
        Contact old = Trigger.oldMap.get(con.Id);
        if (con.BillingStreet != old.BillingStreet) {
            accIds.add(con.AccountId);
        }
    }
    if (accIds.size() > 0) {
        Map<id, Account> accMap = Map<Id, Account>([
                select Id, AccountId
                from Account
                where AccountId in :accIds AND Primary_Contact__c = true
                ]);
			
    if(accMap.size() > 0 ){
       for(Contact con : Trigger.New){
		  Account acc = accMap.get(con.AccountId);
		  acc.BillingStreet = con.BillingStreet;
		}
		
		update accMap.values();
	}	
				
        
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Rahul  37Rahul 37

Hi Mukesh,

Error showing in 10 Line

Please tell me  

Error

Rahul  37Rahul 37
Hi Sai,

Your code is saved successfully but not working
mukesh guptamukesh gupta
Hi Rahul,

Please use below:-
 
trigger UpdateAddress on Contact (after update) {
    Set<Id> accIds = new Set<Id>();
    for (Contact con : Trigger.new) {
        Contact old = Trigger.oldMap.get(con.Id);
        if (con.BillingStreet != old.BillingStreet) {
            accIds.add(con.AccountId);
        }
    }
    if (accIds.size() > 0) {
        Map<id, Account> accMap = New Map<Id, Account>([
                select Id, BillingStreet
                from Account
                where AccountId in :accIds AND Primary_Contact__c = true
                ]);
			
    if(accMap.size() > 0 ){
       for(Contact con : Trigger.New){
		  Account acc = accMap.get(con.AccountId);
		  acc.BillingStreet = con.BillingStreet;
		}
		
		update accMap.values();
	}	
				
        
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Rahul  37Rahul 37

Hello Mukesh

 

I think problem is Primary_contact__c  is Contact Field not Account Field

 

Error

Rahul  37Rahul 37
User-added image
mukesh guptamukesh gupta
Hi  Rahul,

Please use below code and according to this code you should have Primary_Contact__c field on contact object
trigger UpdateAddress on Contact (after update) {
    Set<Id> accIds = new Set<Id>();
    for (Contact con : Trigger.new) {
        Contact old = Trigger.oldMap.get(con.Id);
        if (con.BillingStreet != old.BillingStreet) {
            accIds.add(con.AccountId);
        }
    }
    if (accIds.size() > 0) {
        Map<id, Account> accMap = New Map<Id, Account>([
                select Id, BillingStreet
                from Account
                where Id in :accIds AND Primary_Contact__c = true
                ]);
			
    if(accMap.size() > 0 ){
       for(Contact con : Trigger.New){
		  Account acc = accMap.get(con.AccountId);
		  acc.BillingStreet = con.BillingStreet;
		}
		
		update accMap.values();
	}	
				
        
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Tha above code which I shared will copy the MailingStreet from Contact to billingstreet on Account. Can you check it. I have checked and it is working as expected. You have to edit the MailingStreet on contact such that old value is not equal to new value.

Thanks,
 
Rahul  37Rahul 37

Hi Mukesh,

I have created a Field same name  Account as well as Contact and

I have created also Check Box Field Name - Primary_contact__c on Contact Object

but Error only 10 line

Error

mukesh guptamukesh gupta
Hi Rahul,

It's my mistake because i am using notepad for type the code , I hope this should be work

Please use below code:-
trigger UpdateAddress on Contact (after update) {
    Set<Id> accIds = new Set<Id>();
    for (Contact con : Trigger.new) {
        Contact old = Trigger.oldMap.get(con.Id);
        if (con.BillingStreet != old.BillingStreet && con.Primary_Contact__c == true && old.Primary_Contact__c != true) {
            accIds.add(con.AccountId);
        }
    }
    if (accIds.size() > 0) {
        Map<id, Account> accMap = New Map<Id, Account>([
                select Id, BillingStreet
                from Account
                where Id in :accIds
                ]);
			
    if(accMap.size() > 0 ){
       for(Contact con : Trigger.New){
		  Account acc = accMap.get(con.AccountId);
		  acc.BillingStreet = con.BillingStreet;
		}
		
		update accMap.values();
	}	
				
        
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh