+ Start a Discussion
meds9meds9 

Report on Task Object

Hiii,
Can any one please help me out in generating report on task object, I am able to display only single object records but i need to display as shown below:.. 
User-added image
Thanks in Advance.
Best Answer chosen by meds9
kri 10kri 10
Hi,

Please try this below code.

trigger ExampleTrigTask on Task (before insert) { 
   String sObjName;
   for(Task t:trigger.new){
       if(t.WhatId<> null){
           sObjName = (t.whatId).getSObjectType().getDescribe().getName();
       }
       if(t.WhoID<> null){
           sObjName = (t.WhoID).getSObjectType().getDescribe().getName();
       }
       t.ObjectName__c = sObjName;    
   }
}


 

All Answers

~Onkar~Onkar

You can do it.  Here is the step.

1. Create a custom field on Activity object "Object__C"
2. Create a process from process builder 
3. Execute apex class from process builder and update Object__c with "What.type" or Who.Type (Accessible in Apex class)

User-added image

Sample code for 

public class updateTaskObject {
  @InvocableMethod(label='Get Task Id' description='Get Task Id')
  
  public static void getTaskType(List<ID> ids) {
   
    List<Task> tasks = [SELECT Id,What.Type FROM Task WHERE Id in :ids];
    for (Task tsk : tasks) {
      tsk.Object__c  =  tsk.what.Type; 
    }
    update tasks;
    
  }
}

Hope it will solve your problem.

~Thanks,
Onkar Kumar
meds9meds9
Hiii Onkar,

Thanks for your reply.
Can u please describe clearly how to build a process builder and how to create report.
~Onkar~Onkar
Steps to find process builder

1. Goto Setup
2. Find Process builder from Quick Find.
3. Click on Process Builder
4. Create new process builder

Before Process builder must save above code.

Creating Summary Report.(group by Object name)

1. Click on Report Tab.
2. Select Activity Report type.
3. Select Object and Record Id as Column name for report.

~Thanks,
Onkar Kumar
 
meds9meds9
Hi ,
That Report works even with the below trigger:

Trigger::

trigger Example on Task (before insert) {
      list<string> whatIds=new list<string>();
    list<string> whoIds=new list<string>();
    for(Task t:trigger.new)
    {
        if(t.WhoId<>null)
           whoIds.add(t.WhoId); 
        if(t.WhatId<>null) 
        whatIds.add(t.WhatId);
    }
    system.debug('whoids'+whoIds);
    system.debug('whatids'+whatids);
    if(whoIds.size()<>null){
        system.debug('whoids size'+whoIds.size());
        for(string s:whoIds)
        {
            string s1=s.substring(0, 3);
            system.debug('string '+s1);
            for(Task t1:trigger.new){
                system.debug('hello');
                if(s1=='003'){
                    system.debug('account');
                    t1.ObjectName__c='contact';
                }
                else if(s1=='00Q')
                {
                    t1.ObjectName__c='Lead';
                }
                
            }
        }
    }
    if(whatIds.size()<>null)
    {
        for(string wids:whatIds)
        {
         string st1=wids.substring(0, 3);
            for(Task taskVar:trigger.new)
            {
                if(st1=='001')
                {
                  taskVar.ObjectName__c='Account';
                }
                else if(st1=='006')
                {
                    taskVar.ObjectName__c='Opportunity';
                }
            }
        }
    }
    

}
 
kri 10kri 10
Hi,

Please try this below code.

trigger ExampleTrigTask on Task (before insert) { 
   String sObjName;
   for(Task t:trigger.new){
       if(t.WhatId<> null){
           sObjName = (t.whatId).getSObjectType().getDescribe().getName();
       }
       if(t.WhoID<> null){
           sObjName = (t.WhoID).getSObjectType().getDescribe().getName();
       }
       t.ObjectName__c = sObjName;    
   }
}


 
This was selected as the best answer