0

I have a field on Task Test_Time__c (Date/Time). How can I fetch this field and populate on Last_Login_Time__c on Related Contact record?

Below is task on Contact

Task on Contact

Below is the related Contact

enter image description here

Tried to add this trigger on Task

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

Map<Id, List<Task>> taskWhatIds = new Map<Id, List<Task>>{};

    for (Task t : trigger.new) {
        if (t.WhatId != null) {
        List<Task> tasks = taskWhatIds.get(t.WhatId);
        if (tasks == null) {
            tasks = new List<Task>();
            taskWhatIds.put(t.WhatId, tasks);
        }
        tasks.add(t); 
    }
}

for (Contact c: [Select Id, Last_login_time__c FROM Contact WHERE Id IN :taskWhatIds.keySet()]){
    for (task t : taskWhatIds.get(c.id)){
        t.test_time__c = c.last_login_time__c;
    }
}
}
  • 1
    Deleting your old question and creating a nearly identical one is borderline abusive behavior, especially when both are already near duplicates of a question you asked yesterday. In the future, just edit. Regardless of all that, have you made any attempt to write this trigger yourself? Are you stuck somewhere specifically? – Adrian Larson Mar 15 '18 at 23:55
  • Sorry! Got it! can I continue with this post or shall I undelete the previous post and delete this one? – Lin Mar 15 '18 at 23:57
  • At this point you will have a deleted post either way. Just stick with what you've got and edit with any clarifications. – Adrian Larson Mar 15 '18 at 23:58
  • Yes I tried a trigger on Task, I had added it to the question. No errors on Trigger, but it wouldn't work either – Lin Mar 16 '18 at 0:04
0

Your trigger doesn't do anything to the Contact at all.

for (Contact c: [Select Id, Last_login_time__c FROM Contact WHERE Id IN :taskWhatIds.keySet()]){
    for (task t : taskWhatIds.get(c.id)){
        t.test_time__c = c.last_login_time__c;
    }
}

You need to make two specific changes here:

  1. You're assigning to a field on t, the Task, rather than c, the Contact. You need to fix this by modifying the Contact.
  2. You also do not perform DML. From a Task trigger, you must fire an update DML to persist your changes to a Contact. You need to build a List of contacts to update and do update contactList at the end of your trigger.

I'm pretty sure you don't need this running after insert, after update, before insert, before update. after insert should be enough.

|improve this answer|||||
  • Where should I modify my code? I changed c.Last_Login_Time__c = t.Test_Time__c; ... also just added after Insert. – Lin Mar 16 '18 at 0:48
  • 1
    I'm happy to expand on my answer if it's not clear, but I'm not able to write the code for you. – David Reed Mar 16 '18 at 1:00
  • Anything that helps @David, I have been trying to resolve this from three days in multiple ways. I tried Process Builder, salesforce.stackexchange.com/questions/211193/…, none of them works, let me know if you have a better solution, thanks much. – Lin Mar 16 '18 at 1:23
  • What aspects are you not clear on? I would be happy to provide links to resources that may help. This is not complex Apex, but I understand that as a new developer the scope can be overwhelming. – David Reed Mar 16 '18 at 2:26
  • 1
    I have tried to clarify my answer. I would also strongly recommend completing the Trailhead modules on Apex and triggers, and reading through the Apex Developer Guide. Doing so will build the conceptual vocabulary that will help you ask good questions and give you the ability to effectively integrate suggestions into your code. – David Reed Mar 16 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.