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
dujsudujsu 

How do I reference a custom field in an unrelated object within a trigger?

Hello - I am trying to figure out how to set a custom field value that resides within the Activity object to the value of Task.ActivityDate. I am not sure how to reference the custom field (Activity.Activity_Due_Date__c) properly in my for loop below.

trigger GetUsableTaskDueDate on Task (before insert, before update) {
    Task[] checkTasks = Trigger.new; 
    for(Task t : checkTasks){ 
        Activity.Activity_Due_Date__c = t.ActivityDate;
    }
}

This is my 4th day learning SF and my first real task. I am a 12 year veteran web app developer and this task seems like it should be trivial.

All suggestions are appreciated.

Thanks!
Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

Actually.... I believe Activities are really just Tasks.  Are you trying to set the field of your current task, since it could also be an activity?  So are you essentially trying to take 1 field and copy it to another?

 

 

trigger GetUsableTaskDueDate on Task (before insert, before update)
{
  Task[] checkTasks = Trigger.new; 
  for(Task t: checkTasks)
  {
    t.Activity_Due_Date__c = t.ActivityDate;
  }
}

 

All Answers

TheIntegratorTheIntegrator

As far as I know there is no object called Activity, there is Event and Task. Are you trying to set the value of a custom field in Event? You can do this by querying the Event object, but in my opinion you should have some criteria to select which event record you want to update based on the task record in the trigger.

 

Let me know and I can help you update this code.

Damien_Damien_

Where are you getting this Activity from?

 

I'm not quite sure what you are trying to do.  A more specific use case might be helpful.  Are you trying to create a new Activity from a Task?  If so something like...

 

trigger GetUsableTaskDueDate on Task (before insert, before update)
{
  List<Activity> activities = new List<Activity>();
  Task[] checkTasks = Trigger.new; 
  for(Task t: checkTasks)
  {
    Activity newAct = new Activity();
    newAct.Activity_Due_Date__c = t.ActivityDate;
    activities.add(newAct);
  }
  insert activities;
}

 

Damien_Damien_

Actually.... I believe Activities are really just Tasks.  Are you trying to set the field of your current task, since it could also be an activity?  So are you essentially trying to take 1 field and copy it to another?

 

 

trigger GetUsableTaskDueDate on Task (before insert, before update)
{
  Task[] checkTasks = Trigger.new; 
  for(Task t: checkTasks)
  {
    t.Activity_Due_Date__c = t.ActivityDate;
  }
}

 

This was selected as the best answer
dujsudujsu

Hey Damien_,

 

Thank you for your replies. You are spot on, I just got it to work and have exactly what you wrote.

 

So when you say "I believe Activities are really just Tasks" - what does that mean? I checked for a relationship between the objects but was unable to find anything documented.. 

 

Thanks again.

Damien_Damien_

In your Salesforce Instance

 

Setup -> customize -> Activities

 

Tasks/events/activities are all under this.

 

If you add a field to the Activity Custom fields here, then go look at your Schema in Eclipse.  Look up Tasks, and you will see the new field there.  There is no Activity Table in the database, only Task.  (You will need to make sure you refresh it after adding the custom fields.)

dujsudujsu

Thanks again. How do I view my Schema through Eclipse? I can see the Schema through the Admin, but the custom field resides in the Activity Object. Here's a screen shot..

 

 

 

Damien_Damien_

There is no Activity object.  The Custom field actually gets added to the Task object.  Under your probject in Eclipse, you will see Salesforce.schema.  Just double click that file.

dujsudujsu

Ok - thanks for the tip. I can see Activity_Due_Date__c in the Task object looking at the schema through Eclipse. And it looks like all the "Activity" fields are in there as well. Why would "Activity" show up, as what "looks" like as a separate object in the Schema Builder through the application? I was hoping to show you a screen shot of what I see but Im having issues getting it loaded. Looking through the forum Help to see what Im doing wrong now..

dujsudujsu

Here is what is confusing me in the Schema Builder..It shows the Activity Due Date (Activity_Due_Date__c) under "Activity"...

 

 

Damien_Damien_

Just think of them as a single object, which Activity extends Task.  So an Activity is actually a Task, but a Task isn't necessarily an activity.

dujsudujsu

Thanks again for your response. Did you just try to use a Jedi mind trick on me? :) To me if the custom fields are task related, they should show up under the Task Object in the Schema Builder. Not under an object that doesn't exist. Anyway, I get what you are saying, I will use Eclipse going forward for my schema viewing needs.