You need to sign in to do that
Don't have an account?
Eric Anderson 54
Access Id from object creating in trigger
Hi there,
I have a need to create a trigger that will create a row for a 'Time_Entry' object based on the addition of a row to a 'Requst' object. The 'Request' object has 'Time entries' assigned to it and it is perceived that there is time associated with creating a new request, so the requirement is that the system create a 'Time Entry' for a 'Request' when the new request is opened. I've tried to create a trigger for this based on what i learned from the Trailhead training, but I'm missing something.
I've tried 3 or 4 approaches, and I keep thinking that I'm over thinking it!
Any help would be greatly appreciated.
Thanks!
Eric Anderson
I have a need to create a trigger that will create a row for a 'Time_Entry' object based on the addition of a row to a 'Requst' object. The 'Request' object has 'Time entries' assigned to it and it is perceived that there is time associated with creating a new request, so the requirement is that the system create a 'Time Entry' for a 'Request' when the new request is opened. I've tried to create a trigger for this based on what i learned from the Trailhead training, but I'm missing something.
trigger New_Req_Time_Entry on Request__c (after insert) { //Get the ID number for the request just created. string RequestID = Request__c.Id; //Build the default values to the new time entry record. Time_Entry__c entry = new Time_Entry__c(Activity__c='<Select Activity>', Date_Worked__c=System.today(), Hours_Worked__c=' 0', Minutes_Worked__c='15', Related_Object__c=RequestID); //Insert the new default time entry row into the Salesforce database. Insert entry; }
I've tried 3 or 4 approaches, and I keep thinking that I'm over thinking it!
Any help would be greatly appreciated.
Thanks!
Eric Anderson
Use the following code,
trigger New_Req_Time_Entry on Request__c (after insert) {
list<Time_Entry__c> entrylistToInsert = new list<Time_Entry__c>();
for(Request__c req : trigger.new){
string RequestID = req.Id;
Time_Entry__c entry = new Time_Entry__c(Activity__c='<Select Activity>', Date_Worked__c=System.today(), Hours_Worked__c=' 0', Minutes_Worked__c='15', Related_Object__c=RequestID);
entrylistToInsert.add(entry);
}
Insert entrylistToInsert;
}
- Hope this helps
All Answers
Use the following code,
trigger New_Req_Time_Entry on Request__c (after insert) {
list<Time_Entry__c> entrylistToInsert = new list<Time_Entry__c>();
for(Request__c req : trigger.new){
string RequestID = req.Id;
Time_Entry__c entry = new Time_Entry__c(Activity__c='<Select Activity>', Date_Worked__c=System.today(), Hours_Worked__c=' 0', Minutes_Worked__c='15', Related_Object__c=RequestID);
entrylistToInsert.add(entry);
}
Insert entrylistToInsert;
}
- Hope this helps
Thank you for the help. That got it working for me.
Have a great day!
- Eric Anderson -