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;
}
  • 6
    There are a lot of logic problems. What are you trying to do? – Martin Lezer Mar 15 '17 at 15:07
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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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