0

I keep getting Error: Compile Error: unexpected token: 'trigger' at line 1 column 0 with this simple trigger

    trigger CreateWorkRecordsX on Staff_Week_Plan_Creator__c (after insert) {        

        List <Staff_work_records__c>  WR1 = new List <Staff_work_records__c> ();

        for(Staff_Week_Plan_Creator__c o:trigger.new)
        {

            WR1.Report_Date__c=o.Start_Date_of_Week__c;

        }
                insert WR1;
}

Purpose of this trigger is to create new Staff_work_records__c record after Staff_Week_Plan_Creator__c has been inserted.

Very new to apex, any ideas?

1
  • I think you are creating a trigger directly Your Name | Setup | Create | Apex Classes (this is not the right way) The correct way to do this is to go to the standard/custom object itself and click on the Triggers and clicking the new button there. Oct 8, 2016 at 13:02

2 Answers 2

4

This error appears when you try to create an Apex Class with an Apex Trigger code. Those two metadata types are syntax-similar, but they are totally different. Check if you are trying to save this trigger as .apxc in your developer console. If so, then that's your issue. You should save an .apxt instead.

Example:

enter image description here

The file on the left is a class, and the one in the right is a trigger.

Your code was wrong too, just like itzmukeshy7 said. But I think that if this error appears after you made the suggested modifications, then I think my post will help you now.

0
3

Try this ;)

Problem in this line

WR1.Report_Date__c=o.Start_Date_of_Week__c;

Here WR1 is a list of Staff_work_records__c so you can directly update a property of it's element. Maybe something like this:

trigger CreateWorkRecordsX on Staff_Week_Plan_Creator__c (after insert) {        
    List <Staff_work_records__c>  WR1 = new List <Staff_work_records__c> ();

    for(Staff_Week_Plan_Creator__c o:trigger.new){
        WR1.add(new Staff_work_records__c(Report_Date__c = o.Start_Date_of_Week__c));
    }

    insert WR1;
}
5
  • Hi, Still the same exact error
    – Rufus K.
    Oct 8, 2016 at 10:07
  • @Kollo Can you please update question with the latest code? Oct 8, 2016 at 10:31
  • @Kollo what is this field Report_Date__c ?
    – Anzar
    Oct 8, 2016 at 11:51
  • @Anzar Report_Date__c is a field of a new Staff_work_records__c record I'm trying to create
    – Rufus K.
    Oct 9, 2016 at 15:41
  • 2
    @Anzar, you were right about my code too. After I saved as trigger rather than class, your updated code worked. Thanks
    – Rufus K.
    Oct 9, 2016 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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