I think your issue is versioning, Try something like this:
var documents = DocumentHelper.GetDocuments().Types("article");
foreach (TreeNode document in documents) {
if (document.IsPublished && !document.IsCheckedOut) {
// Remove leading and trailing white spaces from the academic name
var firstName = document.GetValue<String>("FirstName", string.Empty).Trim();
var lastName = document.GetValue<String>("LastName", string.Empty).Trim();
document.CheckOut();
document.SetValue("FirstName", firstName);
document.SetValue("LastName", lastName);
document.Update();
document.CheckIn();
}
document.Publish("Published via RemoveWhitespaces scheduled task.");
}
string detail = "Executed from '/App_Code/CustomScheduledTasks.cs'. Task data:" + ti.TaskData;
EventLogProvider.LogInformation("RemoveWhitespaces", "Execute", detail);