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
David DurantDavid Durant 

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.
User-added image

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!

 
Best Answer chosen by David Durant
James LoghryJames Loghry

Sure.  Your trigger would look something like below:

trigger UpdateFieldValues on Case (after insert) {
    
    Id accessory360Id = [Select Id From Accessory__c Where Name='197 - 360μm FiberFlex™ Fiber' Limit 1].Id;

    List<Accessory_Item__c> accessories = new List<Accessory_Item__c>();
    for (Case c : Trigger.New) {
        if (c.Quick_Order__c == '360 Fiber') {
            accessories.add(new Accessory_Item__c(
                            Case__c = c.Id,
                            Accessory__c = accessory360Id));
        }
    }
    insert accessories;
}

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

David DurantDavid Durant
This is the code that I am currently using. It is not giving me any errors but It is not doing anything.
trigger UpdateFieldValues on Case (after insert) {
        List<Accessory_Item__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,
                            Accessory__c = '197 - 360μm FiberFlex™ Fiber'));
            }
        }
        insert Accessories;
    }

 
James LoghryJames Loghry
David, looks like you're on the right track.  The issue with your trigger above appears that you are not setting "Accessory__c" correctly, however.  Accessory__c should be set to a Salesforce Id.  In your case, it should be set to the Id of the "197 - 360um FiberFlex" record.  

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.
David DurantDavid Durant
Could you give me an example of what the query would look like fetching the id of the record and inserting it into the code in the proper position. It makes sense doing it this way. I just thought that since i was going through a child object I wouldnt have to use SOQL in order to fetch the record ID.

Thank you again this gives another path to follow.
James LoghryJames Loghry

Sure.  Your trigger would look something like below:

trigger UpdateFieldValues on Case (after insert) {
    
    Id accessory360Id = [Select Id From Accessory__c Where Name='197 - 360μm FiberFlex™ Fiber' Limit 1].Id;

    List<Accessory_Item__c> accessories = new List<Accessory_Item__c>();
    for (Case c : Trigger.New) {
        if (c.Quick_Order__c == '360 Fiber') {
            accessories.add(new Accessory_Item__c(
                            Case__c = c.Id,
                            Accessory__c = accessory360Id));
        }
    }
    insert accessories;
}

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.
This was selected as the best answer
David DurantDavid Durant
Perfect! It works. Thank you again for you help I really appreciate it!

I am not framiliar with the custom setting approach. I will have to look into it and possibly impliment it in the future.
David DurantDavid Durant
James,

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.

User-added image
@isTest 
public class TestQuickUpdate {
    static testMethod void insertNewRecord() {
       
Case recordToCreate = new Case();
       
       recordToCreate.Origin         = 'Phone';
       recordToCreate.AccountId      = '001W000000I931R';
       recordToCreate.Site__c        = 'a0EW0000000fLBY';
       recordToCreate.Quick_Order__c = '360 Fiber';
       recordToCreate.ContactId      = '003W000000OAOFK';
       
insert recordToCreate;
    }
}
Thanks agian for your help.