0

I have the following piece of code that I am having some issues with it and I'm trying to update an existing record where the field Emp_Message__c with the new message, the messages can be one or multiple and I try to demonostrate in the code below.

I understand it's complaining that I have added duplicate Id in the List but wondering how would I correct this issue or better approach?

Error:

FATAL_ERROR|System.ListException: Duplicate id in list: a1aV0000001ATBCTA4

List<String> messages = new List<String>();
messages.add('one');
messages.add('two');
messages.add('three');


List<Employee__c> emps = new List<Employee__c>();
emps = [SELECT Id,Emp_Message__c FROM Employee__c WHERE Id =: empId];

for(String msg : messages)
{
   Employee__c emp = new Employee__c(); 
   emp.Id = empId;           
   emp.Emp_Message__c = msg;
   emps.add(emp);
}

if(!emps.isEmpty()) {
   update emps;
}
1
  • 6
    There are a lot of logic problems. What are you trying to do? Commented Mar 15, 2017 at 15:07

1 Answer 1

2

If what you want is to concatenate all the messages and append them to the existing message in the record, you could do this:

List<String> messages = new List<String>();
messages.add('one');
messages.add('two');
messages.add('three');

Employee__c emp = [SELECT Id,Emp_Message__c FROM Employee__c WHERE Id =: empId limit 1 ];
emp.Emp_Message__c += ',' + String.join(messages, ',');
update emp;
0

You must log in to answer this question.

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