You need to sign in to do that
Don't have an account?
amyh
Table Lookup with Variable within Trigger
Hello,
I'm very close to having my trigger fully functional, but I need to do a simple table
lookup for the record I am inserting. Could you please help me with the syntax.
Working Hardcoded:
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [(select name ) from Product2 where ProductCode = 'My Product Code']; //:myid
Non Working with Variable
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [(select name ) from Product2 where ProductCode = :myID];
I don't really get an error message except that my Test now fails when it attempts the insert.
Thank You/
Full Code:
trigger CreateCaseAfterShippingConfirmsOrder on SFDC_520_QuoteLine__c
(after insert, after update)
{
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [select name from Product2 where ProductCode = :myID]; //:myid
if (obj.Shipping_Confirms_Order__c == True) {
Case newCase = new Case(
Subject = 'New Data Production Project',
Description = p.name + ' ' + obj.Description__c,
Status = 'Not Started',
Product__c = 'Custom Data',
Priority = 'Sev-4 (when resources allow)',
Origin = 'Web'
);
insert newCase;
}
}
}
I'm very close to having my trigger fully functional, but I need to do a simple table
lookup for the record I am inserting. Could you please help me with the syntax.
Working Hardcoded:
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [(select name ) from Product2 where ProductCode = 'My Product Code']; //:myid
Non Working with Variable
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [(select name ) from Product2 where ProductCode = :myID];
I don't really get an error message except that my Test now fails when it attempts the insert.
Thank You/
Full Code:
trigger CreateCaseAfterShippingConfirmsOrder on SFDC_520_QuoteLine__c
(after insert, after update)
{
ID myID = null;
for (SFDC_520_QuoteLine__c obj : Trigger.new) {
myID = obj.Product2__c ;
Product2 p = [select name from Product2 where ProductCode = :myID]; //:myid
if (obj.Shipping_Confirms_Order__c == True) {
Case newCase = new Case(
Subject = 'New Data Production Project',
Description = p.name + ' ' + obj.Description__c,
Status = 'Not Started',
Product__c = 'Custom Data',
Priority = 'Sev-4 (when resources allow)',
Origin = 'Web'
);
insert newCase;
}
}
}
Thank you so much for your time! The code looks much better, and scalable too (this will help me with
future triggers for sure).
I am getting an error on this line:
for (SFDC_520_QuoteLine__c obj : Trigger.new)
{
myIDs = obj.Product2__c ;
}
Error: Illegal assignment from ID to LIST:Id
In my custom object which is a Sales Order Line Item, I'm trying to use the field Product2__c which is a Data Type: Lookup
I was assuming that this was the unique id link to the product from the product table, but maybe I don't understand
the data types well enough.
Any thoughts?
thanks!
You need to "add" each Product2 id to the list object.
for (SFDC_520_QuoteLine__c obj : Trigger.new)
{
myIDs.add(obj.Product2__c);
}
Thanks,
Drew
If I could just ask you one more follow up.
This line:
Map<Id,Product2> productMap = new Map<Id,Product2>([select name from Product2 where ProductCode = :myIDs]);
throws the error:
Save error: Invalid bind expression type of LIST:Id for column of type String
where this does work:
Map<Id,Product2> productMap = new Map<Id,Product2>([select name from Product2 where ProductCode = 'MyTestProduct']);
Would I do some kind of convert?
thanks!!
Map<Id,Product2> productMap = new Map<Id,Product2>([select name from Product2 where ProductCode IN :myIDs]);
Now I'm on to trying to write a test script for this.
Thanks for your help!!