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
Jan Kopejtko 2Jan Kopejtko 2 

Help with a simple code

I am writing a simple piece of code that will create a record on the detail side of a relationship upon creation of a record on the master side of relationship, if a checkbox is checked.
 
trigger SilaTrigger on Sila__c (after insert) {
    for(Sila__c a : Trigger.New) {
        if(a.Checkbox__c = true) {
            Dependent__c b = new Dependent__c();
            b.Name = a.Name;
            b.Sila__c = a.Id;
            insert b;
        } else return;
    }
    

}

However I am recieving an error:

SilaTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.SilaTrigger: line 3, column 1

How do I fix this? I think I am supposed to use "after insert" because I need to know the ID of Master record, so I can populate the detail field with the ID of the master.

Thanks alot
Best Answer chosen by Jan Kopejtko 2
SarvaniSarvani
Hi Jan, 

Please replace "=" with "==" in your condition check like if(a.checkbox__c==true)

Thanks

All Answers

SarvaniSarvani
Hi Jan, 

Please replace "=" with "==" in your condition check like if(a.checkbox__c==true)

Thanks
This was selected as the best answer
Andrew GAndrew G
Yep.  
In Apex, == is an equivalent check,  = is an assignment of a value to a variable. 

Regards