0

this is my trigger code to insert new employee record if its not exist with user.but am getting below error. Error: Compile Error: Comparison arguments must be compatible types: String, Set ,help me to resolve..

trigger SyncUserToEmployee  on User (After insert, after update) {
SET<id> empid=new SET<id>();
list<Employee__c> insertemp=new list<Employee__c>();
for (user u:trigger.new)
{
empid.add(u.EmployeeNumber);
}
map<id,Employee__c> em=new map<id,Employee__c>([select id from Employee__c where id IN:empid]);
for(user u:trigger.new)
{
 if(u.EmployeeNumber!=empid)
{
Employee__c e =new Employee__c();
e.First_Name__c=u.Name;
e.Email__c=u.Email;
e.Phone_Number__c=u.Phone;

insertemp.add(e); 
 }
}
 insert insertemp;
 }
3

Your problem is on this line:

if(u.EmployeeNumber!=empid)

The reason is that u.EmployeeNumber is a Text(20) field, and empid is a Set<> that you declared earlier:

SET<id> empid=new SET<id>();

To fix it you can change this set to a String set and then use contains() method instead of ==:

if (!empid.contains(u.EmployeeNumber)) {
    // do your logic if this EmployeeNumber is not in this set
}
1
  • thank you @novag . now its working but how can i synchronize new created employee with user?..
    – lathish
    Apr 15 '16 at 5:43
1

It appears from the error message that the field EmployeeNumber is a string, not an ID which makes sense. As such, this line:

SET<id> empid=new SET<id>(); 

needs to be changed to a string as follows:

SET<string> empid=new SET<string>();

A string will cast to an Id when you do the insert later.

0

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.