Display apex class code on click of Show button - salesforce

I have a dynamic picklist field that contains all the apex classes name that are in my org. There is a "Show" button on the page as well. Now if a user selects a value from this picklist and clicks on Show button, the apex code of that class should get displayed below.
Pls suggest how can I implement it in my VF page.
Thanks!
<apex:form >
<apex:selectList value="{!selectedClass}" size="5">
<apex:selectOptions value="{!ClassList}" ></apex:selectOptions>
</apex:selectList>
<apex:pageBlock >
<apex:commandButton action="{!show}" value="Show" id="Button"/>
<apex:pageBlockSection title="My Current Class">

You could query the body field of the ApexClass object for what you're looking for:
public class SomeController {
private List<ApexClass> allApexClasses;
public String selectedClass {public get; public set;}
public String apexCodeOutput {public get; private set;}
public SomeController() {
// only select classes that aren't part of a managed package, since you won't be able to view the body
allApexClasses = [select id, name, body from ApexClass where lengthwithoutcomments <> -1 order by name asc];
}
public List<SelectOption> getClassList() {
List<SelectOption> opts = new List<SelectOption> opts;
for ( ApexClass ac : allApexClasses )
opts.add(new SelectOption(ac.Id, ac.Name));
return opts;
}
public PageReference show() {
if ( selectedClass != null ) {
Id classId = (Id) selectedClass;
for ( ApexClass ac : allApexClasses ) {
if ( classId == ac.Id ) {
apexCodeOutput = ac.body;
break;
}
}
}
return null;
}
}
And then in your VF page, just rerender the output code when clicking the button. You'll want to use a <pre> tag around the code to preserve spacing so the code is readable.
<apex:form>
<apex:selectList value="{!selectedClass}" size="5">
<apex:selectOptions value="{!ClassList}" ></apex:selectOptions>
</apex:selectList>
<apex:pageBlock >
<apex:commandButton action="{!show}" value="Show" rerender="apexoutput" id="Button"/>
<apex:pageBlockSection title="My Current Class">
<apex:outputPanel id="apexoutput">
<pre>{!apexcodeoutput}</pre>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

Related

Need to update Roles(Custom FIeld) of the Employee(Custom) those are Checked

Roles are assigned to each Employee and each employee has a checkbox. Whenever checkbox is checked and Role selected from Dropdown(Roles from Picklist), New Role Should be assigned to Employee after clicking on Update Button.
How do i get it.. I've assigned ID to checkbox and trying like if Checkbox==true then perform certain condition but it is not working..Please guide me
VF Page
<apex:page extensions="EmployeeSelectClassController13" standardController="Employee__c" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Employees" collapsible="false">
<apex:pageBlockTable value="{!wrapEmployeeList}" var="empWrap" id="table" title="All Employees">
<apex:column >
<apex:inputCheckbox value="{!empWrap.selected}" id="inputCheckbox"/>
</apex:column>
<apex:column value="{!empWrap.emp.Name}" id="inputName" />
<apex:column value="{!empWrap.emp.Role__c}" id="inputRole" />
</apex:pageBlockTable>
<apex:pageBlockSection id="InfoId" columns="1" >
<apex:selectList size="1" value="{!SelectedValue}">
<apex:selectOptions value="{!statusOptions}"/>
</apex:selectList>
</apex:pageBlockSection>
<apex:commandButton value="Update" id="inputButton" action="{!updateRole}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Controller
public class EmployeeSelectClassController13 {
public List<wrapEmployee> wrapEmployeeList {get; set;}
public List<Employee__c> empRecord {get;set;}
public string selectedValue { get;set; }
public List<SelectOption> statusOptions { get;set; }//TO get Roles from Employee
public EmployeeSelectClassController13(ApexPages.StandardController controller){
if(wrapEmployeeList == null){
wrapEmployeeList = new List<wrapEmployee>();
for(Employee__c empList:[Select Name,Role__c from Employee__c]){
wrapEmployeeList.add(new wrapEmployee(empList));
autorun();
}
}
}
public class wrapEmployee{
public Employee__c emp {get; set;}
public Boolean selected {get; set;}
public wrapEmployee(Employee__c e) {
emp = e;
selected = false;
}
}
//To get all Roles(PickList) from Employee Object
public void autoRun()
{
Schema.DescribeFieldResult statusFieldDescription = Employee__c.Role__c.getDescribe();
statusOptions = new list<SelectOption>();
for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
{
statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
}
}
public void updateRole()
{
}
}
Something like this? You don't need IDs in the Visualforce unless you do some JavaScript magic. This can be done completely in Apex.
public void updateRole(){
List<Employee__c> toUpdate = new List<Employee__c>();
for(wrapEmployee wrap : wrapEmployeeList){
if(wrap.selected){
wrap.emp.Role__c = SelectedValue;
// wrap.selected = false; // you could choose to untick checkboxes now?
// or after successful update loop through them one more time and then untick
toUpdate.add(wrap.emp);
}
}
update toUpdate;
}

