Tag Archives: Force.com

Immediate=true is not working on custom buttons with HTML5 docType in Visualforce Page

If you are setting your docType="html-5.0" and having a validation error on cancel, after the immediate="true" for a command button, action function etc.
Here are two options for disable all validation for all command buttons and disable validations for specific command button.

Option 1:
If your requirement is to disable all validation for entire page, then you can add html-novalidate="novalidate" attribute to apex form. This will disable all validations for all command buttons.

<apex:page standardController="Account" docType="html-5.0">
	<apex:form html-novalidate="novalidate">
		<apex:pageBlock title="Account Information">
			<apex:pageBlockButtons location="bottom">
				<apex:commandButton action="{!Cancel}" value="Cancel" />
			</apex:pageBlockButtons>
			<apex:pageBlockSection>
				<apex:inputField value="{!Account.Name}" required="true" ></apex:inputField>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Option 2:
If your requirement is to disable the validation for specific command button, then you can add immediate="true" and html-formnovalidate="formnovalidate" attribute to that command button. This will disable all validations for all command buttons.

<apex:page standardController="Account" docType="html-5.0">
	<apex:form>
		<apex:pageBlock title="Account Information">
			<apex:pageBlockButtons location="bottom">
				<apex:commandButton action="{!Save}" value="Save" />
				 <apex:commandButton action="{!Cancel}" value="Cancel" immediate="true" html-formnovalidate="formnovalidate" />
			</apex:pageBlockButtons>
			<apex:pageBlockSection>
				<apex:inputField value="{!Account.Name}" required="true" ></apex:inputField>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Salesforce StandardController Methods

The following methods are for StandardController:

addFields(fieldNames)
When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup. This method adds a reference to each field specified in fieldNames so that the controller can explicitly access those fields as well.

cancel()
Returns the PageReference of the cancel page.

delete()
Deletes record and returns the PageReference of the delete page.

edit()
Returns the PageReference of the standard edit page.

getId()
Returns the ID of the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

getRecord()
Returns the record that is currently in context, based on the value of the id query string parameter in the Visualforce page URL.

reset()
Forces the controller to reacquire access to newly referenced fields. Any changes made to the record prior to this method call are discarded.

save()
Saves changes and returns the updated PageReference.

view()
Returns the PageReference object of the standard detail page.

Salesforce StandardSetController Methods

The following methods are for StandardSetController:

cancel()
Returns the PageReference of the original page, if known, or the home page.

first()
Returns the first page of records.

getCompleteResult()
Indicates whether there are more records in the set than the maximum record limit. If this is false, there are more records than you can process using the list controller. The maximum record limit is 10,000 records.

getFilterId()
Returns the ID of the filter that is currently in context.

getHasNext()
Indicates whether there are more records after the current page set.

getHasPrevious()
Indicates whether there are more records before the current page set.

getListViewOptions()
Returns a list of the listviews available to the current user.

getPageNumber()
Returns the page number of the current page set. Note that the first page returns 1.

getPageSize()
Returns the number of records included in each page set.

getRecord()
Returns the sObject that represents the changes to the selected records. This retrieves the prototype object contained within the class, and is used for performing mass updates.

getRecords()
Returns the list of sObjects in the current page set. This list is immutable, i.e. you can’t call clear() on it.

getResultSize()
Returns the number of records in the set.

getSelected()
Returns the list of sObjects that have been selected.

last()
Returns the last page of records.

next()
Returns the next page of records.

previous()
Returns the previous page of records.

save()
Inserts new records or updates existing records that have been changed. After this operation is finished, it returns a PageReference to the original page, if known, or the home page.

setFilterID(filterId)
Sets the filter ID of the controller.

setpageNumber(pageNumber)
Sets the page number.

setPageSize(pageSize)
Sets the number of records in each page set.

setSelected(selectedRecords)
Set the selected records.

Custom Send Email Button on Custom Objects

  • Go to Setup | Create | Objects.
  • Select your Custom Object.
  • Go to the Custom Buttons, Links and Actions section.
  • Click on New Button or Link.
  • Create a New Custom Button.
  • Select the Display Type as Detail Page Button.
  • Select the Behavior as Execute JavaScript.
  • Select the Content Source as OnClick JavaScript.
  • Include below code:
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

window.location = '/_ui/core/email/author/EmailAuthor?rtype=003&p3_lkid={!CustomObject__c.Id}&retURL=/{!CustomObject__c.Id}&p5={!$User.Email}&p24="{!CustomObject__c.To_Email__c}&template_id=00X90000001FCj4';

Note: If the custom object holds some of the contact details then you would need to use the values from custom object, not the contact object.

Save it. Edit the Page Layout of the Custom Object and drag this button on to the Page Layout under the Custom Button section.

The information shown below are the parameters that can be added in the URL. Fields that are mark with asterisk are always needed in the URL.

Parameter Name Parameter Value
p2_lkid To (can be Contact or Lead Id)
p3_lkid Related To (usually the parent record Id)
p4 CC
p5 BCC
p6 Subject
p23 Email Body
p24 Additional To
Template_Id Salesforce email template Id
retURL Redirection page when cancel button is clicked

Function to Calculate the 18 Character ID From 15 Character ID

public String Convert15CharTo18CharId(String id) {
    String suffix = '';
    Integer flags;

    for (integer i = 0; i < 3; i++) {
        flags = 0;
        for (Integer j = 0; j < 5; j++) {
            String c = id.substring(i * 5 + j, i * 5 + j + 1);
            //Only add to flags if c is an uppercase letter:
            if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                flags = flags + (1 << j);
            }
        }
        if (flags <= 25) {
            suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags, flags + 1);
        } else {
            suffix += '012345'.substring(flags - 26, flags - 25);
        }
    }
    String convertId = id + suffix;
    //18 Digit Id with checksum
    System.debug('convertId-' + convertId);
    return 18 digitId;
}

Example:

String Id = '0019000001EJNfj';
String convertedId = Convert15CharTo18CharId(Id);