How to add a custom button to contact in salesforce

H

Open you salesforce developer’s account:

Go to Setup->Customize->Contacts->Buttons and Links-> Custom Buttons

1. Create custom button

2. Create a tab corresponding to the page and custom button

3. Check out the parameter id in button link or url

Sample code for custom button on contact page:

Task:

I’d like a custom button written for Salesforce which I could install on the contact page. The billing address of contact should be copied into mailing address of contact. If the address is blank mailing address should remain blank.

Class Code:

public class TryContact
{
public void process()
{
//Read ContactId from url
string id;
id=ApexPages.currentPage().getParameters().get(‘id’);
//Get Contact Details
Contact c = [select OtherStreet from Contact where Id=:id];
ApexPages.Message msg;
if(c.OtherStreet!=null)
{
msg = new ApexPages.Message(ApexPages.Severity.ERROR, ‘Billing address copied’);
ApexPages.addMessage(msg);
c.MailingStreet=c.OtherStreet;
Update c;
}
}
}

Page Code:

<apex:page controller="TryContact" action="{!process}">
    <apex:pagemessages />
</apex:page>

About the author

juilee.joshi
By juilee.joshi

Category