5

This question is related to a previously answered question: Trigger won't Fire

It looks like my organization has an existing test class that isn't playing nicely with my new Trigger. Could someone please help me get rid of the error. I think it's because another trigger has the record open and I'm trying to do something else with it? Any insight is greatly appreciated.

Here is the error:

System.DmlException: Update failed. First exception on row 0 with id a0r40000005MyUPAA0; 
first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MDwinning: execution of AfterUpdate 
caused by: System.FinalException: Record is read-only
Trigger.MDwinning: line 4, column 1: [] MDMeetingcStatsTT.cls   /src/classes    line     13

The test class that was there for a different trigger is here:

@isTest
private class MDMeetingcStatsTT {
static testMethod void testTrigger() {
try {
     MD_Meeting__c o = new MD_Meeting__c();
   insert o;

    System.assertNotEquals(null, o);
 }
  catch(Exception e) {
   List<MD_Meeting__c> l = [SELECT Id from MD_Meeting__c LIMIT 1];
   update l;
   System.assertNotEquals(null, l);
  }
 }
}

And here is my trigger that (thanks to user @eyescream) includes all the required opportunity fields:

trigger MDwinning2 on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
    for (MD_Meeting__c m : Trigger.new) {
    if (m.SAL__c=True) {    
     Opportunity o = new Opportunity ();   
    o.OwnerId = m.Sales_Director__c;
    o.Name = m.Name;
    o.StageName = 'Generate Opportunity';
    o.Market_Developer__c = m.Market_Developer__c;
    o.AccountId = m.Account__c;
    o.Type = 'Sales - New Business';
    o.CloseDate = System.Today()+150;
    o.MeetingLookup__c = m.Id;
    oppToInsert.add(o);
    }//end if
}//end for o
//try {
//        insert oppToInsert; 
//    } catch (system.Dmlexception e) {
//       system.debug (e);
//    } 
}

1 Answer 1

5

On line 4: if (m.SAL__c=True) { use the == operator for the equality check.

The compiler will let you use the = operator incorrectly for some reason.

This is read as an attempt to assign m.SAL__c the value of true which throws the exception you are seeing.

3
  • Odd. I don't recall Apex Code allowing that previously. I wonder when that changed? Also, it is for that reason that I always recommend that one doesn't compare Boolean values with Boolean values. Simply stating if(m.sal__c) is the same thing.
    – sfdcfox
    Commented Jan 23, 2014 at 14:20
  • @sfdcfox Blast from the past, but this syntax works for checkbox only. Same reason you could do Boolean a, b; a = b = true;. This behavior seems like it would always have been a part of Apex, no?
    – Adrian Larson
    Commented Aug 30, 2016 at 18:00
  • @AdrianLarson I've since learned how to wrangle this behavior. I should probably write a post about it...
    – sfdcfox
    Commented Aug 30, 2016 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .