-3

Here how can avoid duplicate lastname

trigger ContactNamesOnAccount on Contact (after update, after insert) {    
  Set<id> accIdList = new Set<id>();
  for(Contact con : Trigger.new){
    accIdList.add(con.accountid);
  }

  List<Account> accUpdateList = new List<Account>();
  List<String> names = new List<String>;

  for(Account acc : [Select id, Contact_Names__c, (Select LastName From Contacts) From Account Where Id In : accIdList]){
    for(Contact con : acc.contacts){
      if(con.LastName != null){
        /* add name to list */
        names.add(con.LastName);
      }
    }

    /* update name separating ', '  */
    acc.Contact_Names__c = String.join(names, ', ');;
    accUpdateList.add(acc);

    /* clear list to add new account contact names */
    names.clear();
  }    
  update accUpdateList;
}

closed as unclear what you're asking by Pranay Jaiswal, Mohith Shrivastava, Boris Bachovski, Reshma, RedDevil May 8 at 19:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    Why not use standard salesforce feature Duplicate Management for this instead of custom code. – Raul May 2 at 10:07
  • What are you trying to do ? Do you want duplicates to be avoided in Contact_Names__c field ? or Do you want to avoid duplicate last names under same account ? Please edit your question with the necessary details. – Vijay Ganji May 2 at 10:10
  • Yes i need to avoid duplicate Contact_Names__c if i have two same names need to populate only one – sree May 2 at 10:44
  • @sree Updated the answer, that should work for you. – Vijay Ganji May 2 at 11:42
  • 2
    I've downvoted this question because you haven't made it clear what the problem is. "Duplicate last names" alone makes it sound like you're having trouble inserting Contact records. If Vijay's answer is right, a much better description of your problem would have been something like "I'm concatenating the last names of all Contacts related to a given Account. If two Contacts have the same last name, the concatenation contains that last name twice. How can I make it so that the concatenation only contains unique names?" – Derek F May 2 at 12:50
2

As per your above comments, you want to avoid duplicates just in Contact_Names__c field of Account which is getting calculated in your trigger. So in order to do that, replacing List<String> with Set<String> should do the trick i.e. instead of using List to collect all the Last Names, you could use Set which will remove the duplicates as per its design.

Replace below line

List<String> names = new List<String>();

with

Set<String> names = new Set<String>();

So, once the duplicates are removed using Set, now you will need to concatenate these last names using comma. As you are using the String's Join method to do this, it won't accept Set as a parameter, hence you will have to convert this Set to List only for this operation which can be done as shown below.

When using String.Join, replace below code

acc.Contact_Names__c = String.join(names, ', ');

with

acc.Contact_Names__c = String.join(new List<String>(names), ', ');

So, your overall trigger would look something like this

trigger ContactNamesOnAccount on Contact (after update, after insert) {    
    Set<id> accIdList = new Set<id>();
    for(Contact con : Trigger.new){
        accIdList.add(con.accountid);
    }

    List<Account> accUpdateList = new List<Account>();
    Set<String> names = new Set<String>();

    for(Account acc : [Select id, Contact_Names__c, (Select LastName From Contacts) From Account Where Id In : accIdList]){
        for(Contact con : acc.contacts){
            if(con.LastName != null){
                /* add name to list */
                names.add(con.LastName);
            }
        }

        /* update name separating ', '  */
        acc.Contact_Names__c = String.join(new List<String>(names), ', ');
        accUpdateList.add(acc);

        /* clear list to add new account contact names */
        names.clear();
    }    
    update accUpdateList;
}
  • Hi if i updated some picklist value Active to InActive i need to remove perticular field – sree May 15 at 13:27
  • @sree if you have any further questions, I would recommend you to post a separate question for that. – Vijay Ganji May 15 at 13:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.