You need to sign in to do that
Don't have an account?
Trigger to create child record not working
I have the following trigger on the Housing_Request__c object which should create a Setup_List__c child record if the Approved_Housing_Option__c field on the housing request is not null.
When I edit and save a housing request I do not get any errors, however, my child record is not created. Can anyone see if I have missed something?
}
trigger createSetup on Housing_Request__c (after insert, after update) {
List <Setup_List__c> suToInsert = new List <Setup_List__c>();
for (Housing_Request__c hr : Trigger.new) {
if (hr.Approved_housing_option__c != null){
Setup_List__c sl = new Setup_List__c ();
sl.RecordTypeId = '012K00000000BAD';
sl.Housing_Request__c = hr.id;
sl.Vendor__c = hr.approved_housing_option__r.vendor__c;
suToInsert.add(sl);
}
}
try {
insert suToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}
Emily,
You are not gertting the Id of Vendor due to fact that, you cant access lookup fields Id from Trigger.new list.
A solution for that is to query records in list and use that list in query, Something like this.
Check if the query is proper.
Hoipe it helps.
All Answers
Please check removing try catch block.
Regards,
Pankaj Raijade.
Thanks, I removed the try catch block and am still seeing the same thing. Record saves with no errors, but child record is not created.
On first pass it seems fine, can you debug hr..Approved_housing_option__c to ensure that it is NOT null.
Also, are you wanting to create a new setup list every time the Housing Request is updated? The trigger as written will create a new setup list when anything on the housing request is updated provided the Approved_housing_option__c is not null.
Thanks, I will check on that.
Also, I did add the following after initial post to make sure a new record is only created if an approved housing option has just been entered:
}
trigger createSetup on Housing_Request__c (after insert, after update) {
List <Setup_List__c> suToInsert = new List <Setup_List__c>();
for (Housing_Request__c hr : Trigger.new) {
if ( ((Trigger.isInsert) ||
(Trigger.isUpdate) && (Trigger.oldMap.get(hr.id).Approved_housing_option__c == null)) &&
(hr.approved_housing_option__c != null) )
{
Setup_List__c sl = new Setup_List__c ();
sl.RecordTypeId = '012K00000000BAD';
sl.Housing_Request__c = hr.id;
sl.Vendor__c = hr.approved_housing_option__r.vendor__c;
suToInsert.add(sl);
}
}
try {
insert suToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}
okay, verified hr.approved_housing_option__c is definitely not null...
Weird... now it's saying I have a Required_Field_Missing [Vendor]
try this now it will show custom error message whenever there is an error:
Hope it helps.
do you have any trigger on Setup_List__c object?
Regards,
Pankaj Raijade,
Yes, there is one before insert, before update trigger on the Setup_List__c object to set the notification date:
trigger noticeUpdate on Setup_List__c (before insert, before update) {
for (Setup_List__c sl : Trigger.new)
//check if notice at First of Month is True
if (sl.Notice_due_on_FOM__c == True){
//change notice date to first of the month
date myDate = sl.Notice_Date__c;
date monthStart = myDate.toStartofMonth();
}
}
Thanks, let me try this!
Rahul,
The custom error message is coming back with the same detail: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Vendor]: [Vendor]
The vendor__c is a master-detail relationship on the Setup_List__c object, would that cause this? Is there another way I need to set that value?
Thanks for your help!
Emily
Emily,
You are not gertting the Id of Vendor due to fact that, you cant access lookup fields Id from Trigger.new list.
A solution for that is to query records in list and use that list in query, Something like this.
Check if the query is proper.
Hoipe it helps.
Rahul,
You are my hero! That is exactly what I needed... I kept thinking I needed to pull back that vendor ID somewhere but couldn't figure out where to put the select statement. That was perfect!! Thank you so very much!
I added the logic back in to determine that the approved housing option was just entered so the entire trigger looks like this now (in case anyone needs this for future reference):
trigger createSetup on Housing_Request__c (after insert, after update)
{
List <Setup_List__c> suToInsert = new List <Setup_List__c>();
for (Housing_Request__c hr : [Select Id, approved_housing_option__r.vendor__c from Housing_Request__c where Id in: Trigger.New])
{
if ( ((Trigger.isInsert) ||
(Trigger.isUpdate) && (Trigger.oldMap.get(hr.id).Approved_housing_option__c == null)) &&
(hr.approved_housing_option__c != null) )
{
Setup_List__c sl = new Setup_List__c ();
sl.RecordTypeId = '012K00000000BAD';
sl.Housing_Request__c = hr.id;
sl.Vendor__c = hr.approved_housing_option__r.vendor__c;
suToInsert.add(sl);
}
}
try {
insert suToInsert;
}
catch (system.Dmlexception e) {
Trigger.new[0].addError(e);
}
}
Good that its resolved,
One more suggestion, never use hardcorded Id's in your code.
While deployment it might not be the case that ids in both the orgs are same so query it and use that Id in your code.
:)
Step 2: Modify the Publisher Actions Layout. In Setup, select Build > Create > Objects and click the Expense Report link. ...
Step 3: Test the Application.
Regards,
Will