You need to sign in to do that
Don't have an account?
Smurfet15
trigger to update opportunity checkbox when adding/update opportunity line item
Hi there,
My requirement is to update(check) opportunity checkbox everytime there is a product added that has product category = "Frame".
Can you please help me?
Nothing is displaying on this : system.debug('Display'+ Olipc);
Below is my code:
--------------------------------------
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;
List<Opportunity> opprt = new List<Opportunity>();
for(OpportunityLineItem Oli: trigger.new){
OppID = Oli.OpportunityID;
Olipc = Oli.PricebookEntry.Product2.product_Category__c ;
system.debug('Display'+ Olipc);
if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
{
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;
opprt.add(oppy);
}
}
}
My requirement is to update(check) opportunity checkbox everytime there is a product added that has product category = "Frame".
Can you please help me?
Nothing is displaying on this : system.debug('Display'+ Olipc);
Below is my code:
--------------------------------------
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;
List<Opportunity> opprt = new List<Opportunity>();
for(OpportunityLineItem Oli: trigger.new){
OppID = Oli.OpportunityID;
Olipc = Oli.PricebookEntry.Product2.product_Category__c ;
system.debug('Display'+ Olipc);
if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
{
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;
opprt.add(oppy);
}
}
}
You didn't get related fields in trigger.new, you need to query it:
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c from OpportunityLineItem where Id IN: trigger.new ];
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;
List<Opportunity> opprt = new List<Opportunity>();
for(OpportunityLineItem Oli: oppLineItems ){
OppID = Oli.OpportunityID;
Olipc = Oli.PricebookEntry.Product2.product_Category__c ;
system.debug('Display'+ Olipc);
if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
{
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;
opprt.add(oppy);
}
}
}
Thanks,
Neetu
All Answers
You didn't get related fields in trigger.new, you need to query it:
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c from OpportunityLineItem where Id IN: trigger.new ];
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;
List<Opportunity> opprt = new List<Opportunity>();
for(OpportunityLineItem Oli: oppLineItems ){
OppID = Oli.OpportunityID;
Olipc = Oli.PricebookEntry.Product2.product_Category__c ;
system.debug('Display'+ Olipc);
if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
{
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;
opprt.add(oppy);
}
}
}
Thanks,
Neetu
The debug it passed and successful on this:
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;
IS there soemthing wrong in this: opprt.add(oppy);