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
sp13sp13 

how to call javascript in an apex class

how can i call script in class?

 

if i have this code:

<script>
    function clearValue() {
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLookup:accountLookup}').value = '';
    document.getElementById('{!$Component.page:frm:pblck:pbSectn:pbSitemLastName:contactLastname}').value = '';
  
    alert();
    return false;
    }
    
    </script>

 

how can i call this in the controller/apex class?

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Try it

<apex:page controller="calljavascript_cls" >

<script>

  function func()

  {

  alert('function calling');

  }

  </script>

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

 

</apex:page>

-------- apex class --------------

public class calljavascript_cls

{

public string callfunc{get;set;}

public calljavascript_cls()

{

    callfunc='<script> func(); </script>';

}

}

All Answers

bob_buzzardbob_buzzard

You can't call JavaScript from the controller, the page is the output of the controller, rather than dynamically interacting with it.  What you can do is set a property in the controller, so that when the page is rendered the javascript executes.  

 

Can you explain a little more about what you are trying to achieve?

sp13sp13

i am trying to clear a lookupfield value when the other field is equal to null


this is the code that i'm calling here(<apex:inputField value="{!leave.Project_1__c}" onclick="{!Approver1}">)

public Contact getApprover1() {
        try {
            if(leave.Leave_Type__c == null || leave.Date_Filed__c == null) {
                 //clearValue();
            } else {
                proj = [select id, name, pse__project_manager__c from pse__proj__c where id =:leave.Project_1__c];
                return [select Id, name from Contact where Id =: proj.pse__project_manager__c Limit 1];
            }
        } catch(Exception e) {
            apexPages.addMessages(e);
        }
        return null;
    }

 

do you know how can i clear the field?

thanks.

bob_buzzardbob_buzzard

Doing this kind of thing in a getter is problematic, as the order of execution isn't guaranteed so the getter for the lookup may execute before the getter that decides to clear it.

 

Does the user make the selection of the other field and then post the form back to the controller?

gbu.varungbu.varun

Try it

<apex:page controller="calljavascript_cls" >

<script>

  function func()

  {

  alert('function calling');

  }

  </script>

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

 

</apex:page>

-------- apex class --------------

public class calljavascript_cls

{

public string callfunc{get;set;}

public calljavascript_cls()

{

    callfunc='<script> func(); </script>';

}

}

This was selected as the best answer