+ Start a Discussion

Simple Trigger - Testing Issues

Hello.  I am trying to write my first Apex test on a very simple trigger...and am having problems. 


My trigger (shown below) is fired upon the update of a custom object Customers__c.  The trigger looks at a separate object (WO__c) to see if any record's WOFlag__c field is greater than zero. If so, it updates the respective record's WOFlag__c  value into the CopiedWOFlag__c field in the same record.


This trigger works fine.  There are absolutely no problems with it.  My issue is trying to correctly write the test.  The below test code returns the error "Compile Error: Field is not writeable: WO__c.Name".


Any thoughts on why I am seeing this?  Any guidance would be greatly appreciated.


The test and trigger are listed below.

******** TEST CODE

public class TESTWOFlagGreaterThanZero { 
    static testMethod void TESTWOresave(){

        WO__c[] WOtoCreate = new WO__c[]{}; 
        for(Integer x=0; x<200;x++){ 
//using "9999" below since the field name is an auto number.  I am seeing the same "not writeable" error with both

//the "name" field and the "WOFlag__c" field
            WO__c wo = new WO__c(name=9999,WOFlag__c=1); 
        insert WOtoCreate; 



******** TRIGGER
trigger WOFlagGreaterThanZero on Customers__c (after update) {
List<WO__c> WOFlagGtrThanZero =
    [ SELECT j.WOFlag__c
    FROM WO__c j
    WHERE j.WOFlag__c > 0

for (WO__c upd: WOFlagGtrThanZero) {
       if ( upd.WOFlag__c > 0 ){
       upd.CopiedWOFlag__c = upd.WOFlag__c;

update WOFlagGtrThanZero;




The auto Number field gets populated automatically so you dont need to insert that value.

It will automatically assign the value.

What is the data type for "WOFlag__c" field?

The formula field also gets automatically assigned to a particular value.


Just mention the required fields of your custom object "WO__c".


It should work. Please let me know if further concerns.


Thanks.  To answer your question, the WOFlag__c field was a formula field - so I omitted that as well.  After that, I was able to save the test successfully - but it fails when I ran the test.


The falure message shows that two other required fields were not listed in the test (I will just call them custom1__c and custom2__c for simplicity in this response).  The catch here is that this WO__c object is a junction object - and the required custom1__c and custom2__c fields are actually the two master objects of the junction object WO__c.


I have tried to insert dummy values for custom1__c and custom2__c - but those fail too.


Any ideas on how I can get the test to run successfully.


I appreciate the help with this.




Could you please post your updated code?




see the values that you are passin is not a plain text you need to pass the record id on your both custom object field because these fields are master detail right.



Thanks all.  I think I am going to just work around my issue with a different aproach.  I do appreciate the help.