Tag Archives: Javascript

Copy Billing Address to Shipping Address in Salesforce Visualforce Page

<apex:page standardController="Contact" extensions="ContactExtn" id="pgContact">

    <script type="text/javascript">
        function copyAddress() {
            // Variables for Billing Address
            var copy_BillingPostalCode = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBPostalCode').value;
            var copy_BillingAddress1 =document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBAdd1').value;
            var copy_BillingAddress2 = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBAdd2').value;
            var copy_BillingAddress3 = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBAdd3').value;
            var copy_BillingCity = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBCity').value;
            var copy_BillingState = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBState').value;
            var copy_BillingCountry = document.getElementById('pgContact:fmContact:pbContact:pbsBillingAdd:ifBCountry').value;
           
            // Copying the Billing Address to the Shipping Address
            if(copy_BillingPostalCode != null)
            {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSPostalCode').value = copy_BillingPostalCode;
             }
            if(copy_BillingAddress1 != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSAdd1').value = copy_BillingAddress1;
            }
            if(copy_BillingAddress2 != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSAdd2').value = copy_BillingAddress2;
            }
            if(copy_BillingAddress3 != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSAdd3').value = copy_BillingAddress3;
            }
            if(copy_BillingCity != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSCity').value = copy_BillingCity;
            }
            if(copy_BillingState != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSState').value = copy_BillingState;
            }
            if(copy_BillingCountry != null) {
                document.getElementById('pgContact:fmContact:pbContact:pbsShippingAdd:ifSCountry').value = copy_BillingCountry;
            }
        }
    </script>
        
    <apex:form id="fmContact">
        <apex:sectionHeader title="Contact Information" subtitle="{!Contact.Name}"/>
        <apex:pageBlock mode="Edit" id="pbContact">
            <apex:pageBlockSection showHeader="true" collapsible="false" columns="2" title="Billing Address Information" id="pbsBillingAdd">
                <apex:inputField value="{!Contact.Billing_Postal_Code__c}" id="ifBPostalCode"/>
                <apex:inputField value="{!Contact.Billing_City__c}" id="ifBCity"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_1__c}" id="ifBAdd1"/>
                <apex:inputField value="{!Contact.Billing_State__c}" id="ifBState"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_2__c}" id="ifBAdd2"/>
                <apex:inputField value="{!Contact.Billing_Country__c}" id="ifBCountry"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_3__c}" id="ifBAdd3"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection showHeader="true" collapsible="false" columns="2" title="Shipping Address Information" id="pbsShippingAdd">
                <apex:inputField value="{!Contact.Shipping_Postal_Code__c}" id="ifSPostalCode"/>
                <a HREF="#" onClick="return copyAddress();">Copy Billing Address to Shipping Address</a>
                <apex:inputField value="{!Contact.Shipping_Address_Line_1__c}" id="ifSAdd1"/>
                <apex:inputField value="{!Contact.Shipping_City__c}" id="ifSCity"/>
                <apex:inputField value="{!Contact.Shipping_Address_Line_2__c}" id="ifSAdd2"/>
                <apex:inputField value="{!Contact.Shipping_State__c}" id="ifSState"/>
                <apex:inputField value="{!Contact.Shipping_Address_Line_3__c}" id="ifSAdd3"/>
                <apex:inputField value="{!Contact.Shipping_Country__c}" id="ifSCountry"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Convert Salesforce 15 Character ID to 18 Character ID Using Javascript

function Convert15CharTo18CharId(id) {
    if (id == null) {
        return id;
    }
    //Scrub quotes from this id
    id = id.replace(/\"/g, '');
    if (id.length != 15) {
        return null;
    }
    var suffix = "";

    for (var i = 0; i < 3; i++) {
        var flags = 0;
        for (var j = 0; j < 5; j++) {
            var c = id.charAt(i * 5 + j);
            if (c >= 'A' && c < = 'Z') {
                flags += 1 << j;
            }
        }
        if (flags <= 25) {
            suffix += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(flags);
        } else {
            suffix += "012345".charAt(flags - 26);
        }
    }
    return id + suffix;
}

Javascript Function For Validation on Number,Letters and Currency Input Fields

<script type="text/javascript">
    // This function is being used for providing different validation on your text box as per your need
    function ValidatedField(e, allow) {

        var AllowableCharacters = '';

        if (allow == 'Letters') {
            AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        }
        if (allow == 'Numbers') {
            AllowableCharacters = '1234567890';
        }
        if (allow == 'NameCharacters') {
            AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\'';
        }
        if (allow == 'NameCharactersAndNumbers') {
            AllowableCharacters = '1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\'';
        }
        if (allow == 'Currency') {
            AllowableCharacters = '1234567890.';
        }

        var k = document.all ? parseInt(e.keyCode) : parseInt(e.which);

        if (k != 13 && k != 8 && k != 0) {
            if ((e.ctrlKey == false) && (e.altKey == false)) {
                return (AllowableCharacters.indexOf(String.fromCharCode(k)) != -1);
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
</script>

Tab Title shown as External Page in Console

<apex:includeScript value="/support/console/20.0/integration.js"/>
<script type="text/javascript">
	function testSetTabTitle() {
		//Set the current tab's title
		sforce.console.setTabTitle('Account: {!Account.Name}');
	}
	var pageLoad = window.onload;
	window.onload = function() {
		testSetTabTitle();
	}
</script>

Insert Records in Salesforce by using JavaScript

In this article I’ll demonstrate how to insert record in Salesforce object by using javascript, in VF page without any standard or custom controller or by apex class.

By using AJAX Toolkit we can do this task easily. There are two types of AJAX Toolkit one is synchronous and another one is asynchronous call.

Here is a simple example of data insert using Javascript in Visualforce page. In below example I’m using synchronous call.
These are the steps to insert data using Javascript:

  1. Connecting to the AJAX Toolkit(By using login methods or getting Session_ID).
  2. Embedding the API methods in JavaScript.
  3. Processing the results.

Sample Code:

<apex:page id="pg">
    <script src="/soap/ajax/20.0/connection.js" type="text/javascript"></script>
	<script>
		function insertAcc(){

			// Getting Session ID.
			sforce.connection.sessionId = "{!$Api.Session_ID}";

			//Creating New Account Record.
			var account = new sforce.SObject("Account");

			//Getting Account Name from inputText.
			account.Name = document.getElementById("pg:frm:pb:pbs:pbsi:txtName").value;

			//Create method 
			var result = sforce.connection.create([account]);

			//Getting result 
			if (result[0].getBoolean("success")) {
				alert("New Account is created with id " + result[0].id);
			}
			else {
				alert("failed to create new Account " + result[0]);
			}
		}
	</script>
	<apex:form id="frm">
		<apex:pageBlock title="Insert Account" tabStyle="Account" id="pb">
			<apex:pageBlockSection title="Account Name" columns="1" id="pbs">
				<apex:pageBlockSectionItem id="pbsi">
					<apex:outputLabel value="Name" />
					<apex:inputText title="Name" id="txtName" />
				</apex:pageBlockSectionItem>
			</apex:pageBlockSection> 
			<apex:pageBlockButtons>
				<apex:commandButton onclick="return insertAcc();" value="Save"/>
			</apex:pageBlockButtons>
		</apex:pageBlock>
	</apex:form>
</apex:page>