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
Forcedotcom737Forcedotcom737 

Trigger Help: What do I need to do to pick up the user captured in a field (Assigned to) in a custom object (ticket) and then assign the same user as owner of a task?

I am very new to apex coding. Please see trigger below which creates a task when a ticket is raised with priority high. The trigger works except for this line : // t.Owner = Tickets__c.Assigned_To__c;

I am trying to pick up the the user to whom the ticket is assigned via field Assigned_To__c in the custom ticket object and then create a task where the owner of the task is the same user as the 'Assigned to' field captures in the ticket object.

What do I need to do to pick up the user captured in a field (Assigned to) in a custom object (ticket) and then assign the same user as owner of a task? 

Trigger compiles except for the commented line:

trigger AssignTicket on Tickets__c (after insert,after update) {

for(Tickets__c tkt : trigger.new){

if(tkt.Priority__c == 'High'){

task t = new task();

t.Subject = 'Ticket has been assigned to you!';
t.Status = 'Not Started';
t.Priority = 'Normal';
//t.Owner = Tickets__c.Assigned_To__c.;

t.WhatId = tkt.id;

       insert t;
                            }


                                 }
 
Best Answer chosen by Forcedotcom737
Forcedotcom737Forcedotcom737
Karanraj Thx a ton for your response. It works!!
I will take the dev training that you suggest. In the meantime could you please answer a couple of questions to help me understand:

1) Ref: List<Task> listTask = new List<Task>();

Why did you create the list? The line "task t = new task() "within the for loop would create the task for every ticket.

2) t.WhatId = tkt.id; -- Could you explain what is going on in this line? I understand this as taking the id of the ticket and associate it with the id of the task thereby making the link. How would you explain this?

Thanks again!

All Answers

KaranrajKaranraj
trigger AssignTicket on Tickets__c (after insert,after update) {
List<Task> listTask = new List<Task>();
for(Tickets__c tkt : trigger.new){

if(tkt.Priority__c == 'High'){

task t = new task();

t.Subject = 'Ticket has been assigned to you!';
t.Status = 'Not Started';
t.Priority = 'Normal';
t.Owner = tkt.Assigned_To__c.;
t.WhatId = tkt.id;
listTask.add(t);
}
insert listTask;

}
Try the above code, couple of thinfs you have to note down. 
1. In your code you have created new instances for the object Tickets with the vaiable tkt, but you still refering the name of the object while getting the value. That's the problem.
2. You also used DML operation inside the for loop. You should use DML,soql query inside the looping statement to avoid hitting governor limits.

I strongly recommend you to take a look into the Salesforce Trailhead which helps you to get start in the salesforce developement. https://developer.salesforce.com/trailhead
Forcedotcom737Forcedotcom737
Karanraj Thx a ton for your response. It works!!
I will take the dev training that you suggest. In the meantime could you please answer a couple of questions to help me understand:

1) Ref: List<Task> listTask = new List<Task>();

Why did you create the list? The line "task t = new task() "within the for loop would create the task for every ticket.

2) t.WhatId = tkt.id; -- Could you explain what is going on in this line? I understand this as taking the id of the ticket and associate it with the id of the task thereby making the link. How would you explain this?

Thanks again!
This was selected as the best answer
KaranrajKaranraj
1) Ref: List<Task> listTask = new List<Task>();
Why did you create the list? The line "task t = new task() "within the for loop would create the task for every ticket.
Answer :
We have the governor limit in salesforce for number of DML operation called in a execution[150 times]. In your old code,you are doing insert operation inside a for loop, suppose if I insert a 155 records of through dataloader, the DML statement will be called 155 it will reach the DML limit easily and you will get an governor limit error. In my approach, I will store all the task in a list and then I will perform the DML operation limit outside the for loop. Even I'm inserting more than 200 records also my DML statement will be called only one time in a execution. Check this link to understand governor limits in salesforce.  https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

2) t.WhatId = tkt.id; -- Could you explain what is going on in this line? I understand this as taking the id of the ticket and associate it with the id of the task thereby making the link. How would you explain this?
Answer:
In the for loop you have created instances for the object Tickets__c with the name 'tkt', so you have to use the variable tkt to access the field value. In the object oriented programing language, you need to create instances for the object to access the value. 

An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access all of the instance's properties and methods. When you make a new instance the process is called instantiation and is typically done using the new keyword

If this resolves your problem, please select the answer as best, so it will be helpful for others 





 
Forcedotcom737Forcedotcom737
Karanraj thanks for your help!

Regarding this statement :2) t.WhatId = tkt.id; I get the use of tkt as being used as an object of a class. My question is around better understanding how the .id's are being connected. Is the id of the ticket being assigned as the newly created tasks id? Or is the task id being added to the newly created task as a refrence id (e.g foreign key). Is the id of the task now same as the id of the ticket? Just wanted to better understand how this is handled.