Monday, June 23, 2014

Custom Visualforce Action

Custom Visualforce Action:
Visualforce pages you create to use as object-specific actions need to use one of the standard object controllers.The following code sample shows a page designed to be used as a custom action on the account object, so it uses the standard Account controller. This action lets users create cases from account detail pages, and it has a different user interface from standard create actions.

Step 1: Create Apex Class
public with sharing class CreateCaseExtension {
    private final SObject parent;
    public Case theCase {get; set;}
    public String lastError {get; set;}
    
    public CreateCaseExtension(ApexPages.StandardController controller) {
        parent = controller.getRecord();
        theCase = new Case();
        theCase.accountId = parent.id;
        lastError = '';
    }
      
    public PageReference createCase() {
        createNewCase();
        theCase = new Case();
        theCase.accountId = parent.id;
        return null;
    }
       
     private void createNewCase() {      
        try {
            insert theCase;
            
            FeedItem post = new FeedItem();
            post.ParentId = ApexPages.currentPage().getParameters().get('id');
            post.Body = 'created a case';
            post.type = 'LinkPost'; 
            post.LinkUrl = '/' + theCase.id;
            post.Title = theCase.Subject;
            insert post;
        } catch(System.Exception ex){
           lastError = ex.getMessage();
        }
    }   
}

Step 2: Create VF Page
<apex:page standardcontroller="Account" extensions="CreateCaseExtension" showHeader="false">
    <script type='text/javascript' src='/canvas/sdk/js/publisher.js'/> 
    <style>
        .requiredInput .requiredBlock, .requiredBlock {background-color: white;} 
        .custompubblock div {display: inline-block;} 
        .custompublabel {width:54px;} 
    </style>
    <script> 
        function refreshFeed() { 
            Sfdc.canvas.publisher.publish({name : 'publisher.refresh', payload : {feed:true}}); 
        }
    </script>   
    <div> 
        <apex:form > 
            <apex:actionFunction action="{!createCase}" name="createCase" rerender="out" 
            oncomplete="refreshFeed();"/> 
            <apex:outputPanel id="out" > 
                <div class="custompubblock"> 
                    <div class="custompublabel">Account:</div><apex:inputField value="{!theCase.accountId}" 
                    style="margin-left:0;"/>&nbsp;&nbsp;&nbsp;
                    <div>Contact:&nbsp;</div><apex:inputField value="{!theCase.contactId}" />
                </div>
                <apex:inputField value="{!theCase.description}" style="width:538px;height:92px;margin-top:4px;" />
                <div class="custompubblock" style="margin-top:5px;"> 
                    <div>Status:&nbsp;</div><apex:inputField value="{!theCase.status}" />&nbsp;&nbsp;&nbsp; 
                    <div>Priority:&nbsp;</div><apex:inputField value="{!theCase.priority}" />&nbsp;&nbsp;&nbsp; 
                    <div>Case Origin:&nbsp;</div><apex:inputField value="{!theCase.origin}" /> 
                </div> 
            </apex:outputPanel>
        </apex:form><br/>
        <button type="button" onclick="createCase();" 
        style="position:fixed;bottom:0px;right:0px;padding:5px 10px; 
        font-size:13px; font-weight:bold; line-height: 
        18px;background-color:#0271BF;background-image:-moz-linear-gradient(#2DADDC, #0271BF);background-repeat:repeat-x;border-color:#096EB3;" 
        id="addcasebutton">Create Case</button> 
    </div>  
</apex:page>

Step 3: Create Action of Type Visualforce on Account
Step 4: Add action to Account Page Layout
Step 5: Finally use Visualforce action on detail page 



Pages for Global Custom Actions :
Pages you create to use as global actions can’t use any of the standard object controllers.
The following code sample shows a Visualforce page designed to be used as a custom action on any object that supports actions.This action lets users create cases from record detail pages, Chatter, Chatter groups (except customer groups), or the home page, and it has a different user interface from standard create actions. As with all global actions, the records created through this action are not automatically associated with any other records.
Step 1: Create Apex Class
public with sharing class CreateCaseController {
public Case theCase {get; set;}
public String lastError {get; set;}
public CreateCaseController() {
theCase = new Case();
lastError = '';
}
public PageReference createCase() {
createNewCase();
theCase = new Case();
return null;
}
private void createNewCase() {
try {
insert theCase;
FeedItem post = new FeedItem();
post.ParentId = ApexPages.currentPage().getParameters().get('id');
post.Body = 'created a case';
post.type = 'LinkPost';
post.LinkUrl = '/' + theCase.id;
post.Title = theCase.Subject;
insert post;
} catch(System.Exception ex){
lastError = ex.getMessage();
}
}
}

Step 2: Create VF Page
<apex:page controller="CreateCaseController" showHeader="false">
<script type='text/javascript' src='/canvas/sdk/js/publisher.js'/>
<style>
.requiredInput .requiredBlock, .requiredBlock {background-color: white;}
.custompubblock div {display: inline-block;}
.custompublabel {width:54px;}
</style>
<script>
function refreshFeed() {
Sfdc.canvas.publisher.publish({name : 'publisher.refresh', payload : {feed:
true}});
}
</script>
<div>
<apex:form >
<apex:actionFunction action="{!createCase}" name="createCase" rerender="out"
oncomplete="refreshFeed();"/>
<apex:outputPanel id="out" >
<div class="custompubblock">
<div>Subject:&nbsp;</div><apex:inputField value="{!theCase.subject}"
style="width:500px;" />
</div>
<div class="custompubblock">
<div class="custompublabel">Account:</div><apex:inputField value="{!theCase.accountId}"
style="margin-left:0;"/>&nbsp;&nbsp;&nbsp;
<div>Contact:&nbsp;</div><apex:inputField value="{!theCase.contactId}"
/>
</div>
<apex:inputField value="{!theCase.description}"
style="width:500px;height:92px;margin-top:4px;" />
<div class="custompubblock" style="margin-top:5px;">
<div>Status:&nbsp;</div><apex:inputField value="{!theCase.status}"
/>&nbsp;&nbsp;&nbsp;
<div>Priority:&nbsp;</div><apex:inputField value="{!theCase.priority}"
/>&nbsp;&nbsp;&nbsp;
<div>Case Origin:&nbsp;</div><apex:inputField value="{!theCase.origin}"
/>
</div>
<div style="color:red;">{!lastError}</div>
</apex:outputPanel>
</apex:form><br/>
<button type="button" onclick="createCase();"
style="position:fixed;bottom:0px;right:0px;padding:5px 10px;
font-size:13px; font-weight:bold; line-height:
18px;background-color:#0271BF;background-image:-moz-linear-gradient(#2DADDC,
#0271BF);background-repeat:repeat-x;
border-color:#096EB3;" id="addcasebutton">Create Case</button>
</div>
</apex:page>    

Step 3: Create Global Action
Step 4: Add to Chatter Group Layout
 Step 5: Finally use Visualforce Global Action on Group 
                            

No comments:

Post a Comment