I have one custom object collegeplan__c and there have couple of record there for particular record for example degreecourse record name is there and xyz is the owner of that record. someone created task under this record then i want ti update parent record lastmodified by user name. suppose abc has created one task under this then last modified by should be ABC.

I am geeting this error "A non foreign key field cannot be referenced in a path expression: Tasks"

  List<CollegePlan__c> Plans = new List<CollegePlan__c>([SELECT Id,LastModifiedBy.Id,(Select Id,OwnerId From Tasks) FROM CollegePlan__c WHERE Id In :PlanIDs ] ); 
        List<CollegePlan__c> updList = new List<CollegePlan__c>();
        for(CollegePlan__c cop : Plans){   
            CollegePlan__c Plan = new CollegePlan__c();
            Plan.Id = cop.Id;
            **Plan.LastModifiedBy.Id = cop.Tasks.OwnerId;**      
        if(updList.size() > 0 )
            update updList;
  • You may have to use custom fields, LastModifiedBy is a system field and can't/should be updated by us.
    – Raul
    Commented May 25, 2020 at 19:40

1 Answer 1


You can't write to LastModifiedById. Even if you could, this line

Plan.LastModifiedBy.Id = cop.Tasks.OwnerId

wouldn't work for a couple of reasons:

  1. Changes to a related object (LastModifiedBy) don't get persisted when you update the parent (update updList). You have to update each sObject record you modify - but you can't use an update on a related object to change the relationship anyway. You'd have to assign to LastModifiedById.
  2. Tasks is a relationship name for a one-to-many relationship. It does not have an OwnerId field.

One very easy way to achieve this is to have your Task trigger write to a field on the parent object - any field, any value. This will have the natural side effect of updating LastModifiedById to the context user, who in many situations will be the owner of the Task (of course, if your business processes allow people to create and assign Tasks to others, that's not quite as elegant). You could create a lookup MostRecentlyUpdatedTask__c, for example, and just populate a Task Id there via your trigger.

Not the answer you're looking for? Browse other questions tagged .