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
shakila Gshakila G 

Trigger Not Working not Updateing?

Hi All,

I wrote a trigger to update Incentive slab in Opportunities.But its, not updating.

kindly Let me know where am doing mistake:

trigger Incentive_Slot on Payment__c (after insert, after update) {
    
    Id OppId;
    ID ProjectID;
    Id IncId;
    Date pDate;
    Opportunity opp = new Opportunity();
     
    
    list<IncentiveLineItem__c> lstIli = new list<IncentiveLineItem__c>();
  
    
    for(Payment__c pRow: Trigger.new) {
        if( (prow.Payment_Type__c == 'Full Pay') || (prow.Payment_Type__c == 'Final Pay')) {
            OppId = prow.Opportunity__c;
             ProjectID = prow.Project__c;
            pDate = prow.Date__c;
        } 
    }
    
    if(OppId!=NUll || OppId!='')
    {    
  
    opp = [select id, Discount__c,StageName, Work_Order_Date__c, Date__c, Pricebook2Id, Pricebook_Name__c, 
            IncentiveSlab__c, Last_Payment_Received_Date__c, Incentive_For__c, Incentive__c,
            NewIncentivePercentage__c, IncentiveLast_Payment_Date__c, Final_Full_Payment_Date__c, 
            Outstanding_Amount__c, Special_Incentive_Account__c from Opportunity where id =: oppId]; 
    
    
    if (opp.StageName=='Closed Won' && opp.ID!=Null) {
        Incentive__c inc = new Incentive__c();        
            
            inc = [select Id from Incentive__c where Price_Book__c=: opp.Pricebook_Name__c and Active__c = true ];

                  
                opp.IncentiveSlab__c = inc.Id;
                Update Opp;   
        
   
       
                }
                
    
    }
    
}
 
Best Answer chosen by shakila G
Maharajan CMaharajan C
Hi Shakila,

Please write the bulkified trigger to avoid the unusual error in future:

I have assumed the Pricebook_Name__c is Lookup in Opporunity , Price_Book__c is Lookup in Incentive__c obj to Pricebook.

Please try the below trigger:

