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
transilalexistransilalexis 

Apex Trigger needed to assign task to owner of parent object

Definitely not new to the cloud here but just dipping my toe into the pool of Apex. 

 

I understand my limitations with workflow and realize that I cannot assign a task "across objects".  I have a custom object "Order" that gets integrated into Salesforce as a read only object from our legacy order system.  I would like to assign a task to the owner of the account to which the order is attached.  This task will be due 14 days after the Created Date of the Order.

 

Workflow can remedy this IF I was to assign the task to the owner of the Order record.  This will not work because the "owner" of the Order object is the "integration admin" or license used in the integration from our legacy system.  The Account is referenced in the Order record. 

 

Sorry for the remedial request, but if anyone on this board can get me started, I would appreciate the help.  Thanks!

 

* I neglected to include that these tasks should only be assigned when the Order is a Sample (Order_Type__c = "Sample").

Jerun JoseJerun Jose

You will need a trigger to get you through this.

 

I'm guessing that you shouldnt have updates on your order object so a trigger which works on insert should suffice.

 

Use the below code as a starting point and add detail as required. Let me know if you have any problems.

 

trigger someName on Order__c (after insert){
	list<Task> taskList = new list<Task>();
	list<Order__c> orderList = [select id, f1__c, f2__c, Account__r.OwnerID from Order__c where ID in :trigger.new];
	for(Order__c ord : orderList){
		Task tsk = new Task();
		tsk.OwnerID = ord.Account__r.OwnerID;
		tsk.Subject = 'something'+ord.f1__c;
		tsk.Body = f2__c;
		tsk.DueDate = system.today().addDays(14);
		taskList.add(tsk);
	}
	insert taskList;
}

 

transilalexistransilalexis

Thank you for your response.  What should I replace f1__c, f2__c with?

Jerun JoseJerun Jose

f1__c and f2__c are some dummy field API names. replace them with the API names of actual fields u need to use for the task assignment. You can then use these field values for assigning task attributes.