• Raj Vaida
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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));
        }                 
        }       
}

 

 

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));
        }                 
        }       
}