+ Start a Discussion
RajnisfRajnisf 

How can i get Createdby Role of task on contact

i want to get the "Role" on contact who created the task/Event.


how can i get that thru trigger/formula?

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

This is not possible to do using formula field. You can do this using triger.

If your requirement is to have the role of the user who created the latest task? (as there will be many tasks under one contact). You can to this by writtimng trigger on task as below,

 

Create field on task : RoleOFOWner__c this will be formula field(CreatedBy.UserRole.Name)

craeet field on contact RoleOFtaskUSer__c text field to store the role name.

 

trigger UpdateRoleOnContact on Task (after Insert) {
Set<Id> IdSet = New Set<Id>();
Set<Id> IdConSet = New Set<Id>();
List<Contact> ListCon = NEw List<Contact>();
MAp<id,string> mapRoleConID = New MAp<Id,String>();
  
    for(task t:trigger.new){
      IdSet.Add(t.id);
    }

    for(Task tsk:[select whoid from task where id in:IdSet] ){
      IdConSet.add(tsk.whoid); 
    }

    for(contact con:[select id,RoleOFtaskUSer__c,(select id,lastmodifieddate,RoleOFOWner__c from Tasks ORDER BY lastmodifieddate ASC ) from contact where id in:IdConSet]){
         mapRoleConID.put(con.id,con.tasks[0].RoleOFOWner__c);
    }
   
    for(Contact c:[select id ,name,RoleOFtaskUSer__c from contact where id In:mapRoleConID.keyset()]){
       c.RoleOFtaskUSer__c = mapRoleConID.get(c.id);
       ListCon.add(c);
    }

update ListCon ;
}

 

 Hit Kudos and mark this as answer if it helps you.

 

All Answers

Yoganand GadekarYoganand Gadekar

This is not possible to do using formula field. You can do this using triger.

If your requirement is to have the role of the user who created the latest task? (as there will be many tasks under one contact). You can to this by writtimng trigger on task as below,

 

Create field on task : RoleOFOWner__c this will be formula field(CreatedBy.UserRole.Name)

craeet field on contact RoleOFtaskUSer__c text field to store the role name.

 

trigger UpdateRoleOnContact on Task (after Insert) {
Set<Id> IdSet = New Set<Id>();
Set<Id> IdConSet = New Set<Id>();
List<Contact> ListCon = NEw List<Contact>();
MAp<id,string> mapRoleConID = New MAp<Id,String>();
  
    for(task t:trigger.new){
      IdSet.Add(t.id);
    }

    for(Task tsk:[select whoid from task where id in:IdSet] ){
      IdConSet.add(tsk.whoid); 
    }

    for(contact con:[select id,RoleOFtaskUSer__c,(select id,lastmodifieddate,RoleOFOWner__c from Tasks ORDER BY lastmodifieddate ASC ) from contact where id in:IdConSet]){
         mapRoleConID.put(con.id,con.tasks[0].RoleOFOWner__c);
    }
   
    for(Contact c:[select id ,name,RoleOFtaskUSer__c from contact where id In:mapRoleConID.keyset()]){
       c.RoleOFtaskUSer__c = mapRoleConID.get(c.id);
       ListCon.add(c);
    }

update ListCon ;
}

 

 Hit Kudos and mark this as answer if it helps you.

 

This was selected as the best answer
RajnisfRajnisf

Thanks a lot for replying..

 

error is at this line..

 

  if(mapRoleConID.size() > 0)

 mapRoleConID.put(con.id,con.tasks[0].RoleOFOWner__c);

 

System.ListException: List index out of bounds

 

 

 

Team WorksTeam Works
trigger UpdateRoleOnContact on Task (after Insert) {
   
   Set<Id> IdSet = New Set<Id>();
   Set<Id> IdConSet = New Set<Id>();
   
   List<Contact> ListCon = NEw List<Contact>();
   MAP<id,Task> mapRoleConID = New MAP<Id,task>();
   
   for(task t:trigger.new){
      if((t.whoid.getsobjecttype()==contact.sobjecttype) && trigger.new != null)
      IdSet.add(t.id);
    }

    if(!idSet.isEmpty()){
    for(Task tsk:[select whoid from task where id IN :IdSet] ){
      IdConSet.add(tsk.whoid);  
    }}

    for(contact con:[select id,RoleOfTaskUser__c,(select id,lastmodifieddate,RoleOfOwner__c from Tasks ORDER BY lastModifiedDate ASC ) from contact where id in:IdConSet]){
         mapRoleConID.put(con.id,con.tasks[0]);
    }
    
    if(!mapRoleConID.isEmpty()){
    for(Contact c:[select id ,name,RoleOfTaskUser__c from contact where id In:mapRoleConID.keyset()]){
       c.RoleOfTaskUser__c = mapRoleConID.get(c.id).RoleOfOwner__c;
       ListCon.add(c);
    }}

update ListCon ;
}