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
Varun AnnadataVarun Annadata 

Calling javascript function from apex class when wrong picklist value is selected

My Vf Page:
<apex:page standardcontroller="Program_Member_MVN__c" extensions="ProgramMemberCaseAttachment" showHeader="false" >
<head>
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
        <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
        <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
        <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var contactTable = j$('[id$="accounttable"]').DataTable({
                    
                });
            });
                         
            <!-- var Type = '{!AttType}';
             
            const selectEl = document.getElementById('{!$Component.newPageBlock.Section.DateRange}');-->
             
            function Callmefunc()
               {
                
                   alert('Select a Type');
                    
                }
          
        </script>
            
</head>


            <apex:form id="form">
            
            <apex:pageBlock id="newPageBlock">
            
            <apex:pageBlockSection columns="3" id="Section">
            
            <apex:outputLabel ></apex:outputLabel>
          
            <apex:outputLabel style="font-weight:bold"><p1>Select Input Type (then click save) =></p1>
                        
            <apex:selectList id="DateRange" value="{!AttType}" size="0"  style="background-color:{!(IF(AttType!=null,'red','white'))}">
                            <apex:actionSupport event="onchange" action="{!processSelected1}"/>
                            <apex:selectOption itemValue="None" itemLabel="None"/>
                            <apex:selectOption itemValue="Type1" itemLabel="Type1"/>
                            <apex:selectOption itemValue="Type2" itemLabel="Type2"/>
                            <apex:selectOption itemValue="Type3" itemLabel="Type3"/>
                        </apex:selectList>
                        
                        </apex:outputLabel>

            <apex:outputText value="{!callfunc}" escape="false"></apex:outputText>
            
            </apex:pageBlockSection>
          
            <apex:pageBlockTable id="accounttable" value="{!PMresults}" var="N" columnClasses="display">
                        
            <apex:column headerValue="Save">
            
            <apex:commandLink value="Save" action="{!processSelected}" oncomplete="window.location.href='/apex/ProgramMemberCaseAttachment?id={!Program_Member_MVN__c.id}'; return false">
                        
            <apex:param name="{!N.Attachment_Id__c}" value="{!N.Attachment_Id__c}" assignTo="{!Attid}"/>

            </apex:commandLink>
            
            </apex:column>
            
            <!--<apex:column headerValue="Attachment Name">
            
            <apex:outputField value="{!N.Attachment_Name__c}"/>     
                  
            </apex:column>-->
            
            <apex:column headerValue="Attachment">
            
            <apex:outputField value="{!N.Attachment__c}"/>      
                  
            </apex:column>
            
            <apex:column headerValue="Type">
            
            <apex:outputField value="{!N.Type__c}"/>      
                  
            </apex:column>
            
            <apex:column headerValue="PM Number">
            
            <apex:outputLink value="/{!N.Program_Member_Lookup__c}" target="_blank">{!N.Program_Member_Lookup__r.Name}</apex:outputLink>     
                  
            </apex:column>
            
            <apex:column headerValue="Case Number">
            
            <apex:outputLink value="/{!N.Case_Lookup__c}" target="_blank">{!N.Case_Lookup__r.Casenumber}</apex:outputLink>     
                  
            </apex:column>
            
            <!--<apex:column headerValue="Name">
            
            <apex:outputLink value="/{!N.Id}" target="_blank">{!N.Name}</apex:outputLink>
            
            </apex:column>-->
            
            <apex:column headerValue="Description">
            
            <apex:outputField value="{!N.Attachment_Description__c}"/>    
                  
            </apex:column>
                  
            </apex:pageBlockTable>
      
            </apex:pageBlock>
            
            </apex:form>
</apex:page>
My Apex Class:
/**
*  ProgramMemberCaseAttachment
*  Created By:     Varun 
 *  Created On:     11/07/2017
*  Description:    This class is responsible for displaying Attachment Details
                     Under Each Program Member
*                  
 **/
