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
Russell baker 1Russell baker 1 

Trigger to Pull related contact designation on Task page when tesk create.

I have a requirement. Contact related to Task, I need to pull his functional role ( custom field) on Activity page layout. So whenever I create or edit an activity and relate with ant contact so his/her functional role should populate automatic.

Please help! How can I achieve it? I tried formula field but unable to get. I tried through trigger but it is not working nighter on new task nor while I update the task.
Please find below code:
 
trigger updatefunctionalrole on Task (before insert, before update) {

Map<ID,String> confunrole = new Map<ID,String>();
List<Task> conTasks = new List<Task>();


for (Task e : trigger.new) {

    if (e.whoID!= null && (String.valueOf(e.whoID)).startsWith('003'))  {

        if (trigger.isInsert || (trigger.isUpdate && e.WhoID != trigger.oldMap.get(e.id).WhoID)) {
            confunrole.put(e.whoID,'');
            conTasks.add(e);
        }
    }
}

for (contact con : [SELECT Functional_Role__c FROM contact WHERE ID IN :confunrole.keySet()]) {
    confunrole.put(con.id,con.Functional_Role__c);
}
// Update the contact functional role field on the Task with the relevant value
for (Task e : trigger.new) {
    e.functional_role__c = confunrole.get(e.whoID);
}
}
Kindly let me me know where I am doing wrong.
 
Pramod GowdaPramod Gowda
Hi,
I have made some changes to your code and its updating "Functional Role" field on Task from Contact.
trigger updatefunctionalrole on Task (before insert, before update) {

Set<Id> contactIds = new Set<Id>();
Map<Id,String> confunrole = new Map<Id,String>();

for (Task e : trigger.new) {

    if (e.whoID!= null && (String.valueOf(e.whoID)).startsWith('003'))  {

        if (trigger.isInsert || (trigger.isUpdate && e.WhoID != trigger.oldMap.get(e.id).WhoID)) {            
            contactIds.add(e.whoID);            
        }
    }
}
system.debug('confunrole***'+contactIds);
for(List<Contact> conList : [SELECT Functional_Role__c FROM contact WHERE ID IN :contactIds]){
    for (contact con : conList) {
        confunrole.put(con.id,con.Functional_Role__c);
    }
}

// Update the contact functional role field on the Task with the relevant value
for (Task e : trigger.new) {
    e.functional_role__c = confunrole.get(e.whoID);
}
}

 
Russell baker 1Russell baker 1
Hi,

Thank you for your reply. I changed my code with your coe and still it is not working. I have checked debug log and found this:
Please find below trigger code.
trigger updatefunctionalrole1 on Task (before insert, before update) {

Set<Id> contactIds = new Set<Id>();
Map<Id,String> confunrole = new Map<Id,String>();

for (Task e : trigger.new) {

    if (e.whoID!= null && (String.valueOf(e.whoID)).startsWith('003'))  {

        if (trigger.isInsert || (trigger.isUpdate && e.WhoID != trigger.oldMap.get(e.id).WhoID)) {            
            contactIds.add(e.whoID);            
        }
    }
}
system.debug('confunrole***'+contactIds);
for(List<Contact> conList : [SELECT Functional_Role__c FROM contact WHERE ID IN :contactIds]){
    for (contact con : conList) {
        confunrole.put(con.id,con.Functional_Role__c);
    }
}

// Update the contact functional role field on the Task with the relevant value
for (Task e : trigger.new) {
    e.functional_role__c = confunrole.get(e.whoID);
}
}

and debud log after do some sort of activity to envoke trigger:
 
09:23:07.61 (61420999)|CODE_UNIT_FINISHED|updatefunctionalrole1 on Task trigger event BeforeInsert for [new]
09:23:07.223 (223210853)|ENTERING_MANAGED_PKG|rh2
09:23:07.228 (228672694)|CUMULATIVE_LIMIT_USAGE
09:23:07.228 (228672694)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100
  Number of query rows: 7 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

09:23:07.228 (228672694)|LIMIT_USAGE_FOR_NS|APXTConga4|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

09:23:07.228 (228672694)|CUMULATIVE_LIMIT_USAGE_END

I am not able to understand why code is not querying data.
 
Pramod GowdaPramod Gowda
Is this the Full logs?
Because in trigger, there is a Debug statement which is not reflecting in your logs.
I suggest you, put more debug statements to the code like @ the entry of trigger, inside for loops so you can find out trigger is working or not.
 
Russell baker 1Russell baker 1
Hi Pramod,

I have checked trigger and it is working with trigger.isUpdate but it is not working with trigger.isInsert.
Trigger works fine when I update any task but when I create new task it is not working.\

I thing some other triggers are blocking it because i tried the same code in my dev org and it is working finr with both Insert and Update.
How can i arrange trigger excution order?
Please help!
Pramod GowdaPramod Gowda
Hi Russell,
If you are using multiple triggers on Task objects, then its not a good practice.
Use any trigger framework to find excution order, avoid recursion.
Check any of the managed package have trigger on task object and go through what that trigger is doing. You cannot see the code but u can check the functionality by crating the task manually.

Pramod