Activity Forums Salesforce® Discussions How to add last name of contact to its related account's name, when any contact field is updated using triggers?

  • How to add last name of contact to its related account's name, when any contact field is updated using triggers?

    Posted by shariq on July 17, 2017 at 12:32 pm

    I want to write a trigger(after) on contact which will add a last name to its account name after any contact field is updated?

    Parul replied 5 years, 6 months ago 4 Members · 3 Replies
  • 3 Replies
  • Raghav Chaturvedi

    Member
    July 17, 2017 at 12:47 pm

    you serach this question on my profile

  • Aman

    Member
    July 17, 2017 at 1:54 pm

    Hello Shariq,

    trigger AddNameAcc on Contact (after update,after insert)
    {
    List<contact> con = [SELECT Id,LastName, Account.Name, Account.Id FROM Contact WHERE Account.Id != null AND Id IN : Trigger.new] ;
    List<Account> acc = new List<Account>();
    for(Contact s : con)
    {
    Account a = new Account();
    a.Id = s.AccountId;
    a.Name = s.Account.Name+s.LastName;
    System.debug('aaa'+a.Id);
    acc.add(a);
    }
    update acc;
    }

  • Parul

    Member
    September 16, 2018 at 7:06 am

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

    List<Id> AccID = New List<Id>();
    for(Contact con : Trigger.new){
    if(con.AccountId != null){
    AccID.add(con.AccountId);
    }
    }

    List<contact> con = [SELECT Id,LastName, Account.Name, Account.Id FROM Contact WHERE Account.Id != null] ;
    List<Account> acc = new List<Account>();
    for(Contact s : Triger.new)
    {
    Account a = new Account();
    a.Id = s.AccountId;
    a.Name = s.Account.Name+s.LastName;
    System.debug(‘aaa’+a.Id);
    acc.add(a);
    }
    update acc;
    }

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos