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

 

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Raj,

 

Below is the updated trigger try that.

 

trigger WinningHandle on Opportunity (before update){    
    List<item__c> lstItem = new List<item__c>();
    item__c objitem;
    for(Opportunity i:Trigger.new) {
        if(i.StageName=='Closed Won'){
            objitem = new item__c();
            String name='';
            String quant='0.00';
            String saleVal='0.00';
            Integer ix;
            for(OpportunityLineItem opx :i.OpportunityLineItems) {
                quant=opx.Quantity+'';
                saleVal=opx.UnitPrice+'';
                name=PricebookEntry.Product2.Name+'';             
            }
            objitem.Item_Name__c = name;
            objitem.Quantity__c = integer.valueof(quant);
            objitem.SaleValue__c = decimal.valueOf(saleVal);
            lstItem.add(objitem);
        }                 
    }  
    if(lstItem.size() >0){
        insert lstItem;
    }   
}

 

 

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

Discuss with meDiscuss with me

Please avoid SOQL and DML statements inside For loop.

Raj VaidaRaj Vaida

Please could you elaborate? I am a newbie!

hitesh90hitesh90

Hi Raj,

 

Below is the updated trigger try that.

 

trigger WinningHandle on Opportunity (before update){    
    List<item__c> lstItem = new List<item__c>();
    item__c objitem;
    for(Opportunity i:Trigger.new) {
        if(i.StageName=='Closed Won'){
            objitem = new item__c();
            String name='';
            String quant='0.00';
            String saleVal='0.00';
            Integer ix;
            for(OpportunityLineItem opx :i.OpportunityLineItems) {
                quant=opx.Quantity+'';
                saleVal=opx.UnitPrice+'';
                name=PricebookEntry.Product2.Name+'';             
            }
            objitem.Item_Name__c = name;
            objitem.Quantity__c = integer.valueof(quant);
            objitem.SaleValue__c = decimal.valueOf(saleVal);
            lstItem.add(objitem);
        }                 
    }  
    if(lstItem.size() >0){
        insert lstItem;
    }   
}

 

 

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

This was selected as the best answer