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
anvesh@force.comanvesh@force.com 

please check this code....

in my  contact  if edit and modify to a feild ,  the feild   in account object should need to be updated.....there is master detail relationship between them,how should we work when there is relation ship like this.

 

trigger contact_ac on Account (is insert,after insert,is update) {
contact ct=new contact();
for(account ac:trigger.new)
{

   if(ac.domain__c=='gmail')

    ct.mail='mail' ;
     update ct;

}

 

 

if  wrong so please help me...

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

Anvesh mistakes u made in your code are as follows.

In the statement u said u want update some field on account when some change is made on contact. and you also said there is a master detail relationship exist, in such scenario you u can do the field update using work flow. Genereally we prefer trigger when there is no relationship exist between 2 objects. Even though if you want to write a trigger. you should learn the proper syntax and coding.

I am making some changes to ur code, follow them 

trigger contact_ac on Account (after insert, after update) {
contact ct=new contact();

List<Account> act = new List<Account>([select Name, Id from Account where Id =: Use Ur MasterdetailName.ID]);

//from the above list you will have all the accounts which are associated with the Contact
for(account ac: act) // iterate through the list for finding the exact match.
{

   if(ac.domain__c=='gmail')

    ct.mail='mail' ;
    // update ct; this is optional since you are writing the trigger on contact object itself.

}

 

If you have question post them I will help u.

 

IF the reply to the question helps to solve, then make this as a solution. and give KUDOS.

All Answers

Suresh RaghuramSuresh Raghuram

Anvesh mistakes u made in your code are as follows.

In the statement u said u want update some field on account when some change is made on contact. and you also said there is a master detail relationship exist, in such scenario you u can do the field update using work flow. Genereally we prefer trigger when there is no relationship exist between 2 objects. Even though if you want to write a trigger. you should learn the proper syntax and coding.

I am making some changes to ur code, follow them 

trigger contact_ac on Account (after insert, after update) {
contact ct=new contact();

List<Account> act = new List<Account>([select Name, Id from Account where Id =: Use Ur MasterdetailName.ID]);

//from the above list you will have all the accounts which are associated with the Contact
for(account ac: act) // iterate through the list for finding the exact match.
{

   if(ac.domain__c=='gmail')

    ct.mail='mail' ;
    // update ct; this is optional since you are writing the trigger on contact object itself.

}

 

If you have question post them I will help u.

 

IF the reply to the question helps to solve, then make this as a solution. and give KUDOS.

This was selected as the best answer
anvesh@force.comanvesh@force.com
k fine.....we mentioned ct.mail='mail' forget about this, i have requirement like if i edit feild in contact the account should need to be updated
Suresh RaghuramSuresh Raghuram

trigger contact_ac on Account (after insert, after update) {
//contact ct=new contact();

List<Account> act = new List<Account>([select Name, Id from Account where Id =: Use Ur MasterdetailName.ID]);

//from the above list you will have all the accounts which are associated with the Contact

Define another list to collect the account records to be updated.

List<Account> updatedActs = new List<Account>();
for(account ac: act) // iterate through the list for finding the exact match.
{

   if(ac.domain__c=='gmail')

   ac.FieldName = The value from the contact u want to pass or any contact value ; // example ac.Email = "Mail";

  updatedActs.add(ac);

}

 

update updatdActs; // write this DML statement out of the for loop to avoid hitting governor limits