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
Oron MizrachiOron 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.


Best Answer chosen by Oron Mizrachi
Ajay K DubediAjay K Dubedi
Hi Oron,
You can use something like this:
 
list<opportunity> lt = trigger.new[0];
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.custom_field__c = productName;
 lt.add(opp);
}
update lt;

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

Ajay K DubediAjay K Dubedi
Hi Oron,
You can use something like this:
 
list<opportunity> lt = trigger.new[0];
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.custom_field__c = productName;
 lt.add(opp);
}
update lt;

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.
This was selected as the best answer
Oron MizrachiOron Mizrachi
hi man , thanx alot for the answer ( im both channels (: )

but, im getting an error - Compile Error: Illegal assignment from Opportunity to List<Opportunity> at line 3 column 20

thanx again
 
Oron MizrachiOron Mizrachi
line 3 is first line in your code
Ajay K DubediAjay K Dubedi
list<opportunity> lt = trigger.new;


Use this line instead. This will solve your error.  And plz select as answer if it helps.
ManojjenaManojjena
H Oron Mizrachi,

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 .

.

 
Oron MizrachiOron Mizrachi
Ajay,

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;


}


}
Oron MizrachiOron Mizrachi
Follow up question  - if i would like to get the Product Sales Price and Quantity aswell, and set them in diffrent fields , what should i add to the code?


thanx again.
Narsimulu ChiranjiNarsimulu Chiranji

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;

}


}
Oron MizrachiOron Mizrachi
hi Narsimulu, 

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?

 
Narsimulu ChiranjiNarsimulu Chiranji

sorry declare like this , as it is decimal u cant assign to string . its about datatypes 
string productName='';
decimal salesprice;
integer Quantity;