0

I've created a trigger to populate Field 1 on Object 1 with Field 2 from Object 2 - when Field 2 is being created. But I am having troubles saving it. I am getting an error message:

"Error: Compile Error: Unexpected token '<'. at line 10 column 6 ".

I am new to Apex, so I am not sure what I am missing, so a bit of help would be appreciated.

*I have created a lookup relationship between the 2 objects.

Please see below the trigger:

trigger updatefield1 on Object_2__c (before insert, before update){

    List<Object_1__C> Obj1IDs = new List<Object_1__C>();

    for(Object_2__c obj: trigger.new){
        Obj1IDs.add(Ite.Object_1__c);
    }
    List <Object_1__c> obj1s = new List <Object_1__c> ([select id, field_1__c from Object_1__c, where id in: Obj1IDs]); - //this is line 10 - where the error is

    for (Object_2__c obj: trigger.new){
        for  (integer i=0; i < obj1s.size(); i++){

            if (obj.Object_1__c == obj1s[i].id{
                obj1s[i].field_1__c = field_2__c;
            }
        }
    }
    update obj1s;
}
3
  • Syntax error i.e. additional comma after Object_1__c – Vijay Ganji Nov 18 '19 at 10:59
  • Thanks for your reply. I've managed to get rid of that error message, but now I am getting a new one "Error: Compile Error: Variable does not exist:" on line "Obj1IDs.add(Ite.Object_1__c);". I have added "Ite" to the code, but doesn't seem to work. Any ideas, @VijayGanji? Thanks. – SSimion Nov 18 '19 at 11:19
  • Where have you declared "Ite" in this code? – User6670 Nov 18 '19 at 11:26
1

There is a comma after the object name in the SOQL. remove that from Object_1__c,

List <Object_1__c> obj1s = new List <Object_1__c> ([select id, field_1__c from Object_1__c where id in: Obj1IDs]);
  • //this is line 10 - where the error is
2
  • Thanks for your reply. I've managed to get rid of that error message, but now I am getting a new one "Error: Compile Error: Variable does not exist:" on line "Obj1IDs.add(Ite.Object_1__c);". I have added "Ite" to the code, but doesn't seem to work. Any ideas? Thanks. – SSimion Nov 18 '19 at 11:19
  • @SSimion it should be "obj", not "Ite" (since "obj" is the variable defined in your "for" loop scope). :) – Renato Oliveira Nov 18 '19 at 12:39
0

Close this if condition.

if (obj.Object_1__c == obj1s[i].id) { obj1s[i].field_1__c = field_2__c; }

0

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.