• jstein
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
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!
  • August 06, 2008
  • Like
  • 0
I'm new to Apex, and I'm trying to write a trigger that will copy several fields from a Line Item into a text field on the opportunity so I can pull them into an email template. I can get the first set to populate, but I want to be able to put several line items on the Opportunity. I've created several field on the Opp called Product1, Product2, Product3, etc.

The issue I'm having is that I'm trying to run a conditional statement that checks to see if there is already a value in the first product field, if not, put the value there, else check the next field and continue on. The problem is that when I use null the statement is always evaluating to true, and when I use '' it is always evaluating to false.

The code I'm using is below:
trigger UpdateOppProducts on OpportunityLineItem (after insert, after update) {
String TheProduct;

for (OpportunityLineItem x : trigger.new) {
Opportunity o = new Opportunity (Id = x.OpportunityId);
TheProduct = 'Coated: ';
TheProduct += x.Coated__c;
TheProduct += ', Felted: ';
TheProduct += x.Felted__c;

if (o.Product1__c == null) {
o.Product1__c = TheProduct;
} else if (o.Product2__c == null) {
o.Product2__c = TheProduct;
} else if (o.Product3__c == null) {
o.Product3__c = TheProduct;
}
//o.Product_List__c += TheProduct;
update o;
}

}


Also, eventually I would like to have this run for all Line Items that are associated with Opportunity o every time any line item is created, edited, or deleted (i'm guessing this is some sort of loop statement) so if anyone has any ideas on this it would also be very appreciated.

Thank you very much in advance to any advice anyone can give an Apex beginner!!!!
  • August 04, 2008
  • Like
  • 0
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!
  • August 06, 2008
  • Like
  • 0
I'm new to Apex, and I'm trying to write a trigger that will copy several fields from a Line Item into a text field on the opportunity so I can pull them into an email template. I can get the first set to populate, but I want to be able to put several line items on the Opportunity. I've created several field on the Opp called Product1, Product2, Product3, etc.

The issue I'm having is that I'm trying to run a conditional statement that checks to see if there is already a value in the first product field, if not, put the value there, else check the next field and continue on. The problem is that when I use null the statement is always evaluating to true, and when I use '' it is always evaluating to false.

The code I'm using is below:
trigger UpdateOppProducts on OpportunityLineItem (after insert, after update) {
String TheProduct;

for (OpportunityLineItem x : trigger.new) {
Opportunity o = new Opportunity (Id = x.OpportunityId);
TheProduct = 'Coated: ';
TheProduct += x.Coated__c;
TheProduct += ', Felted: ';
TheProduct += x.Felted__c;

if (o.Product1__c == null) {
o.Product1__c = TheProduct;
} else if (o.Product2__c == null) {
o.Product2__c = TheProduct;
} else if (o.Product3__c == null) {
o.Product3__c = TheProduct;
}
//o.Product_List__c += TheProduct;
update o;
}

}


Also, eventually I would like to have this run for all Line Items that are associated with Opportunity o every time any line item is created, edited, or deleted (i'm guessing this is some sort of loop statement) so if anyone has any ideas on this it would also be very appreciated.

Thank you very much in advance to any advice anyone can give an Apex beginner!!!!
  • August 04, 2008
  • Like
  • 0