function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfadm sfadmsfadm sfadm 

Error: Illegal assignment from Schema.SObjectField to Id

Hello,

I have the following error message:
Error: Illegal assignment from Schema.SObjectField to Id
when I try to update lookup field in apex.

Here is the method implementation:
public void addTaskCommentHistory(Task task) {
    if(task == Null) {
            return;
    }

    String newValue = task.Description;

    if(newValue != Null && !newValue.equals('')) {
        String taskId = task.Id;
        String fieldName = 'Comments';
        String oldValue = '';

        Task_History__c taskHistory = new Task_History__c();
        taskHistory.Task_ID__c = taskId;
        taskHistory.Field_Name__c = fieldName;
        taskHistory.Old_Value__c = oldValue;
        taskHistory.New_Value__c = newValue;


        String lastModifiedBy = task.LastModifiedById; 
        String userName = '';
        // the query will return no more than one row because the data is extracted by user id
        List<User> listOfUsers = [SELECT Name FROM User where Id = :lastModifiedBy];
        if(listOfUsers.size() > 0){
            User user = listOfUsers.get(0);
            userName = user.Name;
        }

        taskHistory.User__c = user.Name;

        insert taskHistory;
    }    
}

When I try to add
user.Name
to
taskHistory.User__c
in
taskHistory.User__c = user.Name;
I get the "Error: Illegal assignment from Schema.SObjectField to Id" message.

Please advise how to update the User__c lookup field without the error message?


 
Best Answer chosen by sfadm sfadm
Lokeswara ReddyLokeswara Reddy
taskHistory.User__c = user.Id;
You have to assign user id instead of user name, update your query to get id and pass it.

Lokesh...
 

All Answers

Lokeswara ReddyLokeswara Reddy
taskHistory.User__c = user.Id;
You have to assign user id instead of user name, update your query to get id and pass it.

Lokesh...
 
This was selected as the best answer
v varaprasadv varaprasad
Hi,

Please check the updated code below : 
 
public void addTaskCommentHistory(Task task) {
    if(task == Null) {
            return;
    }

    String newValue = task.Description;

    if(newValue != Null && !newValue.equals('')) {
        String taskId = task.Id;
        String fieldName = 'Comments';
        String oldValue = '';

        Task_History__c taskHistory = new Task_History__c();
        taskHistory.Task_ID__c = taskId;
        taskHistory.Field_Name__c = fieldName;
        taskHistory.Old_Value__c = oldValue;
        taskHistory.New_Value__c = newValue;


        String lastModifiedBy = task.LastModifiedById; 
        Id userId = [SELECT id FROM User where Id = :lastModifiedBy].id;
	    taskHistory.User__c = userId;

        insert taskHistory;
    }    
}

Hope this helps you!

 

Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com