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

Problems with SOQL Query causing compile error
Hey all,
I'm trying to teach myself a little bit of Apex and I'm doing it by coming up with problems and then trying to solve them. I'm trying to write a trigger that will update text fields on a parent account from a child. Basically, I've created several fields on an Opportunity called Product1, Product2, etc. I'm working on an Apex trigger that will read all line items associated with that opportunity and put them in the correct fields. I'm not overly concerned with how it looks in the field, I'm just trying to get it to work...
When I try to save, I'm getting an unexpected token error at line 7 column 37. I'm guessing this has to do with how I attempted my SOQL statement, but I've tried several variations and I keep getting the same error. Here is the code:
trigger SetOppProducts on OpportunityLineItem (after insert, after update, after delete) {
String TheProduct, OppName;
Integer TheCount = 0;
OpportunityLineItem x = trigger.new;
Opportunity o = new Opportunity (Id = x.OpportunityId);
for(OpportunityLineItem[] oli : [select Id where OpportunityId = x.OpportunityId] {
TheProduct = 'Coated: ';
TheProduct += x.Coated__c;
TheProduct += ', Felted: ';
TheProduct += x.Felted__c;
TheCount++
if (TheCount = 1) {o.Product1__c = TheProduct} {
else if (TheCount = 2) {o.Product2__c = TheProduct} {
else if (TheCount = 3) {o.Product3__c = TheProduct} {o.Product4__c = TheProduct}
}
update o;
}
Any direction as to where I'm going wrong would be much appreciated. Thanks!
I'm trying to teach myself a little bit of Apex and I'm doing it by coming up with problems and then trying to solve them. I'm trying to write a trigger that will update text fields on a parent account from a child. Basically, I've created several fields on an Opportunity called Product1, Product2, etc. I'm working on an Apex trigger that will read all line items associated with that opportunity and put them in the correct fields. I'm not overly concerned with how it looks in the field, I'm just trying to get it to work...
When I try to save, I'm getting an unexpected token error at line 7 column 37. I'm guessing this has to do with how I attempted my SOQL statement, but I've tried several variations and I keep getting the same error. Here is the code:
trigger SetOppProducts on OpportunityLineItem (after insert, after update, after delete) {
String TheProduct, OppName;
Integer TheCount = 0;
OpportunityLineItem x = trigger.new;
Opportunity o = new Opportunity (Id = x.OpportunityId);
for(OpportunityLineItem[] oli : [select Id where OpportunityId = x.OpportunityId] {
TheProduct = 'Coated: ';
TheProduct += x.Coated__c;
TheProduct += ', Felted: ';
TheProduct += x.Felted__c;
TheCount++
if (TheCount = 1) {o.Product1__c = TheProduct} {
else if (TheCount = 2) {o.Product2__c = TheProduct} {
else if (TheCount = 3) {o.Product3__c = TheProduct} {o.Product4__c = TheProduct}
}
update o;
}
Any direction as to where I'm going wrong would be much appreciated. Thanks!
[select Id where OpportunityId = :x.OpportunityId]
I've incorporated your feedback however I'm still getting the same error (apparently the problem is happening before even that problem) Here is the updated code:
trigger SetOppProducts on OpportunityLineItem (after insert, after update, after delete) {
String TheProduct, OppName;
Integer TheCount = 0;
OpportunityLineItem x = trigger.new;
Opportunity o = new Opportunity (Id = x.OpportunityId);
for(OpportunityLineItem[] oli : [ select Id where OpportunityId = :x.OpportunityId ] {
TheProduct = 'Coated: ';
TheProduct += x.Coated__c;
TheProduct += ', Felted: ';
TheProduct += x.Felted__c;
TheCount++
if (TheCount = 1) {o.Product1__c = TheProduct} {
else if (TheCount = 2) {o.Product2__c = TheProduct} {
else if (TheCount = 3) {o.Product3__c = TheProduct} {o.Product4__c = TheProduct}
}
update o;
}
I'm getting the Unexpected Token error at Line 7 Column 37, which is the left bracket on the SOQL string before 'select'. I'm sure it's a small thing I left out, but I can't figure out what it is...
Thanks again for your quick response!
For example, [select Id From OpportunityLineItem where OpportunityId = x.OpportunityId
-----------------------------------------------------------------------------------------
trigger SetOppProducts on OpportunityLineItem (after insert, after update, after delete) {
String TheProduct, OppName, Product1, Product2, Product3, Product4;
Integer TheCount;
for( OpportunityLineItem x : trigger.new) {
Opportunity o = new Opportunity (Id = x.OpportunityId);
//THIS IS THE OPPORTUNITY I WANT TO UPDATE AT THE END
TheCount = 0;
for(OpportunityLineItem oli : [select Id, Coated__c, Felted__c from OpportunityLineItem where Id = :x.OpportunityId]) {
//THIS IS SUPPOSED TO PULL ALL OF THE LINE ITEMS THAT ARE ASSOCIATED WITH THE OPPORTUNITY, BUT IT'S NOT...
TheProduct = 'Coated: ';
TheProduct += oli.Coated__c;
TheProduct += ', Felted: ';
TheProduct += oli.Felted__c;
TheCount++;
system.debug('TheProduct=' + TheProduct);
if (TheCount == 1) {Product1 = TheProduct;}
//THIS IS HOW I'M STORING THE INFORMATION UNTIL I'M READY TO DUMP IT TO OPPORTUNITY FIELDS
//THESE VARIABLES WERE DECLARED AS STRINGS ABOVE
else if (TheCount == 2) {Product2 = TheProduct;}
else if (TheCount == 3) {Product3 = TheProduct;}
else {Product4 = TheProduct;}
}
o.Product1__c = Product1;
o.Product2__c = Product2;
o.Product3__c = Product3;
o.Product4__c = Product4;
//THIS IS HOW I'M DOING THE DUMP
update o;
}
}
--------------------------------------------------------------------
I'm aware that I will evenutally have to tweak this for the type of action that sets off the trigger (i know this won't work right now on delete) but I at least want this to function for insert and update for now. I'm working on the fifteenth floor of the building, and my computer is going to take a long fall soon if I don't figure this out. Thanks again for all of your help!
This script now works for Insert and Update, but now I'm getting an error on delete saying "Attempt to de-reference a null object"
As soon as I figure this out I'll post a final solution.