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
jsteinjstein 

Conditional Evaluations are always evaluating to true

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!!!!
SuperfellSuperfell
Opportunity o = new Opportunity (Id = x.OpportunityId);

will create a new opportunity object in memory, all its fields will be null (except for the Id which you set), therefore your subsequent field checks will have null values. If you want the actual values of that opportunity from the Db, you're going to have to read it from the DB with a query.
jsteinjstein
Thanks you very much for your help.

I still am having trouble running this query, namely because I've never run a query before. I'm using some of the material provided by Salesforce, and from that I'm using the following line:

Opportunity o = [Select name, Product1__c, Product2__c from Opportunity where Id = x.OpportunityId];

When I try to save however I'm receiving an 'unexpected token' error. Does anyone know the proper syntax for this query? After I've found a working solution I will post it for all future users in my position.

Thanks again!
canonwcanonw
You miss the colon as a reference to variable x.

Code:
Opportunity o = [Select name, Product1__c, Product2__c from Opportunity where Id = :x.OpportunityId];

 
And it would be a good habit to code this way.
Code:
List<Opportunity> oppList = [Select name, Product1__c, Product2__c from Opportunity where Id = :x.OpportunityId];
for(Opportunity opp : oppList) {
// Do something
}

 The later coding style would take care null reference by itself.