You need to sign in to do that
Don't have an account?
Raj Vaida
Issue with Trigger.
I have written following Trigger on Opportunities. My idea is "Trigger has to insert a new record to 'Item' custom object' when the Stage is changed to 'Closed Won'".
No error is reporting but nothing is being inserted.
trigger WinningHandle on Opportunity (before update){ Opportunity[] op=Trigger.new; for(Opportunity i:op) { if(i.StageName=='Closed Won'){ String name=''; String quant='0.00'; String saleVal='0.00'; Integer ix; List<Id> oppIds = new List<Id>(); for(Opportunity o : op){ if(i.id==o.Id){ oppIds.add(o.Id); break; } } List<OpportunityLineItem> oppProds = [SELECT Id, PricebookEntry.Product2.Name, Quantity,UnitPrice FROM OpportunityLineItem WHERE OpportunityId IN :oppIds]; for(OpportunityLineItem opx :oppProds) { quant=opx.Quantity+''; saleVal=opx.UnitPrice+''; name=PricebookEntry.Product2.Name+''; } insert new item__c(Item_Name__c = name, Quantity__c = integer.valueof(quant), SaleValue__c = decimal.valueOf(saleVal)); } } }
Hi Raj,
Below is the updated trigger try that.
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
All Answers
Please avoid SOQL and DML statements inside For loop.
Please could you elaborate? I am a newbie!
Hi Raj,
Below is the updated trigger try that.
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator