You need to sign in to do that
Don't have an account?
Oron Mizrachi
get a related product name in an opportunity field
hi,
im trying setting an opportunity field with the name of a related product (there is always only one related product to each opportunity).
but , because im a new admin , im yet not fully control apex,
i though on trying OpportunityLineItems object (in an opportunity apex trigger) and form there get the connection
but i couldn't find the right code for using the LineItems on Opportunity controller.
could some one help me with the code, its very important for us.
thnx
oron.
im trying setting an opportunity field with the name of a related product (there is always only one related product to each opportunity).
but , because im a new admin , im yet not fully control apex,
i though on trying OpportunityLineItems object (in an opportunity apex trigger) and form there get the connection
but i couldn't find the right code for using the LineItems on Opportunity controller.
could some one help me with the code, its very important for us.
thnx
oron.
You can use something like this:
Here, I am updating the custom_field__c in opportunity with a product name in the opportunity line items.
Let me know if this helps.
All Answers
You can use something like this:
Here, I am updating the custom_field__c in opportunity with a product name in the opportunity line items.
Let me know if this helps.
but, im getting an error - Compile Error: Illegal assignment from Opportunity to List<Opportunity> at line 3 column 20
thanx again
Use this line instead. This will solve your error. And plz select as answer if it helps.
Opportunity is related to OpportunityLineItem which is related to product .As you want product Name to opportunity ,As Opportunity and OpportunityLineItem are One to many related .One opportunity may have more then one product So you want all product name to ooprtunity or only one product name arbitarily to opportunity .
if you need all product then you need to write trigger on OpportunityLine Item .
Please confirm .
.
thank you , thats work great.
i needed to edit the code alitle bit (some small syntax errors) so for later readers ill put the working edition below,
thank you very much for your time.
Manoj - no, we only have 1 product per Opp. thanx any way.
the code:
trigger PrimaryProduct on Opportunity (before update) {
list<opportunity> lt = trigger.new;
list<opportunityLineItem> opli = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where opportunityId =: trigger.new[0].id]);
string productName='';
for(opportunityLineItem o : opli){
productName = o.product2.name;
}
for(Opportunity opp : trigger.new){
opp.Opportunity_Prim_Product_Name__c = productName;
}
}
thanx again.
trigger PrimaryProduct on Opportunity (before update) {
list<opportunity> lt = trigger.new;
list<opportunityLineItem> opli = new list<opportunityLineItem>([select id ,product2.name, opportunityId,UnitPrice,Quantity from opportunitylineitem where opportunityId =: trigger.new[0].id]);
string productName='';
decimal salesprice='';
integer Quantity='';
for(opportunityLineItem o : opli){
productName = o.product2.name;
salesprice=o.UnitPrice;
Quantity =o.Quantity;
}
for(Opportunity opp : trigger.new){
opp.Opportunity_Prim_Product_Name__c = productName;
opp.product_qunt__c=Quantity;
opp.product_salesprice__c=salesprice;
}
}
thank you.
but im getting an error - Error: Compile Error: Illegal assignment from Decimal to String at line 6 column 3,
what do you suggest?
sorry declare like this , as it is decimal u cant assign to string . its about datatypes
string productName='';
decimal salesprice;
integer Quantity;