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
AntjeAntje 

Trigger Help Needed - Across multiple objects

Hi Everybody,
I could use some help on my trigger. I am trying to copy some fields from two standard objetcs into one custom object (dataTable). I am trying to copy several fields from Opportunities as well as some fields from Tasks (if the task is related to opportunity) into my custom object. I am new to apex and could really use some help. I figured out one part of the equation but I am not sure how I would include a trigger that works on the second object. The end result should be that all the fields from both objects are in th same record. I am pretty sure I will need to use SOQL but I can't seem to find exactly what I need. Here is what I got so far. THANK YOU

Trigger AutoDT on Task (after insert,after update) {
List <DataTable__c> newdt = new List <DataTable__c> ();
for (Task t : Trigger.new) {
DataTable__c dts = new DataTable__c ();
dts.TaskSubject__c = t.subject;
dts.TaskStatus__c = t.status;
dts.TaskPriority__c = t.priority;
dts.TaskComments__c = t.Description;

dts.taskDateTaskAssigned__c = t.date_task_assigned__c;
dts.TaskDueDate__c = t.ActivityDate;

newdt.add (dts);
}
insert newdt;
}
sandeep@Salesforcesandeep@Salesforce
Hi Antje, 

You have mentioned that you need to copy data from two different objects in sngle obj but you have not mentioned on which operation you want to do it. IF you want it on both different object's insert/update or any other manupulation then you need to write trigger on these two objects separately. 

OR if you have any relationship among these three objects then we can think in that way so please finalize that event then it would be very easy to execute it in same SOQL execution.

Thanks
Sandeep Singhal
AntjeAntje
Hi Sandeep,

Yes, I have a relationship between tasks and opportunities. If tasks are "related to" (t.what) opportunities then I want to take some of the fields from tasks and copy them into my cusom object. My cusom object has no relationship to tasks or opportunities. I don't want to write a trigger seperately since that would create "numerous records" in my custom object. I would like them to be execute in the same trigger to be in one record on my custom object. Thanks for you help. It is much appreciated.  
KevinPKevinP
First things first, thank you for your clear question and your obvious attempt at a solution before asking. A lot of the time we just get "write my trigger!" I would ask that in the future you use the <> button on the menubar above to insert code so that we get syntax highlighting, line numbers and indentation. 

Not knowing exactly how your third object is related is going to hamper my answer a bit. Here's what you need to do conceptually:
  1. run a soql query to identify your third object's records that are related to this task that has just been written. (I suspect though, you want your trigger to fire on opportunity insert/update not task. 
  2. create a table table object like you do on line 4, and associate your data from the task and the third object to it.
SOQL queries in apex are actually pretty straightforward. Here are some ground rules:
  1. Always assume your query will return multiple rows unless you use LIMIT 1
  2. therefore query into a list or a map
  3. SOQL queries have (at least) 3 clauses - Select, From and Where
    1. The Select clause starts with SELECT and specifies the fields returned separated by commas.  ie: SELECT Id, name 
    2. The From Clause starts with FROM and specifies what table/object should be searched. ie: Account
    3. The Where clause starts with WHERE and determines what records are to be included or excluded.
  4. All together a simple query would look like this:
    1. SELECT Id, Name FROM Account WHERE firstName like 'Kev%' 
    2. This asks only for the id and name fields from the account object where the record's first name field starts with Kev.
You will want to modify this query to match whatever relationship id's you have and to ask for the fields and objects your looking for.