Page is not re-rendering after adding select list code

I am new In salesforce, currently learning a visualforce elements.
I am having a problem in my code that when I insert a SelectList code in my visualForce page then page re-rendering stops working.
Without SelectList page re-rendering works fine.
I have provided a code snippets below. After adding SelectList save method from controller not getting called also contact i am trying to insert is not getting inserted.
Any help is appreciated.
This is my visualforce page.
<apex:page controller="ContactController">
<apex:form >
<apex:pageBlock>
<apex:pageBlockSection >
<apex:inputField value="{!Contact.AccountId}" label="Account Name:"/>
<apex:inputField value="{!Contact.LastName}" label="LastName"/>
<apex:inputField value="{!Contact.phone}" label="Phone"/>
<apex:inputField value="{!Contact.Department}"/>
<apex:inputField value="{!Contact.Designation__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="id3">
<apex:pageBlockSectionItem >
<apex:actionRegion >
<apex:selectList value="{!Contact}" multiselect="true" id="slist1" style="overflow scroll; height : 100px;">
<apex:actionSupport event="onchange" rerender="id1" />
<apex:selectOptions value="{!ContactFields}"></apex:selectOptions>
</apex:selectList><p/>
</apex:actionRegion>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection id="id1">
<apex:pageBlockTable value="{!contactList}" var="c" >
<apex:column headerValue="Last Name">
<apex:outputField value="{!c.Lastname}"/>
</apex:column>
<!-- <apex:column headerValue="Account">
<apex:outputField value="{!c.Account.Name}"/>
</apex:column> -->
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" reRender="id1"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
This is my controller.
public class ContactController {
public Account account { get; private set; }
public Contact contact { get; set; }
public List<Contact> contactList{get {return ([SELECT LastName,Account.Name FROM Contact where Account.Id = :ApexPages.currentPage().getParameters().get('id') order by createdDate DESC limit 5]);} set;}
public Id id;
Map<String, Schema.SobjectField> Contactfields{ get; set;}
List<SelectOption> lstContactFields{get;set;}
public ContactController() {
contact=new Contact();
id = ApexPages.currentPage().getParameters().get('id');
contact.AccountId=id;
Contactfields = Schema.SobjectType.Contact.fields.getMap();
}
public List<SelectOption> getContactFields(){
if(lstContactFields==null){
lstContactFields=new List<SelectOption>();
}
for(Schema.SObjectField s:Contactfields.values()){
Schema.DescribeFieldResult fieldResult = s.getDescribe();
lstContactFields.add(new SelectOption(String.valueof(fieldResult.getName()),String.valueof(fieldResult.getLabel())));
// lstContactFields.add(Contactfields.get(s).getDescribe().getLabel());
//lstContactFields.add(String.valueOf(s));
}
return lstContactFields;
}
public PageReference save() {
try {
System.debug('save method');
upsert contact;
contact.Id=null;
return null;
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
// After successful Save, navigate to the default view page
// PageReference pg = new PageReference(System.currentPageReference().getURL());
// pg.setRedirect(true);
// return pg;
}
}
Your value bindings on these components aren't set up correctly.
<apex:selectList value="{!Contact}" multiselect="true" id="slist1" style="overflow scroll; height : 100px;">
An <apex:selectList> needs to be bound to a String or a List<String> (only for multiselect=true). Here, you've bound it to an sObject variable.
The fact that you've named your sObject variable the same as its class, Contact, is highly likely to cause further problems. Apex isn't case-sensitive, so contact and Contact are the same identifier. In general, you should not reuse reserved words or system class names as variable names, which you do again with id.
<apex:selectOptions value="{!ContactFields}"></apex:selectOptions>
<apex:selectOptions> has to have its value bound to a List<SelectOptions>. You've declared a property by that name with the wrong type:
Map<String, Schema.SobjectField> Contactfields{ get; set;}
while also implementing a getter method with the right type:
public List<SelectOption> getContactFields(){
Again, this is likely to produce unintuitive behavior, even if it does compile and render.

Salesforce apex:commandButton does not execute the action

So I have this in the page:
< apex:commandButton id="songdbabc" value="Synch from song db" action="{!sendingSign}" />
and this in the controller:
public PageReference sendingSign(){
System.debug(' message ');
return null;
}
But everytime I press the button there isn`t any message in the "Log" I mean the action is not executed , any idea?
Do you have another variable or function with same name? I mean public String sendingSign {get; set;} or maybe public String getSendingSign()? These would "shadow" your action function. Do you have more than 1 controller extension in <apex:page> tag? they'd be checked for matching function names from right to left (so if you have few classes maybe it's calling the match from a class you're not looking at)
Open up your browser's JS console / network panel, see if there's any traffic recorded at all when you click the button...
Try changing your sendingSign method to :
public void sendingSign(){
System.debug(' message ');
}
And recheck your log.
EDIT: Fixed! Removed "Static" variables from my extension; all bad behavior cleared right up.
Original:
I had to build a stackoverflow account just so I could vent a little here. I'm facing the same issue, and found that it will only skip my action if I include a picklist on the same page. I've never had this issue.
this page runs the action, but doesn't include the picklist I want:
<apex:page standardController="Checklist_Template_Item__c" recordSetVar="Checklist_Template_Items__r" extensions="ChecklistTemplateItemMassApply">
<apex:form >
<apex:pageBlock >
<apex:commandButton action="{!myCustomAction}" value="Append/Remove" />
<apex:commandButton action="{!cancel}" value="Cancel"/>
<apex:variable value="{!appendValue}" var="appendValue"/>
<apex:variable value="{!selection}" var="selection"/>
<apex:pageBlockSection >
Include your selected items for select machine models.
<apex:inputText value="{!appendValue}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
this page completely skips the action, but includes my picklist:
<apex:page standardController="Checklist_Template_Item__c" recordSetVar="Checklist_Template_Items__r" extensions="ChecklistTemplateItemMassApply">
<apex:form >
<apex:pageBlock >
<apex:commandButton action="{!myCustomAction}" value="Append/Remove" />
<apex:commandButton action="{!cancel}" value="Cancel"/>
<apex:variable value="{!appendValue}" var="appendValue"/>
<apex:variable value="{!selection}" var="selection"/>
<apex:pageBlockSection >
Include your selected items on certain machine models.
<apex:inputText value="{!appendValue}"/>
<apex:pageBlockSectionItem >
<apex:outputText value="Why tho"/>
<apex:selectList size="1" value="{!selection}" required="true">
<apex:selectOptions value="{!applyRemove}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
having either a page constructor or not, action doesn't run when the picklist is included:
extension v1:
public without sharing class ChecklistTemplateItemMassApply{
public static String appendValue{get;set;}
public static String selection{get;set;}
public static List<SelectOption> applyRemove{get;set;}
ApexPages.StandardSetController setCon;
public ChecklistTemplateItemMassApply(ApexPages.StandardSetController myController){
setCon = myController;
List<String> addList = new List<String>{'Applies_To__c'};
setCon.addFields(addList);
system.debug('addList = '+addList);
applyRemove = new List<SelectOption>();
applyRemove.add(new SelectOption('','--None--'));
applyRemove.add(new SelectOption('Include','Include'));
applyRemove.add(new SelectOption('Remove','Remove'));
}
public ApexPages.pageReference myCustomAction(){
system.debug('hey');
PageReference returnPage = new PageReference(ApexPages.currentPage().getParameters().get('retURL'));
system.debug('returnPage = '+returnPage);
return returnPage;
}
}
extension V2:
public without sharing class ChecklistTemplateItemMassApply{
public static String appendValue{get;set;}
public static String selection{get;set;}
public static List<SelectOption> applyRemove{get;set;}
ApexPages.StandardSetController setCon;
public ChecklistTemplateItemMassApply(ApexPages.StandardSetController myController){
setCon = myController;
List<String> addList = new List<String>{'Applies_To__c'};
setCon.addFields(addList);
system.debug('addList = '+addList);
applyRemove = new List<SelectOption>();
applyRemove.add(new SelectOption('','--None--'));
applyRemove.add(new SelectOption('Include','Include'));
applyRemove.add(new SelectOption('Remove','Remove'));
}
public void ApexPages.pageReference myCustomAction(){
system.debug('hey');
}
}

actionSupport InputText Onchange Not Firing

I'm racking my brain over this. Its a simple on change function which should fire but when I test in my org, I never get the debug "here" despite getting the rest in my class. I've looked over dozens of different google results and the code is identical. Does anyone know if SF is just broken?
public class testController {
public static String testNewString {get;set;}
public testController(){}
public static Boolean isAccountAvailable()
{
System.debug('here');
List<Account> tempList = ([SELECT Id
FROM Account
WHERE Name = :testNewString]);
if(tempList.size() != 0)
{
return true;
}
else
{
return false;
}
}
}
<apex:page controller="testController" docType="html-5.0">
<apex:form id="form1">
<apex:inputText value="{!testNewString }" html-placeholder="New Sales Order #">
<apex:actionSupport event="onChange" action="{!isAccountAvailable}" rerender="form1" />
</apex:inputText>
</apex:form>
</apex:page>
For whoever is looking for an answer, the fix for this code snippet is subtle. Make sure the function returns a page reference. Making fields global might help make it visible to VF.
public class TestController {
public String testNewString {get;set;}
public testController(){}
public PageReference isAccountAvailable()
{
System.debug('here');
List<Account> tempList = ([SELECT Id
FROM Account
WHERE Name = :testNewString]);
if(tempList.size() != 0)
{
System.debug('true');
}
else
{
System.debug('false');
}
return null;
}
}
<apex:page controller="TestController" docType="html-5.0">
<apex:form id="form1">
<apex:inputText value="{!testNewString}" html-placeholder="New Sales Order #">
<apex:actionSupport event="onchange" action="{!isAccountAvailable}" rerender="form1" />
</apex:inputText>
</apex:form>
</apex:page>

How to get an input value on a Visualforce page from an Apex controller?

Let's pretend that I have an sObject called MyCutomObject with the fields Column1, Column2, Column3, PickMeColumn and others. The type of the PickMeColumn is Picklist.
While it's easy to access the object's instance data in my page, I'm a bit stucked with how to get the user's input data from that page to be accessible inside the controller.
Page code:
<apex:page sidebar="false" standardController="MyCustomObject__c" extensions="MyCustomSearchController">
<apex:form >
<apex:pageBlock title="Search Criteria">
<apex:pageBlockSection>
<apex:inputField value="{!myObject.PickMeColumn__c}" />
</apex:pageBlockSection>
<apex:commandButton value="Search" id="SearchButton" action="{!search}"/>
</apex:pageBlock>
<apex:pageBlock title="Search Results">
<apex:pageBlockTable value="{!myObjectList}" var="myObject">
<apex:repeat value="{!myObject}" var="aRecord">
<apex:column value="{!aRecord.Column1__c}"/>
<apex:column value="{!aRecord.Column2__c}"/>
<apex:column value="{!aRecord.Column3__c}"/>
</apex:repeat>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller code:
public class MyCustomSearchController {
public MyCutomObject__c myObject {get;set;}
public List<MyCutomObject__c> myObjectList {get;set;}
public MyCustomSearchController(ApexPages.StandardController controller) {
}
public PageReference search() {
String ValueSelectedByUser = '??? Help!';
myObjectList = [SELECT Column1__c, Column2__c, Column3__c FROM MyCutomObject__c WHERE PickMeColumn__c = ValueSelectedByUser];
return ApexPages.currentPage();
}
}
In controller's code simply like so:
public class MyCustomSearchController {
public MyCutomObject__c myObject {get;set;}
public List<MyCutomObject__c> myObjectList {get;set;}
public MyCustomSearchController(ApexPages.StandardController controller) {
myObject = new myCustomObject__c(); // Must create the object!
}
public PageReference search() {
String ValueSelectedByUser = myObject.PickMeColumn__c;
myObjectList = [SELECT Column1__c, Column2__c, Column3__c FROM MyCutomObject__c WHERE PickMeColumn__c = :ValueSelectedByUser];
// Have to add the colon
return ApexPages.currentPage();
}
}

Resources