Thursday, 9 August 2012

Display Trigger names of selected objects

Hi,
I am trying to explain to show the trigger names for selected object from picklist on visualforce page.

For this i used Dynamic apex and Apextrigger object.

Controller:
------------
public  class Dynamicobjectstriggerspageclass {
    public boolean ren { get; set; }
    public List<apextrigger> Triggerss { get; set; }
    public List<SelectOption> items { get; set; }
    public String selectedObj { get; set; }
    Public List<SelectOption> trignames{get;set;}
    public string selectedtrigname{get;set;}
    public Dynamicobjectstriggerspageclass (){
        Triggerss=new List<apextrigger>();
        trignames=new List<selectoption>();
        List<Schema.sObjecttype> lst= Schema.getGlobalDescribe().values();
        items= new List<SelectOption>();
        items.add(new SelectOption('','--None--'));
        for(Schema.sobjecttype s:lst){
        //if(String.valueOf(s).contains('__c'))
            items.add(new SelectOption(string.valueOf(s),String.valueOf(s)));
        }
        items.sort();
    }  
    public void fun1(){
       
    }
    public List<selectoption> getapextriggers(){
    
        if(selectedObj!=null && selectedObj!=''){
               Triggerss=new List<apextrigger>();
               trignames=new List<selectoption>();     
        trignames.add(new SelectOption('','--None--'));
        for(ApexTrigger aptrig:[select id,name from apextrigger where TableEnumOrId=:selectedObj]){
             trignames.add(new SelectOption(aptrig.id,aptrig.name));
        }
        }
        return trignames;      
    }

}
--------------------------------------
Visualforce Page
---------------------------------------
<apex:page controller="Dynamicobjectstriggerspageclass">
<apex:form >
<apex:actionfunction name="fun" action="{!fun1}" rerender="trig"/>
  <apex:pageblock >
      <apex:outputText value="Object Name"/> &nbsp;
        <apex:SelectList value="{!selectedObj}" size="1" onchange="fun()">
          <apex:selectOptions value="{!items}"/>
           </apex:SelectList>
         <apex:outputpanel >
                <apex:outputText value="Trigger Name:"/>&nbsp;
                <apex:selectList value="{!selectedtrigname}" id="trig" multiselect="false" size="1">
                                <apex:selectoptions value="{!apextriggers}"/>
                </apex:selectList>
         </apex:outputpanel>
   </apex:pageblock>
  </apex:form>
</apex:page>

No comments:

Post a Comment