JS alert not firing on Detail Page button click via VF page - Answers - Salesforce Trailblazer Community
Ask Search:
Prathamesh VerlekarPrathamesh Verlekar 

JS alert not firing on Detail Page button click via VF page

I’ll try to keep this simple and precise.

I created a Detail Page button (called Push Lines) on the Quote object in CPQ with the following properties:

Display type: Detail Page Button
Content Source: VisualForce Page

This VF page in turns calls an autolaunched flow using the <flow:interview> property.

But I want this flow to run only when a checkbox called ‘Primary’ is selected on the quote.
If the quote is not Primary, I wish to throw an alert and not execute the flow at all.

Vf page pseudo code:
 
<apex:page standardController="SBQQ__Quote__c">
    
    <script>
        window.onload = function(){
            if(!{!SBQQ__Quote__c.SBQQ__Primary__c}){
                alert("Cannot push lines - quote is not marked as Primary");
            }
            else{
                <flow:interview name="Quote_Lines_Push_Workflow" finishLocation="/{!SBQQ__Quote__c.Id}">
                    <apex:param name="quoteId" value="{!SBQQ__Quote__c.Id}"/>
                    …other params input to flow
…
                </flow:interview>            
            }
        }
    </script>
</apex:page>

For some reason, it recognizes the window.onload call and executes the flow when the quote is primary.
But when the flag is not set, it does not pop an alert. Very frustrating!!

Options I tried/not feasible:
  1. Changing the content type to onclick JavaScript. This makes my button disappear from the Lightning layout. I don’t think detail button with JS is supported in Lightning.
  2. Writing a controller - if possible I would like to avoid writing a custom controller.
What am I doing wrong? Please advise.

~Prath
Best Answer chosen by Prathamesh Verlekar
Narender SinghNarender Singh
To display the message of alert, you can just use the Screen element and put the Display Text component in it to display the text.

You should be directly calling the flow from an object quick action. Instead of going through so much hassle of creating a visualforce page and then launching the flow from the vf page.

All Answers

Narender SinghNarender Singh
Quick question: Why so much of hassle when you can handle all this within a flow itself?
Just simply check the value of Primary field within the flow using decision element and set up the flow accordingly.
Prathamesh VerlekarPrathamesh Verlekar
I pass the Primary flag as an input to the flow.
Also used a decision box and handled it in the flow.
The page reloads and returns back to the quote...but there is no visual feedback.

Requirements are to show an alert/toast message on button press when the quote is not primary. 
Narender SinghNarender Singh
To display the message of alert, you can just use the Screen element and put the Display Text component in it to display the text.

You should be directly calling the flow from an object quick action. Instead of going through so much hassle of creating a visualforce page and then launching the flow from the vf page.
This was selected as the best answer
Prathamesh VerlekarPrathamesh Verlekar
I created an autolaunced flow, so I don't have a screen element available to add.
The initial thought was to proactively capture this Primary quote case using JS and not call the flow all all if the validation fails. Hence I created the autolaunced flow.

And autolaunced flows are not available for selection from an Obejct Quick action.

I guess i'll give creating a screen flow a shot and doing a validation in there.

Thanks!
Narender SinghNarender Singh
Yes, you should re-create the flow as a screen flow. :)
Narender SinghNarender Singh
If it all works out for ya, kindly consider closing this thread. 
Thanks! :)
Prathamesh VerlekarPrathamesh Verlekar
I managed to do this without creating a screen flow. 
What I did was:

1. Create the Detail Page button which calls the VF Page.
2. Rewrote the logic on the VF Page with some REQUIRE statements for the JS. I did not use the <flow:interview> element. Instead used the window.open() and passed it the url of the flow with the input paremeters I wanted to send.
3. The flow remained unchanged.

Since the JS check happens upfront, I do not call the flow at all if the primary flag validation fails. And also added a retURL in the url to make it come back to the same page from where the button was pressed.

Thanks for the inputs!
Narender SinghNarender Singh
That seems a bit too much for a very simple solution. The solution could have been so much easier and neat and easily maintainable but that's just my opinion. :)