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
Sarah@CBORDSarah@CBORD 

Apex Trigger giving a compile error

I am getting this error when I go to save my trigger "Compile Error: expecting a left angle bracket, found 'ProductName' at line 2 column 10"

 

 

trigger RenewalItemUpdate on SFDC_520_QuoteLine__c (before insert, before update) {
    List ProductName = new List();
        for (Integer i = 0; i < Trigger.new.size(); i++)
        {
            ProductName.add(Trigger.new[i].Product2__c);
        }
    List RenewalList = new List([Select Id from Annual_Renewal_Pricebook_Item__c
     where GP_Item_Number__c in :ProductName]);
        for (Integer i = 0; i < Trigger.new.size(); i++)
        {
            if (Trigger.new[i].Product2__c != null)
            {
                Trigger.new[i].GP_Item_Number__c = RenewalList[i].Id;
            }
            else
            {
            Trigger.new[i].GP_Item_Number__c = null;
            }
        }
}

 

Can anyone look at this and see why I am getting this error?  I just need a fresh set of eyes.

Thanks!

learnSFlearnSF

write second line as,

 

List<Object Name> ProductName = new List<Object Name>();

 

 

Sarah@CBORDSarah@CBORD
Thank you for the help but now it's saying "
Error: Compile Error: Invalid nested collection type at line 2 column 39 
"  maybe I should go back to try an s-control for this.
DManelskiDManelski
Can you paste your new code?
EricL_SilvaEricL_Silva

Hi, you may be putting a wrong Object name:

 

List<Object Name> ProductName = new List<Object Name>()

 

That must be the name of the object you want to get the "product name".

 

I hope it helps...

Sarah@CBORDSarah@CBORD
trigger RenewalItemUpdate on SFDC_520_QuoteLine__c (before insert, before update) {
    List<Object Name> ProductName = new List<Object Name>();
        for (Integer i = 0; i < Trigger.new.size(); i++)
        {
            ProductName.add(Trigger.new[i].Product2__c);
        }
    List RenewalList = new List([Select Id from Annual_Renewal_Pricebook_Item__c
     where GP_Item_Number__c in :ProductName]);
        for (Integer i = 0; i < Trigger.new.size(); i++)
        {
            if (Trigger.new[i].Product2__c != null)
            {
                Trigger.new[i].GP_Item_Number__c = RenewalList[i].Id;
            }
            else
            {
            Trigger.new[i].GP_Item_Number__c = null;
            }
        }
}
Message Edited by Sarah@CBORD on 01-23-2009 04:05 AM
Sarah@CBORDSarah@CBORD

Here's the background on my request which may have been helpful to add before :)

 

I am trying to populate a lookup on an object from a field on the same object.  The lookup will then go to another table and populate another field on this object with a formula using the lookup.

 

So I have the object that needs the lookup populated called SFDC_520_QuoteLine__c which had a field Annual_Renewal_Pricebook_Item__c that's a lookup and I need it to be populated with the value from Product2__c.  Product2__c is the same value as GP_Item_Number__c from Annual_Renewal_Pricebook_Item__c. 

 

Long story short I  need to populate a lookup field with a value from a field on the same object.  I am thinking maybe a s-control would be easier but I thought that a trigger would fire automatically when the object record is created or updated.

 

Thank you all for your help.

EricL_SilvaEricL_Silva

I think you did not get that:

 

you need to put this line on line 2 where Object Name must be a valid object, I mean an object as Annual_Renewal_Pricebook_Item__c, for example

List<Annual_Renewal_Pricebook_Item__c> ProductName = new List<Annual_Renewal_Pricebook_Item__c>();

 

With this, you create a list similar to the object Annual_Renewal_Pricebook_Item__c, did you get it?

DManelskiDManelski
And you'll need to do the same on line 7 as well.
Sarah@CBORDSarah@CBORD

I did do that.  Now my codes looks like:

trigger RenewalItemUpdate on SFDC_520_QuoteLine__c (before insert, before update) {
    List<SFDC_520_QuoteLine__c > ProductName = new List<SFDC_520_QuoteLine__c >();
        for (Integer i = 0; i < Trigger.new.size(); i++)
        {
            ProductName.add(Trigger.new[i].Product2__c);
        }
    List <Annual_Renewal_Pricebook_Item__c> RenewalList = new List<Annual_Renewal_Pricebook_Item__c>(
        [Select Id from Annual_Renewal_Pricebook_Item__c
             where GP_Item_Number__c in :ProductName]);
            for (Integer i = 0; i < Trigger.new.size(); i++)
            {
                if (Trigger.new[i].Product2__c != null)
                {
                    Trigger.new[i].GP_Item_Number__c = RenewalList[i].Id;
                }
                else
                {
                    Trigger.new[i].GP_Item_Number__c = null;
                }
        }
}

 

and it's throwing the error : Error: Compile Error: Incompatible element type Id for collection of SOBJECT:SFDC_520_QuoteLine__c at line 5 column 13

 

Frustrating when you are new to this type of code...sorry for the novice questions!  Thanks for the help though!

EricL_SilvaEricL_Silva

Did you try the another kind of FOR loop?

 

Try this:

 

Set<ID> ProductName = new Set<ID>(); //I don't know what kind of variable it will be, if it is string, change ID and put String for (SFDC_520_QuoteLine__c prodName : Trigger.new) { ProductName.add(prodName.Product2__c); }

 

 

 

 

Sarah@CBORDSarah@CBORD

Okay this code below works and will save:

trigger RenewalItemUpdate on SFDC_520_QuoteLine__c (after insert, after update) {     
    Set<String> ProductName = new Set<String>(); 
        for (SFDC_520_QuoteLine__c prodName : Trigger.new){
            ProductName.add(prodName.Product2__c);
        }
}
Now how do I take the value of the Product2__c field and update my lookup f ield Renewal_Item__c with that value.
EricL_SilvaEricL_Silva

The same way that you got a value from the other list you created, but on this one, it is only the field that you need, so you can use ProductName.get(i) or ProductName[i] or even create as the following:

for (String prodName1 : ProductName)

 As you wish or as it applies on you logic.

 

Any doubts fell free to ask...

Sarah@CBORDSarah@CBORD

Sorry to keep going back and forth on this.  I think I understand what you mean but when I try to apply it to my code it's giving me an error "

Error: Compile Error: Loop variable must be of type Object at line 6 column 43 
"

 

Here's my code:

trigger RenewalItemUpdate on SFDC_520_QuoteLine__c (after insert, after update) {    
        Set<object> ProductName = new Set<Object>();
           for (SFDC_520_QuoteLine__c prodName : Trigger.new){
           ProductName.add(prodName.Product2__c);
           } 
               for (SFDC_520_QuoteLine__c prodName1 : ProductName){
               ProductName.add(prodName1.Renewal_Item__c);
               }
}