public with sharing class ProgramMemberCaseAttachment{

    public string callfunc{get;set;}

    public string AttType{get;set;}

    public Id Attid{get;set;}
    
    public List<Attachment_Type__c> PMresults{get;set;}
        
    public Program_Member_MVN__c cs;
    
    public ProgramMemberCaseAttachment(ApexPages.StandardController controller)
    {
    
    //AttType ='';
    
    cs=(Program_Member_MVN__c)controller.getRecord();
        
    List<case> Test = new List<case>([select id from case where Program_Member_MVN__c =: cs.id]);
    
    System.debug('Test:'+Test);
    
    List<String> Id = new List<String>();
    
    System.debug('AttType:'+AttType);
    
    for(case Cs1 : Test){
    
    Id.add(Cs1.id);
    
    }
    
    PMresults = [select Program_Member_Lookup__r.Name,Case_Lookup__r.Casenumber,Attachment__c,Case_Lookup__c,Program_Member_Lookup__c,Attachment_Id__c,Parent_Id__c,Id,Attachment_Name__c,Attachment_Description__c,Name,Type__c from Attachment_Type__c where Program_Member_Lookup__c =: cs.id OR Case_Lookup__c IN: Id];
    
    System.debug('PMresults '+PMresults );
    
    }
    
    public void processSelected1(){
    
    //AttType = System.currentPageReference().getParameters().get('Test');
    
    System.debug('AttType'+AttType);
    
    }
    
    public void processSelected(){
    
    System.debug('AttType 1'+AttType);
    
    List<Attachment_Type__c> PMType = new List<Attachment_Type__c>();
    
    Attachment_Type__c At1 = new Attachment_Type__c ();
    
    At1 = [select Type__c from Attachment_Type__c where Attachment_Id__c =: Attid];
    
    System.debug('At1 '+At1 );
    
    if(AttType == 'None'){
    
    callfunc = '<script> Callmefunc(); </script>';
    
    }
    
    else{
    
    At1.Type__c  = AttType ;
    
    
    PMType.add(At1);
    
    }

          set<Attachment_Type__c> dedupSet = new set<Attachment_Type__c>();
          dedupSet.addAll(PMType);
          List<Attachment_Type__c> dedupList = new List<Attachment_Type__c>();
          dedupList.addAll(dedupSet);
          System.debug('dedupList'+dedupList);
          Update dedupList;
       
    }
    
    }

I am trying to call a javascript function from apex class when a None Picklist vlaue is selected.I am not able to get a alert message on the vf page.Where am i doing wrong? 
 
Deepak Pandey 13Deepak Pandey 13
Hi Varun ,
  call your script 'Callmefunc' on vf page where type is mentioned not in class. 
Varun AnnadataVarun Annadata
I have tried that way it was not working.Thats why i am trying to call from the class.
Deepak Pandey 13Deepak Pandey 13
<script>
function Callmefunc()
{
alert(document.getElementById("newPageBlock.Section.DateRange"));
if(document.getElementById("newPageBlock.Section.DateRange") == 'None' )
{
alert('Select a Type');
}
}
</script>


Onchange of type use this.
Or use Apex:message--
/**
*  ProgramMemberCaseAttachment
*  Created By:     Varun 
 *  Created On:     11/07/2017
*  Description:    This class is responsible for displaying Attachment Details
                     Under Each Program Member
*                  
 **/
public with sharing class ProgramMemberCaseAttachment{

    public string callfunc{get;set;}

    public string AttType{get;set;}

    public Id Attid{get;set;}
    
    public List<Attachment_Type__c> PMresults{get;set;}
        
    public Program_Member_MVN__c cs;
    
    public ProgramMemberCaseAttachment(ApexPages.StandardController controller)
    {
    
    //AttType ='';
    
    cs=(Program_Member_MVN__c)controller.getRecord();
        
    List<case> Test = new List<case>([select id from case where Program_Member_MVN__c =: cs.id]);
    
    System.debug('Test:'+Test);
    
    List<String> Id = new List<String>();
    
    System.debug('AttType:'+AttType);
    
    for(case Cs1 : Test){
    
    Id.add(Cs1.id);
    
    }
    
    PMresults = [select Program_Member_Lookup__r.Name,Case_Lookup__r.Casenumber,Attachment__c,Case_Lookup__c,Program_Member_Lookup__c,Attachment_Id__c,Parent_Id__c,Id,Attachment_Name__c,Attachment_Description__c,Name,Type__c from Attachment_Type__c where Program_Member_Lookup__c =: cs.id OR Case_Lookup__c IN: Id];
    
    System.debug('PMresults '+PMresults );
    
    }
    
    public void processSelected1(){
    
    //AttType = System.currentPageReference().getParameters().get('Test');
    
    System.debug('AttType'+AttType);
    
    }
    
    public void processSelected(){
    
    System.debug('AttType 1'+AttType);
    
    List<Attachment_Type__c> PMType = new List<Attachment_Type__c>();
    
    Attachment_Type__c At1 = new Attachment_Type__c ();
    
    At1 = [select Type__c from Attachment_Type__c where Attachment_Id__c =: Attid];
    
    System.debug('At1 '+At1 );
    
    if(AttType == 'None')
    {
            pagereference p = apexpages.Currentpage();
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'You can not choose none'));
    }
    
    else{
    
    At1.Type__c  = AttType ;
    
    
    PMType.add(At1);
    
    }

          set<Attachment_Type__c> dedupSet = new set<Attachment_Type__c>();
          dedupSet.addAll(PMType);
          List<Attachment_Type__c> dedupList = new List<Attachment_Type__c>();
          dedupList.addAll(dedupSet);
          System.debug('dedupList'+dedupList);
          Update dedupList;
       
    }
    
    }

