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;
}