You need to sign in to do that
Don't have an account?

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!
write second line as,
List<Object Name> ProductName = new List<Object Name>();
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...
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;
}
}
}
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.
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?
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!
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); }
Okay this code below works and will save:
Now how do I take the value of the Product2__c field and update my lookup f ield Renewal_Item__c with that value.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:
As you wish or as it applies on you logic.
Any doubts fell free to ask...
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 "
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);
}
}