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
I am new to Salesforce.comI am new to Salesforce.com 

I need Trigger Example

I need Trigger example... When ever u entered city name in Account object that will automatically update on Contact object  by using trigger i need example code.

 

 

Thanks,

SFDC_Learner

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Here is the trigger for you, Hope it helps you in understanding and writing the triggers next time :)

 

trigger CopyCityToContact on Account (after update) {
	// Start writing trigger
	
	// Collect all updated Account Ids where city is added or updated 
	Set<Id> setAccountIds = new Set<Id>();
	
	// Collect all the Account's Id which has Billing city populated
	for(Account a : Trigger.new) {
		if(a.BillingCity != null) {
			setAccountIds.add(a.Id);
		}
	}
	
	// Have a list for collecting COntacts to update
	List<Contact> contactsToUpdate = new List<Contact>();
	
	// Iterate all the Contact of Acount
	for(Contact c: [Select MailingCity, Id, Account.BillingCity 
					from 
					Contact 
					where 
					AccountId IN: setAccountIds]) {
						
		// Add to Contact list only when Mailing city of the contact is not populated with Account's billing city
		if(c.MailingCity  != c.Account.BillingCity) {
			contactsToUpdate.add(new Contact(Id = c.Id, MailingCity  = c.Account.BillingCity, phone  = '1'));
		}
	}
	
	
	// If there are any contact then perform update
	if(!contactsToUpdate.isEmpty()) {
		update contactsToUpdate;
	}
}

 

All Answers

Jeff MayJeff May

Before we get into the trigger sample code, are you sure you want this trigger?  By setting addresses for all Contacts for an Account to the same City (or other fields) as the Account, you are preventing the possibility that a Contact might have a different address than the Account.  Generally, the Account has a BillingAddress and a MailingAddress; but each Contact has its own address info.

Sagar PareekSagar Pareek

When creating a trigger in Salesforce, you need to decide whether you want actions to occur before or after, as in before insert, before update, before delete versus after insert, after update, after delete.

The Apex documentation actually has a great article on this called Apex Trigger: Context Variable Considerations.

For instance, if you’re getting an error message

System.FinalException: Record is read-only

It may be due to a code segment like this:

trigger Test_Trigger on Object__c (after update) {
  for (Object__c object : Trigger.New) {
    object.Field__c = 'New Value';
  }
}

This is because, specifically in an after update, when attempting to change fields using Trigger.New, this is “[n]ot allowed. A runtime error is thrown, as trigger.new is already saved.”

 

Don't forget to give kudos ,if this reply helps you.

Rahul SharmaRahul Sharma
I think the best way to achieve this without trigger is to Use a workflow rule.
Why do you need a Trigger for this?
I am new to Salesforce.comI am new to Salesforce.com

Hi Sharma

 

 

                         please provide example for this...

 

 

 

 

Rahul SharmaRahul Sharma

Here is the trigger for you, Hope it helps you in understanding and writing the triggers next time :)

 

trigger CopyCityToContact on Account (after update) {
	// Start writing trigger
	
	// Collect all updated Account Ids where city is added or updated 
	Set<Id> setAccountIds = new Set<Id>();
	
	// Collect all the Account's Id which has Billing city populated
	for(Account a : Trigger.new) {
		if(a.BillingCity != null) {
			setAccountIds.add(a.Id);
		}
	}
	
	// Have a list for collecting COntacts to update
	List<Contact> contactsToUpdate = new List<Contact>();
	
	// Iterate all the Contact of Acount
	for(Contact c: [Select MailingCity, Id, Account.BillingCity 
					from 
					Contact 
					where 
					AccountId IN: setAccountIds]) {
						
		// Add to Contact list only when Mailing city of the contact is not populated with Account's billing city
		if(c.MailingCity  != c.Account.BillingCity) {
			contactsToUpdate.add(new Contact(Id = c.Id, MailingCity  = c.Account.BillingCity, phone  = '1'));
		}
	}
	
	
	// If there are any contact then perform update
	if(!contactsToUpdate.isEmpty()) {
		update contactsToUpdate;
	}
}

 

This was selected as the best answer