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
Lisa KattawarLisa Kattawar 

Copy Activity records to Custom "Marketing Activity" Object

Can someone help me with a trigger that will do the following (hopefully this is just a matter of sharing code you've already written for a similar situation):

If Activity is created by "Marketing" (I know this based on created by user)
   Copy Activity Record to Custom Marketing_Activity__c object
   Delete Activity Record

Thanks in Advance!!
Lisa


Best Answer chosen by Lisa Kattawar
Vinit_KumarVinit_Kumar
Lisa,

Try below code :-

trigger copyTaskRecord on Task (after insert,after update) {

	List<Task> tList =  new List<Task>();
	List<Task> taskList =  new List<Task>();
	List<Id> userIds = new List<Id>();
	List<Manticore_Marketing_Activity_History__c> newList = new List<Manticore_Marketing_Activity_History__c>();
	
	for(Task t : trigger.new)
	{
		tList.add(t);
		userIds.add(t.CreatedById);
	}

		Map<Id,User> userMap = new Map<Id,User>([select id,UserPermissionsMarketingUser from User where id in:userIds]);
		
for(Task t : trigger.new)
	{
		if(userMap.get(t.CreatedById).UserPermissionsMarketingUser==true)
		{
			Manticore_Marketing_Activity_History__c  ma = new Manticore_Marketing_Activity_History__c (Contact__c = t.whoId,EventDescription__c=t.subject,);//Populate all the fields //required to insert the Manticore_Marketing_Activity_History__c record
			newList.add(ma);
		}
	}
	
	if(newList.size()>0)
	{
		insert newList;
	}
	//delete the task record
	
	for(Task t :tList)
	{
		Task t1 = new Task(id=t.id);
		taskList.add(t1);
   }
    delete taskList;


}

If this helps,please mark it as best answer to help others :)

All Answers

Pavan Kumar KajaPavan Kumar Kaja
Below code will work. But only problem with this is in Task whatid is related to any object(Account, Opportunity or any custom object), whoid is Contact or Lead

While saving these two u can only store these ids in Marketing Activity object.

WhatId__c = t.Whatid
Whoid__c = t.WhoId

Here WhatId__c, Whoid__c are text fields.

Trigger CreateMarketActivity on Task(after insert){
List<Marketing_Activity__c> lstMA = new List<Marketing_Activity__c>();
List<Task> lstTsk = new List<Task>();
for(Task t :Trigger.new){
  // Add all fields from task
  Marketing_Activity__c  ma = new Marketing_Activity__c(Subject__c = t.Subject,
                 Description__c =t.Description,
                 .....
                 );
                
  lstMA.add(ma);
  lstTsk.add(t);
}

If(!lstMA.isEmpty() && !lstTsk.isEmpty()){
  try{
   insert lstMA;
   delete lstTsk;
  }catch(DmlException e){
   System.debug('DML failed due to '+e);
  }
}

}


Lisa KattawarLisa Kattawar
Thanks Ashi

I ended up with the following Trigger (after minor changes for exact field names, etc...).  I'm getting a DML exception on line18 (which is delete lstTsk;).  When I remove this line, it works great...BUT, I still have my Task (since I don't have the line to delete it).

Any ideas for me?  I truly appreciate your help!  I'm not a developer, but I'm trying :)

This is the error I get when I leave in line 18.

User-added image


Trigger CreateMarketingActivity on Task(after insert){
List<Manticore_Marketing_Activity_History__c> lstMA = new List<Manticore_Marketing_Activity_History__c>();
List<Task> lstTsk = new List<Task>();
for(Task t :Trigger.new){
  // Add all fields from task
  Manticore_Marketing_Activity_History__c  ma = new Manticore_Marketing_Activity_History__c
                 (Contact__c = t.WhoId,
                  EventDescription__c = t.Subject
                 );
               
  lstMA.add(ma);
  lstTsk.add(t);
}

If(!lstMA.isEmpty() && !lstTsk.isEmpty()){
  try{
   insert lstMA;
   delete lstTsk;
  }catch(DmlException e){
   System.debug('DML failed due to '+e);
  }
}

}

Lisa KattawarLisa Kattawar
Here is the text from the error (in case pic in last post is too small to read)

Apex script unhandled trigger exception by user/organization: 005G0000002ksEj/00DJ0000003K4h3
Source organization: 00DA0000000HFaB (null)
CreateMarketingActivity: execution of AfterInsert

caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old

Trigger.CreateMarketingActivity: line 18, column 1
Pavan Kumar KajaPavan Kumar Kaja
Hi Lisa,

Try belo code.

Trigger CreateMarketingActivity on Task(after insert){
List<Manticore_Marketing_Activity_History__c> lstMA = new List<Manticore_Marketing_Activity_History__c>();
List<Task> lstTsk = new List<Task>();
for(Task t :Trigger.new){
  // Add all fields from task
  Manticore_Marketing_Activity_History__c  ma = new Manticore_Marketing_Activity_History__c
                 (Contact__c = t.WhoId,
                  EventDescription__c = t.Subject
                 );
               
  lstMA.add(ma);
  lstTsk.add(new Task(id=t.id));
}

If(!lstMA.isEmpty() && !lstTsk.isEmpty()){
  try{
   insert lstMA;
   delete lstTsk;
  }catch(DmlException e){
   System.debug('DML failed due to '+e);
  }
}

}


Vinit_KumarVinit_Kumar
Lisa,

Try below code :-

trigger copyTaskRecord on Task (after insert,after update) {

	List<Task> tList =  new List<Task>();
	List<Task> taskList =  new List<Task>();
	List<Id> userIds = new List<Id>();
	List<Manticore_Marketing_Activity_History__c> newList = new List<Manticore_Marketing_Activity_History__c>();
	
	for(Task t : trigger.new)
	{
		tList.add(t);
		userIds.add(t.CreatedById);
	}

		Map<Id,User> userMap = new Map<Id,User>([select id,UserPermissionsMarketingUser from User where id in:userIds]);
		
for(Task t : trigger.new)
	{
		if(userMap.get(t.CreatedById).UserPermissionsMarketingUser==true)
		{
			Manticore_Marketing_Activity_History__c  ma = new Manticore_Marketing_Activity_History__c (Contact__c = t.whoId,EventDescription__c=t.subject,);//Populate all the fields //required to insert the Manticore_Marketing_Activity_History__c record
			newList.add(ma);
		}
	}
	
	if(newList.size()>0)
	{
		insert newList;
	}
	//delete the task record
	
	for(Task t :tList)
	{
		Task t1 = new Task(id=t.id);
		taskList.add(t1);
   }
    delete taskList;


}

If this helps,please mark it as best answer to help others :)
This was selected as the best answer
Lisa KattawarLisa Kattawar
Ashi and Vinit,

Thank you both!!  I truly appreciate the help.  I'm marking Vinit's answer as "best" because he also addressed my need to check the user profile.  I have this working in our sandbox and I'm ready to move it to production (I only made one little change...I had to move the "delete" logic under the if statement above it because I only want to delete from Activity if I added it to Marketing Activity).

Again, thank you both for your help!