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
PugetSoundJimPugetSoundJim 

Why am I getting these errors? "Variable does not exist. Illegal assignment from Schema.SObjectField to String"

So I am writing a class to pull out community login data from the user object to populate a custom object which will be used to create reports for customers, since community users are unable to access the user object.

Here's what I have so far.  Thinking this should work but I'm getting an error when populating the data into the custom object.
 
global class CommunityLoginHistory implements Schedulable {
    
    // This method queries the LoginHistory Object for Community User logins in the past 15 minutes and populates the CommunityLoginHistory custom object.
 
    global void execute(SchedulableContext ctx) {
        DateTime d = Datetime.now().addMinutes(-15);
        List<User> UserLoginHistory = [SELECT Id,
                                              name, 
                                              LastLoginDate, 
                                              profile.name, 
                                              userrole.name, 
                                              account.name 
                                            FROM User 
                                            WHERE profile.name LIKE '%Customer Community Plus%' 
                                            AND isactive = true 
                                            AND LastLoginDate > :d ];
                                            
        if ( !UserLoginHistory.isEmpty()) {
            List<CommunityLoginHistory__c> clh = new List<CommunityLoginHistory__c>();
            for (User ulh : UserLoginHistory) {
                clh.add (new CommunityLoginHistory__c ( UserID__c           =   Id,
                                                        UserName__c         =   name,
                                                        LastLoginDate__c    =   LastLoginDate,
                                                        UserProfile__c      =   profile.name,
                                                        UserRole__c         =   userrole.name,
                                                        UserAccount__c      =   account.name
                ));
            }
            insert clh;
        }
    }
}
In the for loop, when trying to populate the record with the data that was pulled from the SOQL query, the Developer Console is giving me the following errors.

Line 21 - Variable does not exist: Id - Illegal assignment from Schema.SObjectField to String
Line 22 - Variable does not exist: name - Illegal assignment from Schema.SObjectField to String
Line 23 - Variable does not exist: LastLoginDate - Illegal assignment from Schema.SObjectField to String

I'm at a loss for why that is the case when the console doesn't complain about the other variables.  Hoping someone can help.
Vincent AlbiserVincent Albiser
Hello,

Add ulh. before the fields you're iterating on :
 
UserID__c                =   ulh.Id,
UserName__c              =   ulh.name,
LastLoginDate__c         =   ulh.LastLoginDate,
UserProfile__c           =   ulh.profile.name,
UserRole__c              =   ulh.userrole.name,
UserAccount__c           =   ulh.account.name
Regards,
 
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Jim,

as vicent has mentined, you need to add the variable name right before the field.
I would also say that you need to check if the clh list is empty or not and then put the DML on it.

Try using the following class,
 
global class CommunityLoginHistory implements Schedulable {
    
    // This method queries the LoginHistory Object for Community User logins in the past 15 minutes and populates the CommunityLoginHistory custom object.
 
    global void execute(SchedulableContext ctx) 
    {
        DateTime d = Datetime.now().addMinutes(-15);
        List<User> UserLoginHistory = [SELECT Id,
                                              name, 
                                              LastLoginDate, 
                                              profile.name, 
                                              userrole.name, 
                                              account.name 
                                            FROM User 
                                            WHERE profile.name LIKE '%Customer Community Plus%' 
                                            AND isactive = true 
                                            AND LastLoginDate > :d ];
                                            
        if ( !UserLoginHistory.isEmpty()) 
        {
        
            List<CommunityLoginHistory__c> clh = new List<CommunityLoginHistory__c>();
            
            for (User ulh : UserLoginHistory) 
            {
                clh.add (new CommunityLoginHistory__c ( UserID__c           =   ulh.Id,
                                                        UserName__c         =   ulh.name,
                                                        LastLoginDate__c    =   ulh.LastLoginDate,
                                                        UserProfile__c      =   ulh.profile.name,
                                                        UserRole__c         =   ulh.userrole.name,
                                                        UserAccount__c      =   ulh.account.name
                ));
            }
            
           try{
               If(!clh.IsEmpty()){
                    insert clh;
               }
           }
           Catch(Exception e){
               System.debug('Thrown Exception Is:: ' + e.getMessage());
           }
        }
    }
}