trigger Incentive_Slot on Payment__c (after insert, after update) {

    Set<Id> OppId = new Set<Id>();
    List<Opportunity> opplist = new List<Opportunity>();
    set<Id> PBname = new set<Id>();
    List<Incentive__c> inc = new List<Incentive__c>(); 
    Map<Id,Id>     incMap = new Map<id,id>();
    List<Opportunity> OppListtoUpdate = new List<Opportunity>();

    for(Payment__c pRow: Trigger.new) {
        if( (prow.Payment_Type__c == 'Full Pay') || (prow.Payment_Type__c == 'Final Pay')) {
            OppId.add(prow.Opportunity__c);
        } 
    }
    
    if(OppId.size() > 0)
    {
        opplist = [select id, Discount__c,StageName, Work_Order_Date__c, Date__c, Pricebook2Id, Pricebook_Name__c, 
            IncentiveSlab__c, Last_Payment_Received_Date__c, Incentive_For__c, Incentive__c,
            NewIncentivePercentage__c, IncentiveLast_Payment_Date__c, Final_Full_Payment_Date__c, 
            Outstanding_Amount__c, Special_Incentive_Account__c from Opportunity where id IN : OppId]; 
    }
    
    If(opplist.size() > 0)
    {
        for(Opportunity o : opplist)
        {
            if (o.StageName=='Closed Won' && o.Pricebook_Name__c != null) {
                PBname.add(Pricebook_Name__c);
            }

        }
    }
    
    if(PBname.size() > 0)
    {
        inc = [select Id,Price_Book__c from Incentive__c where Price_Book__c IN : PBname and Active__c = true ];
        for(Incentive__c i: inc)
        {
           if(i.Price_Book__c != null)
            incMap.put(i.Price_Book__c,i.Id);
        }
    }
    
    for(Opportunity op : opplist)
    {   
        if (op.StageName=='Closed Won' && op.Pricebook_Name__c != null && incMap.containskey(op.Pricebook_Name__c)) {
        Opportunity oppn = new Opportunity(id = op.Id, Pricebook_Name__c = op.Pricebook_Name__c);
        oppn.IncentiveSlab__c = incMap.get(oppn.Pricebook_Name__c);
        OppListtoUpdate.add(oppn);
        
        }
    }
    
    if(OppListtoUpdate.size() > 0)
    Update OppListtoUpdate;
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!



Thanks,
Raj

All Answers

Maharajan CMaharajan C
Hi Shakila,

Please write the bulkified trigger to avoid the unusual error in future:

I have assumed the Pricebook_Name__c is Lookup in Opporunity , Price_Book__c is Lookup in Incentive__c obj to Pricebook.

Please try the below trigger:

trigger Incentive_Slot on Payment__c (after insert, after update) {

    Set<Id> OppId = new Set<Id>();
    List<Opportunity> opplist = new List<Opportunity>();
    set<Id> PBname = new set<Id>();
    List<Incentive__c> inc = new List<Incentive__c>(); 
    Map<Id,Id>     incMap = new Map<id,id>();
    List<Opportunity> OppListtoUpdate = new List<Opportunity>();

    for(Payment__c pRow: Trigger.new) {
        if( (prow.Payment_Type__c == 'Full Pay') || (prow.Payment_Type__c == 'Final Pay')) {
            OppId.add(prow.Opportunity__c);
        } 
    }
    
    if(OppId.size() > 0)
    {
        opplist = [select id, Discount__c,StageName, Work_Order_Date__c, Date__c, Pricebook2Id, Pricebook_Name__c, 
            IncentiveSlab__c, Last_Payment_Received_Date__c, Incentive_For__c, Incentive__c,
            NewIncentivePercentage__c, IncentiveLast_Payment_Date__c, Final_Full_Payment_Date__c, 
            Outstanding_Amount__c, Special_Incentive_Account__c from Opportunity where id IN : OppId]; 
    }
    
    If(opplist.size() > 0)
    {
        for(Opportunity o : opplist)
        {
            if (o.StageName=='Closed Won' && o.Pricebook_Name__c != null) {
                PBname.add(Pricebook_Name__c);
            }

        }
    }
    
    if(PBname.size() > 0)
    {
        inc = [select Id,Price_Book__c from Incentive__c where Price_Book__c IN : PBname and Active__c = true ];
        for(Incentive__c i: inc)
        {
           if(i.Price_Book__c != null)
            incMap.put(i.Price_Book__c,i.Id);
        }
    }
    
    for(Opportunity op : opplist)
    {   
        if (op.StageName=='Closed Won' && op.Pricebook_Name__c != null && incMap.containskey(op.Pricebook_Name__c)) {
        Opportunity oppn = new Opportunity(id = op.Id, Pricebook_Name__c = op.Pricebook_Name__c);
        oppn.IncentiveSlab__c = incMap.get(oppn.Pricebook_Name__c);
        OppListtoUpdate.add(oppn);
        
        }
    }
    
    if(OppListtoUpdate.size() > 0)
    Update OppListtoUpdate;
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!



Thanks,
Raj
This was selected as the best answer
shakila Gshakila G
Hi raj,

Thanks, for your effort.

I don't want to update pricebook ID into Incentive Slab.

  I want to get Incentive ID into oppn.IncentiveSlab__c
 
Maharajan CMaharajan C
Hi Shakila,

Here we are getting the Incentive id only not the PricebookId.

        oppn.IncentiveSlab__c = incMap.get(oppn.Pricebook_Name__c);  // it will return the Incentive Id. In the incMap map it will the fetch the value for key incMap so the value is Incentive ID.


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!



Thanks,
Raj
shakila Gshakila G
Hi raj,

Thanks for your effort

Its working fine