In page add = 
<apex:page standardcontroller="Program_Member_MVN__c" extensions="ProgramMemberCaseAttachment" showHeader="false" >
<head>
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
        <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
        <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
        <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var contactTable = j$('[id$="accounttable"]').DataTable({
                    
                });
            });
                         
            <!-- var Type = '{!AttType}';
             
            const selectEl = document.getElementById('{!$Component.newPageBlock.Section.DateRange}');-->
             
            function Callmefunc()
               {
                
                   alert('Select a Type');
                    
                }
          
        </script>
            
</head>


            <apex:form id="form">
            
            <apex:pageBlock id="newPageBlock">
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            
            <apex:pageBlockSection columns="3" id="Section">
            
            <apex:outputLabel ></apex:outputLabel>
          
            <apex:outputLabel style="font-weight:bold"><p1>Select Input Type (then click save) =></p1>
                        
            <apex:selectList id="DateRange" value="{!AttType}" size="0"  style="background-color:{!(IF(AttType!=null,'red','white'))}">
                            <apex:actionSupport event="onchange" action="{!processSelected1}" rerender="showmsg"/>
                            <apex:selectOption itemValue="None" itemLabel="None"/>
                            <apex:selectOption itemValue="Type1" itemLabel="Type1"/>
                            <apex:selectOption itemValue="Type2" itemLabel="Type2"/>
                            <apex:selectOption itemValue="Type3" itemLabel="Type3"/>
                        </apex:selectList>
                        
                        </apex:outputLabel>

            <apex:outputText value="{!callfunc}" escape="false"></apex:outputText>
            
            </apex:pageBlockSection>
          
            <apex:pageBlockTable id="accounttable" value="{!PMresults}" var="N" columnClasses="display">
                        
            <apex:column headerValue="Save">
            
            <apex:commandLink value="Save" action="{!processSelected}" oncomplete="window.location.href='/apex/ProgramMemberCaseAttachment?id={!Program_Member_MVN__c.id}'; return false">
                        
            <apex:param name="{!N.Attachment_Id__c}" value="{!N.Attachment_Id__c}" assignTo="{!Attid}"/>

            </apex:commandLink>
            
            </apex:column>
            
            <!--<apex:column headerValue="Attachment Name">
            
            <apex:outputField value="{!N.Attachment_Name__c}"/>     
                  
            </apex:column>-->
            
            <apex:column headerValue="Attachment">
            
            <apex:outputField value="{!N.Attachment__c}"/>      
                  
            </apex:column>
            
            <apex:column headerValue="Type">
            
            <apex:outputField value="{!N.Type__c}"/>      
                  
            </apex:column>
            
            <apex:column headerValue="PM Number">
            
            <apex:outputLink value="/{!N.Program_Member_Lookup__c}" target="_blank">{!N.Program_Member_Lookup__r.Name}</apex:outputLink>     
                  
            </apex:column>
            
            <apex:column headerValue="Case Number">
            
            <apex:outputLink value="/{!N.Case_Lookup__c}" target="_blank">{!N.Case_Lookup__r.Casenumber}</apex:outputLink>     
                  
            </apex:column>
            
            <!--<apex:column headerValue="Name">
            
            <apex:outputLink value="/{!N.Id}" target="_blank">{!N.Name}</apex:outputLink>
            
            </apex:column>-->
            
            <apex:column headerValue="Description">
            
            <apex:outputField value="{!N.Attachment_Description__c}"/>    
                  
            </apex:column>
                  
            </apex:pageBlockTable>
      
            </apex:pageBlock>
            
            </apex:form>
</apex:page>

try it.
Varun AnnadataVarun Annadata
Thank you I tried both still it is not working.The Vf page is a inline vf page in a record page.Does it have any effect on the functionlaity?