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
JAW99JAW99 

Trigger to Update field on Account on Insert/Update of Contact Role

I think the title explains it - i am looking for some help writing a trigger that updates a specified field on the account record when we have a Contact Role that of a specific role and is primary. thanks.

pbattissonpbattisson

Hey JAW99

 

The help and documentation are a great place to get started for this. What you want to do is add a trigger to contact for after insert and after update that will update the account. 

 

If you want something more specific then PM me and I will help as far as I can.

 

Paul

pbattissonpbattisson

Hey JAW99

 

The sort of thing you will be wanting then is something like this:

 

trigger testTrigger on Contact (after insert, after update) {

	for(Contact c : trigger.new)
	{
		if(c.someField == someValue)
		{
			//Some code here to update your account
			//and add it to a list
		}
	}
	update accList;
}

 

 

That's only the sort of very bare bones of it but it should get you started. You can add triggers through the UI (Setup -> Develop -> Triggers) or use the Eclipse plugin to develop a new trigger.

 

I hope that helps a bit, if you need more detail please list specific fields and actions.

 

Paul

sravusravu

Try the following code:

 

trigger updateAccount on Contact(after insert, after update){

public Id cId;

public Id aId;

 

List<CaseContactRole> ccr  = [select Id,ContactId, Role from CaseContactRole where ContactId IN :Trigger.New.Id];

for(CaseContactRole cRole : ccr){

          if(cRole.Role==<replace the role you wish to compare>){

                                cId = cRole.ContactId;

          }

}

for(Contact c : [select accountId from contact where id = :cId]){

         aId = c.AccountId;

}

for(Account acc : [select Id from Account where id=:aId]){

         acc.<field name you wish to update> = <updated value>;

         update acc;

}

 

Let me know if you face any difficulty................