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
klr650klr650 

Trigger Help

Need some help with trigger to write to a custom object when submitted and then once work flow approved update the custom object with approved status. 

I was thinking of changing the following

**********//pick up the Quotes which has just approved.
    for(Integer i=0; i<trigger.new.size();i++){
        if(trigger.new[i].Special_Pricing_Approved__c == true && trigger.old[i].Special_Pricing_Approved__c != true){
            quotes.add(trigger.new[i]);     
        }
    }


with

*********   //pick up the Quotes which has just approved.
    for(Integer i=0; i<trigger.new.size();i++){
        if(trigger.new[i].Special_Pricing_Status__c == 'Submitted' && trigger.old[i].Special_Pricing_Status__c != 'Submitted'){
            quotes.add(trigger.new[i]);     
        }
    }*************


Currently it is set to write when approved is True then it writes to object. I want to write when submitted then update the object status field when approved with Approved.

trigger TrgSpecialPricingApproval on Quote (after update) {
    List<Quote> quotes = new List<Quote>();
    List<Quote> tempQuotes = new List<Quote>();
    List<Approved_Special_Pricing__c> aspList = new List<Approved_Special_Pricing__c>();
    List<Id> oppIds = new List<Id>();
    List<Id> quoteIds = new List<Id>();
    List<Id> PricebookEntryIds = new List<Id>();
    Map<Id,Opportunity> tempMap1 = new Map<Id,Opportunity>();
    Map<Id,List<QuoteLineItem>> tempMap2 = new Map<Id,List<QuoteLineItem>>();
    Map<Id,Id> tempMap3 = new Map<Id,Id>();
    List<Special_Pricing_Line_Item__c> spliList = new List<Special_Pricing_Line_Item__c>();
    List<PricebookEntry> peList = new List<PricebookEntry>();
    
    //pick up the Quotes which has just approved.
    for(Integer i=0; i<trigger.new.size();i++){
        if(trigger.new[i].Special_Pricing_Approved__c == true && trigger.old[i].Special_Pricing_Approved__c != true){
            quotes.add(trigger.new[i]);     
        }
    }
    
    if(quotes.size() != 0){
        for(Quote quote: quotes){
            oppIds.add(quote.OpportunityId);
            quoteIds.add(quote.id);
        }
        List<Opportunity> oppList = [select Id,AccountId,Sales_Region__c  from Opportunity where id = :oppIds]; 
        for(Opportunity opp:oppList){
            tempMap1.put(opp.id,opp);
        }
        
        List<QuoteLineItem> qli = [Select q.UnitPrice, q.ListPrice, q.LineNumber, q.PricebookEntryId, q.Id, q.Discount_Percent__c, q.CurrencyIsoCode, q.QuoteId From QuoteLineItem q where q.QuoteId = :quoteIds];
        
        if(qli.size() != 0){
            for(QuoteLineItem q: qli){
                List<QuoteLineItem> temp = new List<QuoteLineItem>();
                if(tempMap2.get(q.QuoteId) != null){
                    temp = tempMap2.get(q.QuoteId);
                    temp.add(q);        
                    tempMap2.put(q.QuoteId, temp);
                }else{
                    temp.add(q);
                    tempMap2.put(q.QuoteId, temp);
                }
                PricebookEntryIds.add(q.PricebookEntryId);
            }
            
            if(PricebookEntryIds.size() != 0){
            peList = [Select p.Product2Id, p.Id From PricebookEntry p where p.Id = :PricebookEntryIds];
            }
            
            for(PricebookEntry pe: peList){
                tempMap3.put(pe.Id,pe.Product2Id);
            }
        }
            
        //create new Account special pricing boject from Quote.
        for(Quote quote: quotes){
            Approved_Special_Pricing__c asp = new Approved_Special_Pricing__c();
            asp.End_User_Account__c = (tempMap1.get(quote.OpportunityId)).AccountId;
            asp.Active__c = true;
            asp.Partner_Account__c = quote.Partner_Account__c;
            asp.Competitor_Account__c = quote.Competitor_Account__c;
            asp.Reseller_Account__c = quote.Reseller_Account__c;
            asp.Special_Pricing_Expiration_Date__c = quote.Special_Pricing_Expiration_Date__c;
            asp.Printronix_Territory__c = (tempMap1.get(quote.OpportunityId)).Sales_Region__c;
            asp.Name = quote.Special_Pricing_Name__c;       
            asp.Special_Pricing_Status__c = 'Approved';  
            asp.Trade_In_or_Buy_Back__c = quote.Trade_In_or_Buy_Back__c;
            asp.Special_Pricing_Background__c = quote.Special_Pricing_Background__c;
            asp.Special_Configuration_Required__c = quote.Special_Configuration_Required__c;
            asp.Competitor_Price__c = quote.Competitor_Price__c;
            asp.Competitor_Product__c = quote.Competitor_Product__c;   
            aspList.add(asp);       
        }
        insert aspList;
        
        for(Quote quote: quotes){
            for(Approved_Special_Pricing__c asp: aspList){
                if(asp.End_User_Account__c == (tempMap1.get(quote.OpportunityId)).AccountId){
                    
                    //write Special Pricing id submitted Quote. 
                    Quote tempQuote = [select Approved_Special_Pricing__c,id from Quote where id = :quote.id];
                    tempQuote.Approved_Special_Pricing__c = asp.id;
                    tempQuotes.add(tempQuote);
                    
                    //create Special Pricing Line Items from Quote Line Items.
                    if(tempMap2.size() != 0){
                        List<QuoteLineItem> temp = tempMap2.get(quote.id);
                        for(QuoteLineItem qq: temp){
                            Special_Pricing_Line_Item__c spli = new Special_Pricing_Line_Item__c();
                            spli.List_Price__c = qq.ListPrice;
                            spli.Approved_Special_Pricing__c = asp.id;
                            spli.Discount_Percent__c = qq.Discount_Percent__c;
                            spli.CurrencyIsoCode = qq.CurrencyIsoCode;
                            spli.Sales_Price__c = qq.UnitPrice;
                            spli.Product__c = tempMap3.get(qq.PricebookEntryId);
                            spliList.add(spli);
                        }
                    }
                }
            }       
        }
        update tempQuotes;  
        insert spliList;            
    }
}