+ Start a Discussion
anvesh@force.comanvesh@force.com 

Please correct this simple trigger?

I have a field OpenedTaskDate__c on Task  this is date field.

i should need to display this date on contact field (TaskTestDate__c). so i have writtened a trigger like this

trigger updateTrig on Contact (before insert,after insert , before update) {
Task T =new Task();
for(contact c:Trigger.new)
c.TaskTestDate__c= t.OpenedTaskDate__c;


but it was not displaying the task field value on contact field.

Chris760Chris760

Is this your first trigger?  Just wondering, as there appears to be quite a few mistakes in your code.

 

The trigger would have to be initated by the Task, since you want a value on the task to copy to the contact whenever someone updates/inserts a task record, correct?  You're also mixing before and after inserts and I'm not sure why.  You use before update/inserts when you're updating the same object that you're triggering from (with very few exceptions) so that you don't cause recursive loops (you update a record which updates itself, which updates itself, which updates itself on and on to infinity).  And you use after update/insert when you're updating a different object from the object you're triggering from (where your record needs to be in the database before you can run queries from other objects that reference your record Id).

 

You also seem to be missing your DML command (which is always needed in after insert/update triggers and almost any time you perform updates between objects).

 

Anyway, your trigger would need to look something more like:

 

trigger updateContactFromTask on Task (after insert, after update) {

 

  map<Id,Date> contactDateMap = new map<Id,Date>();

  list<Contact> contacList = new list<Contact>();

 

  for(Task t : Trigger.new)

    contactDateMap.put(t.WhoId,t.OpenedTaskDate__c);

  }

 

  for(Contact c : [select Id, TaskTestDate__c from Contact where Id IN: contactDateMap.keyset()]){

    c.TaskTestDate__c = contactDateMap.get(c.Id);

    contactList.add(c);

  }

 

  update contactList;

 

}

 

By the way, you're also posting in the wrong section.  Typically people wanting apex help post in the Apex Development section. ;)  Also, don't forget to refer to the Apex Code Developers Guide whenever you're stuck.  It's structured like Google, just click "Search" and type your question in and it will usually give you info about what you want.

anvesh@force.comanvesh@force.com

Thanks for your effort but misunderstanding let me clarify you

 

 

 

 

I would like to create a new Contact field on contact.  this field will pull the date of the most recent activity record that fits the following criteria.

TaskTestDate  :        Most recent task record date of specified record type (i have 3 record types here so assume first record type id as ='00XXXXXXXXXX' ).

EventTestDate :      Most recent  event record date of specified record type (i have 3 record type ).

sorry  for any confusion. and really thanks for your effort.

Actually it is my imagination , when we open any task, on task field it displayes with current datetime (A i mentioned in previously question) , so  when i assign this field to contact field it displayes the task opened datetime.

Chris760Chris760

Hi Anvesh,

 

The problem with putting a trigger on the contact, is that it will never be able to update the contact unless someone happens to update the contact record and hit "save" for some reason.  A "trigger" is a piece of code that ONLY runs when someone updates, inserts, deletes, or undeletes a record.   Triggers aren't like formulas, where they display values in real time simply by viewing a record.  So you have to always ask yourself, "where is the value that I want to put on this record, going to get pulled from?"  Because that's the object where your trigger will have to be written.

 

So in your case, you'd have to write two seperate triggers: one for the Task object, and one for the Event object.

 

Does that make a little more sense?

 

Side Note: If you want many-to-one database values to be displayed in real time on a parent object (like a formula field would behave), you'd need to create a Visualforce page.  Visualforce is capable of running APEX Classes as a result of someone simply viewing the Visualforce page, without the user needing to perform a DML command (insert, update, delete, undelete -- which is what a trigger would require).

anvesh@force.comanvesh@force.com

please give me sollution for this

Chris760Chris760

Solution for a visualforce page?  I've never written a visualforce page (it's like writing a HTML page that you use to override your normal salesforce page, except you'd code the visualforce page yourself instead of using the normal salesforce page layout when you click on a record).  You'd have to go to the Visualforce Development section to get help making one and reference the Visualforce Developers Guide.

Prady01Prady01

Hi there yes this can be done using a trigger and the trigger need to be on Task and not on contact.

 

Ex:

 

 trigger updateTrig on Task(before insert,after insert , before update) {

       set<Id> tkid's = new set<Id>();

     For(Task tk: trigger.new){

        tkid's(tk.id);

     }

// Call a the class and method pass the list so that the class can make all the heavy lifting and update your contact record.

   ClassName.MethodName(tkid's);

}

 

Hint: when writting the class plz use taskrelation object to get the contact. Check out the Salesforce DOM if you not sure.

 

Hope this help!

Prady

Arunkumar.RArunkumar.R

Hi Anvesh,

 

Plase use the below code, surely you well get your desired solution.

 

trigger ctcupdate on Task (after insert,after update) {
set<id> s1=new set<id>();

List<Task> l1 = new List<Task>();

for(task t1:trigger.new)
{
s1.add(t1.whoid);
l1.add(t1);
}
List<contact> c1 = new List<contact>([select Name,TaskTestDate__c from contact where id in: s1]);
for(task t2:l1)
{
for(Contact c3:c1)
{
c3.TaskTestDate__c=t2.OpenedTaskDate__c;
}
}
update c1;
}

 

 

Plase mark if you found this as a best solution and Don't forget to give KUDOS.

 

Thanks and Regards,

Arunkumar.R | Salesforce Certified Developer