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
girbotgirbot 

Trigger: Expression cannot be assigned at line -1 column -1

Hello All,

This is my first go at writing an APEX trigger so apologies if there is something obvious I am missing...

My aim: When the object event is created, the region field is set depending on the value of a field called country.

This is what I am trying at the moment:

 

 

trigger EventRegion on Event__c(after insert){
        
        Event__c[] courseList = trigger.new;  
        if(Event__c.Country__c='UK') {
        Event__c.Region__c='1';
    
}// else nothing  
}

 

 

The above gives me - Error: Compile Error: Expression cannot be assigned at line -1 column -1 

 

I've read about how it is prefferable to support the bulk nature of triggers, but I quite get my head around it. Am I missing something obvious?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

a single = means you can trying to set Event__c.Country__c equals to 'UK'. If you want to do a comparison you need to use a double ==

Also, Trigger.New is already a list, so you don't need to add the collection to a new list - you can just loop through it as is.

 

And to update a record in a trigger, you have to update in the before insert event, not the after insert (you would typically use that if you want to update some other objects due to an update by these records)

 

Finally, you could actually do this via workflow I think - so I would check that you need to write this as a trigger before using triggers...

 

But for reference, you can then write the trigger like this:

 

 

trigger EventRegion on Event__c(before insert){
        
        for (Event__c e: trigger.new) {  
        if(e.Country__c =='UK') {
        e.Region__c = '1';

}
    
 
} //end trigger

 

 

All Answers

BritishBoyinDCBritishBoyinDC

a single = means you can trying to set Event__c.Country__c equals to 'UK'. If you want to do a comparison you need to use a double ==

Also, Trigger.New is already a list, so you don't need to add the collection to a new list - you can just loop through it as is.

 

And to update a record in a trigger, you have to update in the before insert event, not the after insert (you would typically use that if you want to update some other objects due to an update by these records)

 

Finally, you could actually do this via workflow I think - so I would check that you need to write this as a trigger before using triggers...

 

But for reference, you can then write the trigger like this:

 

 

trigger EventRegion on Event__c(before insert){
        
        for (Event__c e: trigger.new) {  
        if(e.Country__c =='UK') {
        e.Region__c = '1';

}
    
 
} //end trigger

 

 

This was selected as the best answer
girbotgirbot

Great thank you, that has worked a treat. Also didn't know about the difference in = and ==.

 

Thanks again!