Tag Archives: Apex

Calling apex method from a custom button using javascript

Sometimes we need to call a method of an apex class, on click of a custom button. Let’s take an example, how we call an apex method, on click of a custom button.
Step 1:
Lets first create the Apex class.

global class MyClass {
    webservice static String myMethod(String studentId){
       return 'Test';
    }
}

Note:

  • Class needs to be a Global class.
  • And the method you intend to call from the javascript must be a Webservice Method.

Step 2:
Now to setup the custom Button.
Goto –> Setup –> Object –> Buttons, links and Actions section –> Click New Button or Link

1

  • Enter the Name of the button.
  • Behaviour : Execute Javascript.
  • Content source : On-Click Javascript.

In the code for button copy the following line of codes:

{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
 
try{ 
  var studentId = '{!Student__c.Id}';
  var result = sforce.apex.execute("MyClass", "myMethod",{studentId : studentId}); 
  alert(result);
} 
catch(ex) {
  alert(ex); 
}

2

Send Meeting Invite Calendar Email From Apex

Sample Code:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Set To Addresss
mail.setToAddresses(new List<String>{'itzbiswajeet@gmail.com'});
//Set Email Subject
mail.setSubject('Test Meeting Invitation');
//Set Email Body
mail.setPlainTextBody('Test Meeting');

//Meeting Start Time & End Time
DateTime dt = DateTime.now().addDays(5);
//Start Date
String startDT = String.valueof(dt.year() +'0'+ dt.month() +''+ dt.day() + 'T010000Z');
//End Date
String endDT = String.valueof(dt.year() +'0'+ dt.month() +''+ dt.day() + 'T020000Z');

//Create Meeting Body
String meetingInviteBody = ''; 
meetingInviteBody += 'BEGIN:VCALENDAR\n';        
meetingInviteBody += 'PRODID::-//hacksw/handcal//NONSGML v1.0//EN\n';
meetingInviteBody += 'VERSION:2.0\n';
meetingInviteBody += 'METHOD:PUBLISH\n';
meetingInviteBody += 'X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n';
meetingInviteBody += 'BEGIN:VEVENT\n';
meetingInviteBody += 'CLASS:PUBLIC\n';
meetingInviteBody += 'CREATED:20150126T203709Z\n';        
meetingInviteBody += 'DTEND:'+endDT+'\n';
meetingInviteBody += 'DTSTAMP:20150126T203709Z\n';        
meetingInviteBody += 'DTSTART:'+startDT+'\n';
meetingInviteBody += 'LAST-MODIFIED:20150126T203709Z\n';
meetingInviteBody += 'LOCATION:USA\n';
meetingInviteBody += 'PRIORITY:5\n';
meetingInviteBody += 'SEQUENCE:0\n';
meetingInviteBody += 'SUMMARY:Test Meeting\n';
meetingInviteBody += 'LANGUAGE=en-us:Meeting\n';
meetingInviteBody += 'TRANSP:OPAQUE\n';
meetingInviteBody += 'X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><META NAME="Generator" CONTENT="MS Exchange Server version 08.00.0681.000"><TITLE></TITLE></HEAD><BODY><!-- Converted from text/plain format --></BODY></HTML>\n';
meetingInviteBody += 'X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n';
meetingInviteBody += 'X-MICROSOFT-CDO-IMPORTANCE:1\n';
meetingInviteBody += 'END:VEVENT\n';
meetingInviteBody += 'END:VCALENDAR';

//Meeting Email Attachment
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.Filename = 'meeting.ics'; 
attach.ContentType = 'text/calendar';     
attach.Inline = true;     
attach.Body = Blob.valueOf(meetingInviteBody);

//Attach Meeting Attachment
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});
//Send Email
Messaging.SendEmailResult[] er = Messaging.sendEmail(new Messaging.Email[] { mail }); 

Checkbox Options in Salesforce

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form>
        <!--Country Checkboxes-->
        <apex:selectCheckboxes value="{!countries}">
            <!--Country List-->
            <apex:selectOptions value="{!CountryList}"/>
        </apex:selectCheckboxes><br/>
        <apex:commandButton value="Submit" action="{!getSelectedCountries}" rerender="out" status="status"/>
    </apex:form>
    <!--Selected Countries Output-->
    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="Loading...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>Selected Countries:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>

Apex Class:

public class SampleController {
    
    Public List<string> countries {get;set;}
    
    public SampleController(){
        countries = new List<string>();
    }
    
    public List<SelectOption> getCountryList() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('USA','USA'));
        options.add(new SelectOption('INDIA','India'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }
    
    public pagereference getSelectedCountries(){
        system.debug('selectedcountries -' + countries);
        return null;
    }
}

Output:

To display checkbox options in vertical direction, use layout property layout="pageDirection" in selectCheckboxe component.

<apex:selectCheckboxes value="{!countries}" layout="pageDirection">
    <apex:selectOptions value="{!CountryList}"/>
</apex:selectCheckboxes>