• Shyam Sundar 92
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
Hi, My scenario is i have a QtyOnHand__c Custom field on Product2. Once i give Quantity in OpportunityLineItem (If ProductId matched based on quantity value subtaction should happen this works fine.... Another is LineItem Opportunity stage closed lost the QtyOnHand field in product2 should get added as of the value in quantity. This update action in my trigger is not working ...
Please Have Look.....


trigger RemainingQuant on OpportunityLineItem (after insert,before update) {

  
    set<id> PIds = new set<id>();
    Id oppId;
    String o;
    

    for(OpportunityLineItem opLineItem : [select OpportunityId From OpportunityLineItem  where Id IN :Trigger.new LIMIT 1] ){
     if(opLineItem.OpportunityId != null){
          oppId = opLineItem.OpportunityId;
        }
    }

    if(oppId != null){
         o = [select StageName From Opportunity where Id = :oppId ][0].StageName;
    }

   for(OpportunityLineItem opli : Trigger.new){
       PIds.add(opli.Product2Id);
   } 
   
         
     list<Product2> pList = [select id, name , QtyOnHand__c from Product2 where Id IN : PIds];  
     
   

     for(Product2 p : pList){
      
         for(OpportunityLineItem op : Trigger.new){
             if(p.id == op.product2Id && op.OpportunityId!= Null){ 
             
            
                 if(Trigger.isInsert){
                     if(op.Quantity > p.QtyOnHand__c){
                         op.Quantity.addError(' no availability of stock in Product ');
                     }
                     
               
                    p.QtyOnHand__c-= op.Quantity;
                 


              
}               

                 if(Trigger.isUpdate){
                
                
                 
                 if(o== 'Closed Lost'){
                                     
                     p.QtyOnHand__c +=(Trigger.oldMap.get(op.Id).Quantity).intValue();
                        
                        
                        
                        
                    
                 }}
             }
             
     
   }
    
    update pList;
 
 } 
}
 
How to skip selecting record type in an object ?
When im changing lead status on WF the records which i wrote in this trigger t get created on opportunity creates multiple time as each time i save in lead. Im trying newly in trigger. Kindly have a look.
This is my trigger.

trigger createopportunity1 on Lead (before update) {
 list<opportunity> ops= new list<opportunity>();
  list<opportunity> ops1= new list<opportunity>();
 
for(lead l:trigger.new)

{
  if(l.IsConverted==true &&  l.ConvertedOpportunityId==null && l.Insurance__c== true ){
  
 
  opportunity o = new opportunity();
 
 

  o.name = l.Lastname;
  o.Company__c=l.company;
  o.accountid = l.ConvertedAccountId;
  o.StageName = 'Approach Customer';
  o.CloseDate = system.today();  
  o.prdpick__c='insurance' ;
  o.value__c = 'insurance';
   
  
 
  
  ops.add(o);
  
  
           
} if(l.IsConverted==true &&  l.ConvertedOpportunityId==null && l.investments__c== true ){
  
 
  opportunity o1 = new opportunity();
  o1.name = l.Lastname;
  o1.Company__c=l.company;
  o1.accountid = l.ConvertedAccountId;
  o1.StageName = 'Approach Customer';
  o1.CloseDate = system.today();  
   o1.prdpick__c='investments' ;
   o1.value__c = 'investments';
   
  ops1.add(o1);


}


}
insert ops;
insert ops1;
}