0

i am trying to copy values from a custom field in account object and trying to create topics based on those values. Trigger is working fine. But if there are any old Topics are inserting then it is throwing error.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger InsertTopic caused an unexpected exception, contact your administrator: InsertTopic: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, A topic with the name, 54644, already exists.: [Name]: Trigger.InsertTopic: line 19, column 1

trigger InsertTopic on Account (after insert) {

List<TopicAssignment> taList = new List<TopicAssignment>();
List<TopicAssignment> taList1 = new List<TopicAssignment>();
List<Topic> topicList= new List<Topic>();

for(Account c : trigger.new)
{
String d1= c.Topics__c; // Custom field on Account Object. 
system.debug('aaaaaaaaaa'+d1);
String[] d= d1.split('\\s*,\\s*');
for(String s: d)
{
  Topic ta1 = new Topic();
   ta1.Name=s;
   topicList.add(ta1);
}
 system.debug('aaaaaaaaaa'+topicList);
insert topicList;

for(Topic s1: topicList)
{
TopicAssignment ta = new TopicAssignment();
   ta.TopicId=s1.Id;
   taList.add(ta);
   ta.EntityId = c.Id;
}

insert taList;    
}
}

How can i overcome that error and still need that topic to be assigned to the record. Any ideas?

3
  • Is the name field from the topic object set to unique values, or do you have any active rules on your org that doesn't allow duplicates for the topic objects? If so you could perhaps disable them and try again. Aug 25, 2016 at 8:31
  • There are no active rules in my Org. It's the feature from Topics , If any duplicate topic in inserting it will throw error as that topic is already existing. If we manually enter them it will suggest us the topic with that name , so there wont be any chance for Errors. Aug 25, 2016 at 8:38
  • Duplicate Topics are not allowed. You can check following link as a reference help.salesforce.com/apex/…. As @MarcZaharescu has suggested, you can modify the trigger logic to create Topic which are not present in existing system.
    – Devendra
    Aug 25, 2016 at 10:34

1 Answer 1

0

So from my understanding the problem you are facing occurs when there already are Topics on your org with that name. Hence I suggest you to check this case before you try to create a new Topic.

This would look something like:

trigger InsertTopic on Account (after insert) {

List<TopicAssignment> taList = new List<TopicAssignment>();
List<TopicAssignment> taList1 = new List<TopicAssignment>();
List<Topic> topicList= new List<Topic>();

for(Account c : trigger.new)
{
String d1= c.Topics__c; // Custom field on Account Object. 
system.debug('aaaaaaaaaa'+d1);
String[] d= d1.split('\\s*,\\s*');

List<Topic> existingTopics = [Select id,name from Topic where name in: d];

for(String s: d)
{
   if(existingTopics.size()>0
    {
      Topic ta1 = existingTopics[0];
    }
   else
    {
      Topic ta1 = new Topic();
      ta1.Name=s;
    }


   topicList.add(ta1);
}
 system.debug('aaaaaaaaaa'+topicList);
insert topicList;

for(Topic s1: topicList)
{
   TopicAssignment ta = new TopicAssignment();
   ta.TopicId=s1.Id;
   taList.add(ta);
   ta.EntityId = c.Id;
}

insert taList;    
}
}

Note: I haven't tried out the code so there might be some syntax errors

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .