Toggle Side Panel

  • Blogs
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › Write a Salesforce Trigger for account name should be a combination of lastnames of all its related contacts

    Tagged: Account Detail, Contact Record, Record Processing, Salesforce Customization, Salesforce Trigger

    • Salesforce® Discussions

      Write a Salesforce Trigger for account name should be a combination of lastnames of all its related contacts

      Posted by Raghav Chaturvedi on June 22, 2017 at 6:58 AM

      Write a trigger for account name should be a combination of lastnames of all its related contacts. eg. if I have two contacts 1. test name 2. test name2 in an account. In this case account name should be "name name2". If I add a new contact "test name3" to this account, now account name should be "name name2 name3

      Raghav Chaturvedi replied 9 years, 4 months ago 2 Members · 9 Replies
      • Account Detail
      • Contact Record
      • Record Processing
      • Salesforce Customization
      • Salesforce Trigger
    • 9 Replies
    • Sudhir

      Member
      June 22, 2017 at 3:59 PM

      Check this out.. UpdateAccountNameFromContactLastName

      Update as per your trigger design template.

    • [adinserter block='9']
    • Raghav Chaturvedi

      Member
      June 23, 2017 at 1:33 AM

      sir in your trigger account name is updated by contact last name but i want the combination of both account name and contact last name

    • Raghav Chaturvedi

      Member
      June 23, 2017 at 2:38 AM

      trigger contactlastName on Contact (after insert,after update)
      {
      list lst=new list();
      for(contact c:Trigger.new)
      {
      lst.add(c.accountId);
      }
      list lac=new list();
      list lacc=[select Id,Name,(select Id,lastName from contacts) from account where Id in:lst];
      for(account a:lacc)
      {
      for(contact con: a.contacts)
      {
      a.Name=a.Name+' '+ con.lastName;
      lac.add(a);
      }
      }
      update lac;
      }

      this trigger works only one time,after run this trigger second related contact is not created,plz help

    • Sudhir

      Member
      June 23, 2017 at 7:35 AM

      I see that this is not mentioned this (account name should be combination of both account name and contact last name) in your first post here. Please make sure you have your requirement clear in the first instance.

      So your Account name should be "AccountName ContactLastName1 ContactLastName2 ContactLastName3 and so on" ?  Confirm and then I will update the code and post.

    • Sudhir

      Member
      June 23, 2017 at 8:48 AM

      Based on the assumption in my previous post, updated the code here..UpdateAccountNameFromContactLastName2

       

      This will work in all scenario - Insert, Update, Delete, Undelete of a Contact on an Account.

      • This reply was modified 9 years, 4 months ago by  Sudhir.
    • Raghav Chaturvedi

      Member
      June 23, 2017 at 8:48 AM

      yes in a account(Raghav),if we have 1 contact(lastNamme=kumar) then account name should we updated by(Raghav Kumar) and again we add a contact(lastnam=sharma)then account nae should we raghav kumar sharma and so on.

      this is my question

    • Sudhir

      Member
      June 23, 2017 at 8:50 AM

      Enjoy the updated code I posted a while ago.  🙂

    • Sudhir

      Member
      June 23, 2017 at 12:16 PM

      Did the solution work for you..

    • Raghav Chaturvedi

      Member
      June 23, 2017 at 12:28 PM

      yes and thanks

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    Popular Salesforce Blogs

    No Code Tableau and Salesforce Integration

    Blog in Salesforce integration

    No Code Salesforce integrations are the best way to seamlessly integrate various software and platforms to boost productivity and enhance overall performance. With the new…

    Authentication Tab, Authentication Type, Best Practices, Boost Productivity, Businesses
    Apphienz Feb 1, 2022
    2,737  Views
    Driving engagement and Sales using Salesforce Marketing Cloud and Pardot

    Driving engagement and Sales using Salesforce Marketing Cloud and Pardot

    Blog in Others

    Digital marketing has seen explosive growth in the recent years due to the high adoption of digital technologies. According to Mary Meeker’s Internet Trends Report…

    Automation Software, B2B Marketing, B2C Marketing, Customer Experiences, Email Campaigns
    LevelShift (formerly DemandBlue) Aug 21, 2018
    3,309  Views
    Businesses Need Salesforce

    Why Do Businesses Need Salesforce in 2021?

    Blog in Salesforce

    As a cloud-based CRM platform, Salesforce provides businesses with the means to manage their events and tasks and track customer cases. It also allows the…

    Applications, Business, Business System, Businesses, Cloud Based CRM
    Aziz Nicleson Oct 20, 2021
    3,746  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.