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
bstangerbstanger 

Insert not working in trigger

I have an sObject "CLCA_Sub_Category__c" that I don't seem to be able to insert into.  Below is the trigger I'm using.  There are no records in "CLCA_Sub_Category__c" so the first SOQL query does evaluate to isEmpty().

 

Anyone have any thoughts?

 

 

 

trigger GetCTQCategory on Case (before insert, before update) {

    Case[] cs = trigger.new;   
   
    //Add Case.CLCA_Sub_Category Id & Value to CLCA Sub Category Object, if not already there
   
    CLCA_Sub_Category__c[] clcaSub = [Select Name, CaseSubCategoryId__c FROM CLCA_Sub_Category__c
                                    where CaseSubCategoryId__c = :cs[0].Sub_Category__c];

    if (clcaSub.isEmpty()==true)
    {
        CLCA_Sub_Category__c clcaSubNew = new CLCA_Sub_Category__c (Name='Test',
                                            CaseSubCategoryId__c=cs[0].Sub_Category__c);
        insert clcaSubNew;
       
    }
   
}

SuperfellSuperfell
how is it not working? do you get an error?
bstangerbstanger

I don't get an error message, it just doesn't insert anything.  Here's the test method:

 

 

public class TestGetCTQCategory { static testMethod void myTest() { List<Case> myCase = [Select Id, Project__c, Teacher__c, Category__c, CTQ_Category__c, Type, Sub_Category__c From Case limit 1]; Case cs = new Case(Project__c = myCase[0].Project__c, Type = myCase[0].Type, Subject = 'Test', Category__c = myCase[0].Category__c, Sub_Category__c = myCase[0].Sub_Category__c, Session_Id__c=0, Sessions_Impacted__c = 1, Sessions_Missed__c = 0, Teacher__c=myCase[0].Teacher__c); insert cs; //myCase = [Select Id, Project__c, Teacher__c, Category__c, CTQ_Category__c, Type, // Sub_Category__c From Case where Id=:myCase[0].Id]; //system.assertEquals('1234', myCase[0].CTQ_Category__c); //system.assertEquals(myCase[0].CTQ_Category__c, cs.CTQ_Category__c); List<CLCA_Sub_Category__c> clcaSub = [Select Name, CaseSubCategoryId__c FROM CLCA_Sub_Category__c where CaseSubCategoryId__c = :myCase[0].Sub_Category__c]; system.assertEquals(clcaSub[0].CaseSubCategoryId__c, myCase[0].CTQ_Category__c); } }

 


 

WesNolte__cWesNolte__c

Hey

 

I would suggest that as a first step you trace your execuation path using system.debug message eg.

 

System.debug('Subcat: '+cs[0].Sub_Category__c);

 

 CLCA_Sub_Category__c[] clcaSub = [Select Name, CaseSubCategoryId__c FROM CLCA_Sub_Category__c
                                    where CaseSubCategoryId__c = :cs[0].Sub_Category__c];


System.debug('Is the list empty? '+clcaSub.isEmpty());

 

   if (clcaSub.isEmpty()==true)
    {
        CLCA_Sub_Category__c clcaSubNew = new CLCA_Sub_Category__c (Name='Test',
                                            CaseSubCategoryId__c=cs[0].Sub_Category__c);
        insert clcaSubNew;
       
    }

 

A few more message wouldn't hurt, perhaps just inside the if-block and just after. Debug messages are a powerful developer tool, crude, but important.

 

Wes

bstangerbstanger

Wes,

 

thanks for the reply.  I've tried to put debug code in, but when I do my testing in Eclipse, the debug statement results don't show up in the Eclipse debug log.  They also don't show up in the debug log in SF.  Where can I go go view debug statement results in Eclipse.

 

Brent

WesNolte__cWesNolte__c

Heya

 

It took me a while to find it too:)  Are you looking in Setup > Administration Setup > Monitoring > Debug logs?

 

Wes

bstangerbstanger

Wes,

 

Thanks for the note.  I did find the right debug log.  Thanks for the heads up.  Also, solved the problem.  It wouldn't work in the Sandbox, but did in production.  Sandbox needs updated, but can't do that for a week or so.  Thanks for the help.

 

Brent