You need to sign in to do that
Don't have an account?

APEX Trigger from Case to Child Custom Object
I am attempting to create an APEX Trigger on Case to insert a record to a custom object on the case related list called Accessory_Item__c and a list of items stored in Accessory__c. When we are placing an order we click an add new button on the Accessory_Item__c related list and it takes us to a look upfield in which we look up through the Accessory__c object where an item is stored called 197 - 360μm FiberFlex™ Fiber. I have a picklist field on the Case object called Quick_Order__c. When this Quick_Order__c = 360 Fiber I want the trigger to select the 197 - 360μm FiberFlex™ Fiber from Accessory__c, place it into Accessory_Item__c and add it to the case.
Below is an example of what should show up on the related list object on the case.

Below is the code that I currently have but I am getting several error messages from it. I do not think I am referencing the correct objects in the correct places. I have done as much research as I can to understand apex and I thought I was close but the error messages tell me otherwise.
Below is an example of what should show up on the related list object on the case.
Below is the code that I currently have but I am getting several error messages from it. I do not think I am referencing the correct objects in the correct places. I have done as much research as I can to understand apex and I thought I was close but the error messages tell me otherwise.
trigger UpdateFieldValues on Case (after insert) { List<Accessory__c> Accessories = new List<Accessory_Item__c>(); //This trigger give me an error on line 11 column 39 this is because when I am adding a new purchase request for some reason it kicks me from the sandbox into production for (Case c : Trigger.New) { if (c.Quick_Order__c == '360 Fiber') { Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = '197 - 360μm FiberFlex™ Fiber')); } } insert Accessories; }Any help is appreciated!
Sure. Your trigger would look something like below:
Note, I still think the custom setting approach is the way to go (as you don't have to requery every 200 records through a trigger), but this should get you going.
All Answers
You *could* hard code this. However, this is a bad practice and you should stay away from it. Instead, I would recommend you create a custom setting (Setup->Develop->Custom Settings), and create a custom setting for the 360 fiberflex fiber record.
Alternatively, you could query for the Accessory__c record by the name (197 - 360um FiberFlex Fiber in this case), and fetch the Id of the record that way as well.
Thank you again this gives another path to follow.
Sure. Your trigger would look something like below:
Note, I still think the custom setting approach is the way to go (as you don't have to requery every 200 records through a trigger), but this should get you going.
I am not framiliar with the custom setting approach. I will have to look into it and possibly impliment it in the future.
This is the first trigger that Ive ever built and Im having trouble using a test class to cover at least 75% of the code.
Can you help on this portion too? Below is what I have so far. But Im getting an error message when running the test.