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
affuaffu 

URGENT:not updating contact fields in task

I have created a custom field in Task(Activity) and i want to update that field with Phone field from contacts.

 

Main reason for writting a trigger is i want to show it in a Task list view(visualforce page).

 

Here is the Trigger:

 

 

trigger CopyPhone on Task (after update) {

   Set<Id> contactIds = new Set<Id>();
    for(Task t : trigger.new){
        String wId = t.WhoId;
        if(wId!=null && wId.startsWith('003') && !contactIds.contains(t.WhoId)){
            contactIds.add(t.WhoId);
        }
    }
    List<Contact> taskcons = [Select Id,Phone from Contact where Id in :contactIds];
    Map<Id, Contact> conMap = new Map<Id, Contact>();
    for(Contact c : taskcons){
        conMap.put(c.Id,c);
    }
    for(Task t : trigger.new){
        String wId = t.WhoId;
        if(wId!=null && wId.startswith('003')){
            Contact thisCon = conMap.get(t.WhoId);
            if(thisCon!=null){
                t.ThirdPartyNo__c = thisCon.Phone;
            } 
        }
    }
}

 Thanks in Advance.........

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Needs to be "before update" / "before insert". As a side note, you can make this more efficient:

 

trigger copyPhone on Task (before insert, before update) {
  set<id> whoids = new set<id>();
  for(task t:trigger.new) {
    whoids.add(t.whoid);
  }
  map<id,contact> contacts = new map<id, contact>([select id,phone from contact where id in :whoids]);
  for(task t:trigger.new) {
    if(contacts.get(t.whoid) != null) {
      t.thirdpartyno__c = contacts.get(t.whoid).phone;
    }
  }
}

Some notes here:

 

The query optimizer will silently ignore ID values that are not contact IDs (at least, it has in the past, I don't see why that would have changed).

 

You can put a query result directly into a map without looping through the list.

All Answers

sfdcfoxsfdcfox

Needs to be "before update" / "before insert". As a side note, you can make this more efficient:

 

trigger copyPhone on Task (before insert, before update) {
  set<id> whoids = new set<id>();
  for(task t:trigger.new) {
    whoids.add(t.whoid);
  }
  map<id,contact> contacts = new map<id, contact>([select id,phone from contact where id in :whoids]);
  for(task t:trigger.new) {
    if(contacts.get(t.whoid) != null) {
      t.thirdpartyno__c = contacts.get(t.whoid).phone;
    }
  }
}

Some notes here:

 

The query optimizer will silently ignore ID values that are not contact IDs (at least, it has in the past, I don't see why that would have changed).

 

You can put a query result directly into a map without looping through the list.

This was selected as the best answer
affuaffu

Thank u very much for replying,

I have tried ur code but it is not updating the task field.

 

 

 

affuaffu

Sorry I got it Thanku very much .My testing was wrong........

 

Thank u very much..........