1

Upon creation of new user, in context of private sharing model, I want to create new default records for Sobject jobs__c which need to be identical across the organization (as these are referenced by other workflows, etc...). Therefore, with creation of a new user, we want to create new jobs__c records with the same name and with each new user as record owner...

However, this code throws a read-only exception.

To wit: log reads: System.final exception: record is read-only.

What has me puzzled is that if I leave out the line of code which is Comment in this version, the trigger compiles, test class runs passes everything, and the trigger performs as desired... except, no filter on records effected.

I have written this code snippet in other triggers for custom objects, filtering collection of the trigger "for" loop... never encountered this error.

Any thoughts, anybody? I am certainly not an expert on this; got to be somebody who can explain this.

 trigger addnulljob on User (after insert) {
 list <jobs__c> newusers = new list <Jobs__c>();    
    for (user brandnew : trigger.new) {
//    LINE BELOW THROWS READ ONLY EXCEPTION ON INSERT...    
//      if (brandnew.contract_app_user__c = true){    
       jobs__c a = new jobs__c();    
       a.name = 'null job';    
       a.ownerid = brandnew.id;
       newusers.add(a);    
    }         
  if (!newusers.isempty())
  insert newusers;
}
6

You can't assign a value to the Trigger.new collection in an after trigger. brandnew is a record coming from the trigger, so when you attempt to assign true it give you an error.

Now, more importantly, I don't believe you are actually trying to assign the value true to that field, I assume you're trying to perform an "equals" conditional. You will need to write it as if (brandnew.contract_app_user__c == true){ with the double equals sign.

  • 2
    An even better way is to remove the == true altogether. If brandnew.contract_app_user__c is a checkbox, its value will provide the conditional all by itself. – Bradley Delaune Jan 21 '15 at 21:46
  • See... I knew it had to be something really lame like that:) Really great way to beat apex operators into your skull: run up against brick wall for hours on end. Thanks for the correctional advise. Everything now hunky-dory! – VeeDee Jan 21 '15 at 22:54
0

if (brandnew.contract_app_user__c == true) or if(brandnew.contract_app_user__c)

correct if condition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.