[SalesForce] How to relate WhatID field of Task object with custom object

hi I am learning Salesforce. I have created trigger for my custom object. I have an error the following type:

Error: Compile Error: No such column 'Theme__c' on entity 'Task'. If
you are attempting to use a custom field, be sure to append the '__c'
after the custom field name. Please reference your WSDL or the
describe call for the appropriate names. at line 14 column 44

My code:

trigger Status_completed on Transport__c (before update) {

  Set<id> taskids = new Set<Id>();

  for (Transport__c  u : Trigger.new) {

    if ( u.Status__c  = 'Maintainace finished')
    {
          if(u.TaskLookupField!=null)
            taskids.add(u.TaskLookupField);
    }
  }

 List<Task> taskstoupdate = new List<Task>([Select id,Theme__c from Task 

where    

id in: taskids]);

 for(Task t : taskstoupdate){

if(t.Theme__c=='Revision')
 t.Theme__c='Complete';    
}

 update taskstoupdate;

}

I think I have to relate my transport object with the Task object through the field Whatid in the select statement, but I dont know how to do it . I cant find corresponding field in my custom object Transport should I created it
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm#topic-title

I have doubts can I relate this field whatId with my custom field directly or I have to do it through the Account as intemediate object ?

Best Answer

If your aim is to update Tasks that are related to to your Transport__c custom object, then the Task WhatId field will contain the Id values of any related Transport__c objects. So to change the status of the Task objects when the Transport__c object statuses change you would use code like this:

trigger Status_completed on Transport__c (after update) {

    Set<Id> ids = new Set<Id>();
    for (Transport__c  t : Trigger.new) {
        Transport__c old = Trigger.oldMap.get(t.Id);
        if (t.Status__c != old.Status__c && t.Status__c == 'Maintainace finished') {
           ids.add(t.Id);
        }
    }

    if (ids.size() > 0) {
        Task[] tasks = [
                select Id from Task
                where WhatId in :ids
                and Status != 'Completed'
                ];
        for (Task t : tasks) {
            t.Status = 'Completed';
        }
        update tasks;
    }
}
Related Topic