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
pjaenick.ax736pjaenick.ax736 

Possible to call a javascript function from scheduled apex?

Have a working javascript button.  Now I'd like to avoid pushing it :)

So I've utilized the other suggestions I've found and embedded my script in a VF page, but having trouble making the class "schedulable":
global class calljavascript_cls implements Schedulable {

    global string callfunc{get;set;}

    global void execute(SchedulableContext sc) {
        calljavascript_cls();
    }

    @future(callout=true)
    public static void calljavascript_cls() {
        callfunc='<script> func(); </script>';
    }
}

Error: Compile Error: Variable does not exist: callfunc at line 14 column 9

Thanks for any assistance,
Pete
Best Answer chosen by pjaenick.ax736
Raj VakatiRaj Vakati
Try this
 
global class calljavascript_cls implements Schedulable {

    global static string callfunc{get;set;}

    global void execute(SchedulableContext sc) {
        calljavascript_cls();
    }

    @future(callout=true)
    public static void calljavascript_cls() {
        callfunc='<script> func(); </script>';
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
global class calljavascript_cls implements Schedulable {

    global static string callfunc{get;set;}

    global void execute(SchedulableContext sc) {
        calljavascript_cls();
    }

    @future(callout=true)
    public static void calljavascript_cls() {
        callfunc='<script> func(); </script>';
    }
}

 
This was selected as the best answer
pjaenick.ax736pjaenick.ax736
Thanks Raj -  I tried it, but my VF page doesn't accept it as 'static' -
The property String callfunc is referenced by Visualforce Page (Javascript_VFpage) in salesforce.com. Remove the usage and try again. at line 3 column 19
 
<apex:page controller="calljavascript_cls" >

    <script>
        function func() {
            alert('some function');
        }
    </script>

    <apex:outputText value="{!callfunc}" escape="false"></apex:outputText>

</apex:page>
Any other ideas?
 
Raj VakatiRaj Vakati
I dnt see any issue with the code and my API version 44.0 

User-added image
pjaenick.ax736pjaenick.ax736
Thanks Raj - I marked your initial reply as "Best Answer" :)

Here's what interesting - if the VF page is first (verbatim, as above), you get the error I encountered when setting the callfunc global string variable to 'static'.  If you comment out the <outputText> block in the VF page, then change the variable, then restore the <outputText> block in the VF page - all is well :)  Odd behavior.

Thanks again!