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 

Apex Trigger Not Working :(

Hi Everbody,
I am new to apex, so please be patient. This is my first try at coding. Any help I could get is very much appreciated. I created a custom object (DataTable). This object does not have any parent/child relationships. I am trying to "copy" some information from Tasks into this custom object if the Task is related to an Opportunity. For now, all I want is to populate a field ( called TaskSubject__c) in my custom object that is equal to Subject on the Task/Activity object. Thank You! Here is what I got....

trigger AutoCreateDT on Task (after insert) {
for (Task t : Trigger.new) {
// Check if Task is related to Opportunity
if (t.what =='Opportunity') {
//Populate task subject in custom object called DataTable
DataTable d = new DataTable ();
d.TaskSubject__c = t.subject;

}
}
}
Best Answer chosen by Antje
Blake TanonBlake Tanon
You're not executing any DML operations (creating the new data table), there are some other considerations but they might fall outside of the scope.  See below, this example should work.
 
trigger AutoCreateDT on Task(after insert){

    //create a list to hold the new data table records - so this trigger will work in bulk
    list<Data_Table__c> tables = new list<Data_Table__c>();

    for(task t:trigger.new){

        if(t.whatId != null && t.Subject != null && t.WhatId.getSobjectType() == 'Opportunity'){
            //add a new data tables to the list
            tables.add(new Data_Table__c(TaskSubject__c = t.subject));

        }

    }
    
    if(tables.size() > 0){
        //create the new tables
        insert tables;

    }

}

 

All Answers

Blake TanonBlake Tanon
You're not executing any DML operations (creating the new data table), there are some other considerations but they might fall outside of the scope.  See below, this example should work.
 
trigger AutoCreateDT on Task(after insert){

    //create a list to hold the new data table records - so this trigger will work in bulk
    list<Data_Table__c> tables = new list<Data_Table__c>();

    for(task t:trigger.new){

        if(t.whatId != null && t.Subject != null && t.WhatId.getSobjectType() == 'Opportunity'){
            //add a new data tables to the list
            tables.add(new Data_Table__c(TaskSubject__c = t.subject));

        }

    }
    
    if(tables.size() > 0){
        //create the new tables
        insert tables;

    }

}

 
This was selected as the best answer
AntjeAntje
Blake,
Thank You very much! This seemed to do the job. I most certainly apprecite you helping me out. 
AntjeAntje
?Thank you very much for helping me out on this last week. You are a life saver. I do have a few more questions (please remember I am a newbie with absolutely no Java/Apex knowledge). I have been playing around with the code that you provided me. Question 1) How would I add more fields to this existing code? Meaning I would like to populate TaskPriority__c (this custom field is on my custom object DataTable) from Tasks. Here my goal is to get the data into the same record that we previously created. I have it working to the point that it creates a separate entry, but that is not quite what I am looking for. My next question I have is how would I add fields from another object? Meaning I am also trying to copy data from Opportunities in the same record that we are creating in the DataTable. I appreciate all your help. Thanks, Antje