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
Kyle GolikKyle Golik 

Apex Trigger not executing when Start Sync button is clicked

Background:
Our company works in print media and when we sell ads for our magazine, our reps fill the fulfillment information out at the Quote Line Item object.

Our accounting software can only read from the Opportunity Product level.

I have created a trigger that achieves that with one catch, it doesn't recognize when a rep needs to sync the primary quote with the Opportunity.

The button is a standard Salesforce button, meaning it comes with Salesforce and I don't see how to customize it. (Start and Stop Sync button)
If I were to update a record at the Quote Line Item object after syncing the Quote, the trigger works as expected.

Question:
How to execute a trigger on click of a Standard Button?

 

trigger UpdateOpportunityProductMonthsToFulfill on QuoteLineItem (after insert, after update) {
    String monthsToFulfill = '';
    map<String, String> updateMap = new map<String, String>();
    
    // This is where the text for the Months_To_Fulfill__c field is built
    for(QuoteLineItem qli : Trigger.new){
        // To make a spelled-out list of months & years instead of the older "start through end"
        
        List<String> yearOneMonths = qli.Months_Served_Y1__c.replaceAll('None(;)?', '').split(';');
        for(String month : yearOneMonths){
            monthsToFulfill += ( monthsToFulfill.length()==0 ? '' : '; ' ) + month + ' ' + qli.Year_Served_First__c;
        }
        if(!(qli.Year_Served_Second__c == null || qli.Year_Served_Second__c.equals('None'))){
            List<String> yearTwoMonths = qli.Months_Served_Y2__c.replaceAll('None(;)?', '').split(';');
            for(String month : yearTwoMonths){
                monthsToFulfill += '; ' + month + ' ' + qli.Year_Served_Second__c;
            }
        }
        
        updateMap.put(qli.QuoteId, monthsToFulfill);
    }
  
    for(String quoteID : updateMap.keySet()){
        // This is the calculated value we saved from looping through the Trigger.new Collection
        monthsToFulfill = updateMap.get(quoteID);
        
        // Get the OpportunityLineItems associated with this QuoteId
        List<OpportunityLineItem> oliList = [Select Id, OpportunityId, Months_To_Fulfill__c 
                                             From OpportunityLineItem Where OpportunityId In (Select OpportunityId 
                                                                                              From Quote Where Id = :quoteID )];
        
        List<OpportunityLineItem> olisToUpdate = new List<OpportunityLineItem>();
        
        if(oliList.size() > 0){
            for (OpportunityLineItem oli : oliList){
                // Populate this olisToUpdate list because we may 
                // want to get smarter about what's actually updated 
                // in the future.  For now, everything is updated.
                oli.Months_To_Fulfill__c = monthsToFulfill;
                olisToUpdate.add(oli);
            }
            
            // Batch update the OpportunityLineItems we identified earlier
            if(olisToUpdate.size() > 0){
                update olisToUpdate;
            }
        }
    }     
}
 

 

Best Answer chosen by Kyle Golik
Gaurish Gopal GoelGaurish Gopal Goel
Suppose you want to show this VF page on the click of a button on Account object.
1. Create a VF page with the standardController="Account"
2. Create/Edit the button and select this VF page. This VF page will be only available for the button if you have used the same standardController.

Now you are good to go, the button click will open this VF page. You can refer this article - https://salesforce.stackexchange.com/questions/12832/how-to-call-a-visualforce-page-from-a-custom-button 

If this answer solves your problem then mark it as the solution to help others. Thanks.

All Answers

Gaurish Gopal GoelGaurish Gopal Goel
Hi Kyle,

The trigger only works when the event is fired. In this case it will only work when a QuoteLineItem is inserted or updated.

To make this logic work on the click of a button, you will have to create a VF Page and an Apex controller. That VF page will be called on a click of a button. Thanks.
Kyle GolikKyle Golik

Good morning @Gaurish, 

On the VF Page, what information would need to be included to make this work? 

Gaurish Gopal GoelGaurish Gopal Goel
Suppose you want to show this VF page on the click of a button on Account object.
1. Create a VF page with the standardController="Account"
2. Create/Edit the button and select this VF page. This VF page will be only available for the button if you have used the same standardController.

Now you are good to go, the button click will open this VF page. You can refer this article - https://salesforce.stackexchange.com/questions/12832/how-to-call-a-visualforce-page-from-a-custom-button 

If this answer solves your problem then mark it as the solution to help others. Thanks.
This was selected as the best answer
Kyle GolikKyle Golik

@Gaurish

Thank you so much! I tried this morning after returning from holiday and it worked