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
RickoT1031RickoT1031 

Run apex code on load via javascript

Hey all, so my goal here is if they are redirected to this custom visual force page and certian ticket information is not present it redirects them to the edit page to add the apropriate information, however I cannot get the visual force page to see my functions when editing the java (see below)

 

Visual Force Page:

 


<apex:page standardcontroller="Case" extensions="EscalateTicket">

<apex:sectionHeader title="Change Status" subtitle="Please select the new status below"/>

<style>

textarea { width:50%; height:120%; }

</style>

<script>

window.onload = CheckCategories;

function CheckCategories() {

if ({!CheckCats()} == false)

{

alert ("Categorization Information not entered, Skipping to edit");document.location =

"{!GoToEdit}" }

}

</script> <apex:form >

<apex:pageblock >

<apex:pageBlockSection title="Escalation Selection" columns="1">

<apex:selectList value="{!NewAssignment}" multiselect="False" Required="True">

 <apex:selectOptions value="{!Groups}"/>

</apex:selectList>

<apex:inputField value="{!case.Escalation_Comments__c}" Required="True"/>

 </apex:pageBlockSection>

</apex:pageblock><

apex:commandButton action="{!Save}" value="Escalate Ticket"/>

</apex:form>

 

</apex:page>


The error message I get is "Unknown Function CheckCats Check Spelling"

 

The apex methods in question are below

 

Apex Class


public class EscalateTicket {

private final Case c;

public EscalateTicket(ApexPages.StandardController stdController) { this.c = (Case)stdController.getRecord();

}

 

public Boolean CheckCats(){

if ((c.type == '') || (c.Sub_Category__c =='' ) || (c.reason == '')) {return False;} return true;

}

 

public PageReference GoToEdit(){return new PageReference('/' + c.id + '/e?retURL=%2F' + c.id);}

 

}

bob_buzzardbob_buzzard

You don't need the brackets after checkCats.  The fact that you have included them makes the platform think that you are trying to invoke a formula type of function (like isNull() for example) that doesn't exist.

 

 

RickoT1031RickoT1031
But if i take out the parentheses after the function name, apex thinks it is a property and not a function... if I leave the parentheses and take out the curly brackets the javascript thinks im calling a js function... cant win either way lol Maybe you can show me an example, am I missing something?
bob_buzzardbob_buzzard
Do you get an error if apex thinks its a property?  I've used methods this way before without problems.
RickoT1031RickoT1031

Yes,

 

if I do this if (!CheckCats == false) I get a javascript error as shown below


Message: 'CheckCats' is undefined
Line: 118
Char: 2
Code: 0
URI: https://c.cs3.visual.force.com/apex/EscalateTicket?scontrolCaching=1&id=500Q0000001IrJa


 

if I do this if ({!CheckCats} == false) I get the save error below from salesforce


Save error: Unknown property 'CaseStandardController.CheckCats' Salesforce Sandbox/src/pages EscalateTicket.page line 0 1263852967942 3544


if I do this if ({!CheckCats()} == false) I get the save error below from salesforce

Save error: Unknown function CheckCats. Check spelling. Salesforce Sandbox/src/pages EscalateTicket.page line 0 1263853087524 3547


if I do if (!CheckCats() == false) I get the following javascript error (The line and char are the if statement)


Message: Object expected
Line: 118
Char: 2
Code: 0
URI: https://c.cs3.visual.force.com/apex/EscalateTicket?scontrolCaching=1&id=500Q0000001IrJa

Why do I feel like this should be a lot easier than it is being

bob_buzzardbob_buzzard
Taking a step back and re-reading your original post, if you are looking to redirect the user on some conditions that are available to your apex code, you would be better to use the action attribute on page - this specifies an action method on the controller that gets called before the page is rendered out.  You could check the criteria in this method and either return null to leave the user on the current page or redirect them to the correct page.
RickoT1031RickoT1031

Thanks for all your help bob, I actually figured out a work around.. if VisualForce wants to see it as a property, I let it.  I just changed my functions to properties and when it does a getvalue I have it perform the code I wanted it to execute instead of doing it as a function.  Its a bit dirty for my code practices ,but it works so that makes me warm and fuzzy

 

Thanks for your help!

 

~Rick

bob_buzzardbob_buzzard
Glad to hear you got there.  Often getting it to work is hard enough without worrying about the elegance.
RickoT1031RickoT1031
Yah I am slowly learning that with Salesforce you have to go function over form 90% of the time lol