+ Start a Discussion
Aam MAam M 

trigger to update contact when account record is updated

I want to write trigger to enable a checkbox in contact whenever its Account is modified(updated). 
I'm not able to put the condition for Account updated or not! Can you guys provide the logic?? 
 
Best Answer chosen by Aam M
Akshay_DhimanAkshay_Dhiman
Hi Mujahid,
You should try this code .
Apex Trigger
trigger accountcontat on Account (after update){
   List<Contact> contactList = new List<Contact>();
   List<Contact> contactList1 = new List<Contact>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
   If(acc.Contacts.size()>0){
     contactList.addAll(acc.Contacts);
    }
}
for(contact c:contactList){
       c.checkbox__c=true;
       contactList1.add(c);
}
update contactList1;
}
I hope it will help you.
Regards,
Akshay
Please mark my answer as a solution if it is helpful.

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Mujahid,

Hope it will helps you :)

trigger AccountTrigger on Account (after update)
  {
     Set<Id> set_accountId = new Set<Id>();
     List<Contact> contactList = new List<Contact>();

for(Account acc : Trigger.new)
  {
    if(acc.FieldApiName!=Trigger.oldMap.get(acc.id).FieldAPIName)
        set_accountId.add(acc.id);
  }

Map<Id,List<Contact>> mp_ListContact = new Map<Id,List<Contact>>();

for(Contact con : [Select Id,AccountId,CheckboxField from Contact where accountId IN : set_accountId])
  {
     if(mp_ListContact!=null && mp_ListContact.containsKey(con.AccountId))
        {
            List<Contact> conList = mp_ListContact.get(con.AccountId);
             conList.add(con);
            mp_ListContact.put(con.AccountId, conList);
        }

     else
        mp_ListContact.put(con.AccountId, new List<Contact>{con});

   }


for(Account acc : Trigger.new)
  {
     if(mp_ListContact!=null && mp_ListContact.containsKey(acc.id))
         {
             for(Contact con : mp_ListContact.get(acc.id))
                 {
                      // Write logic to update contact as sample below : 
                          con.CheckboxField = True;
                          con.FirstName = acc.Name;
                          contactList.add(con);
                  }
            }
   }

if(contactList!=null && contactList.size()>0)
   update contactList;

}

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Harshit Garg 6Harshit Garg 6
HI Mujahid,

If I am not wrong,
You want when anyone will update the account then the checkbox will automatically check on contact.

Thanks,
Harshit garg
Akshay_DhimanAkshay_Dhiman
Hi Mujahid,
You should try this code .
Apex Trigger
trigger accountcontat on Account (after update){
   List<Contact> contactList = new List<Contact>();
   List<Contact> contactList1 = new List<Contact>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: Trigger.new]){
   If(acc.Contacts.size()>0){
     contactList.addAll(acc.Contacts);
    }
}
for(contact c:contactList){
       c.checkbox__c=true;
       contactList1.add(c);
}
update contactList1;
}
I hope it will help you.
Regards,
Akshay
Please mark my answer as a solution if it is helpful.
This was selected as the best answer
Saravana Bharathi 1Saravana Bharathi 1
Hi,

You can achieve it through Process Builder.

Create process on Account Object, You can specify entry condition on account record to qualification.
Update Child record.

You can update all Contact under that account (or). You can specify the condition on Contact record, and update the checkbox on it.

Thanks.
goabhigogoabhigo
If the task is to write a trigger, then you can use above codes given by Rajat and Akshay; hopefully after understanding it, not just copy paste.

Otherwise, as Saravana suggested, go for Process builder since it is easy to maintain later stages. Process builder is designed to reduce the coding efforts required for various such small things, so use it.

--
Abhi
Akshay_DhimanAkshay_Dhiman
Thanks Mujahid for selecting my answer as a best answer.

Regards,
Akshay
Vamsi MopideviVamsi Mopidevi
trigger updateContact on Account (after update){
   List<Contact> contacts = [select id, checkboxField__c from contact where AccountId in : Trigger.New];
   for(contact c:contacts){
       c.checkboxField__c=true;
       }
    update contacts;
}
Wilson Busaka 3Wilson Busaka 3
Hi guys, I'm trying to write a trigger on the Opportunity object to update the Opportunity Name when the related Account Object Name changes.
Harshit Garg 6Harshit Garg 6
Hi Wilson,

You want to update opportunity name same as account name. Please correct me if i am wrong.

Thanks,
Harshit Garg
8909685434
harshitgarg2591@gmail.com
Wilson Busaka 3Wilson Busaka 3
Hi Harshit,

Yes, but when Account Name is updated.
my current code is below:
trigger AccountOppChecker on Account (after update) {
    List<Opportunity> opp = new List<Opportunity>();
    for(Account a: Trigger.new){
        Opportunity o = [SELECT Id__c, Name FROM Opportunity 
                         WHERE Id =: a.OpportunityId__c];
        o.name = a.name;
        opp.add(a);
    }
    update opp;
}

Thanks.
smriti sharan19smriti sharan19
UNDERSTAND LOGIC
1.Based on connect_america_account__c on account, make changes connect_america_account__c on  contact
A. trigger will be written on account
2. Will it be before or after trigger?
A.After Trigger are used to perform logic on the related objects so it is after trigger
3. How are account and contact related ?
A.They are related based on accountid on contact
4. Which trigger context variable will be used?
update

FINAL CONCEPT
trigger UpdateConnectAmericaTrigger on Account (after update){
List<Contact> newCList = new List<Contact>();
Set<Id> setaccountId = new Set<Id>();

for(Account acc : Trigger.new)
  {
    if(acc.connect_america_account__c!=Trigger.oldMap.get(acc.id).connect_america_account__c 
    && acc.connect_america_account__c == true
    ){
      setaccountId.add(acc.id);
    }
  }
  
List<Contact> conlist =[Select id, connect_america_account__c,accountid from contact where accountid IN: setaccountId];
for(contact c: conlist){
c.connect_america_account__c = true;
newClist.add(c);
}
update newClist;
}
Kalyani GadeKalyani Gade

ABC Firm needs to make the service desk more productive. They have an idea to create Case Solutions as a reference guide for customers and service agents if any case resolution is accepted by the customer.

Create a custom object ‘Case Solution’ with fields Case Description, Resolution.
- Create a custom field ‘Accept Solution’ as a checkbox on Case object.
- Create a custom field ‘Resolution’ as a text area on Case Object.
- Whenever a case has the status ‘Closed’ and its ‘Accept Solution’ checkbox is selected; a new Case Solution record should be created.

please share code for this problem statement .