• Shekhar Chaudhary 6
  • 0 Points
  • Member since 2018

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1

Hi All, in my custom object I am having a cutom button"convert" when i click this button"convert"it will  create new Account,Contact and Opportunity. But I need to do same as Lead(Ie,. in Lead if click "convert" button it will go to an other page where we can existing Account for that or we can create a new Account. For creating Opportunity it will have a checkbox which will ask us whether we have to new opportunity for that record, after we made all this we again  "convert")

Like that when i click my custom button"convert" on my custom tab, it must go to another window from that page we have select Account for that(existing or new) if i select existing account means all Account must be displayed in the listbox, we have to select any from that and we have to  create a new opportunity for that record.


Here i have attached my Apex code to create Account, contact and opportunity.Also VF page code.

Apex code:


public class Convert

public PageReference RedirecttoSite()
String currentLead = '/' + siteObj.Id;
PageReference pageRef = new PageReference(currentLead);
return pageRef;

private Site__c siteObj;
//public ID Cus_Account_ID, Cus_obj_Record_ID;
// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public Convert(ApexPages.StandardController stdController)
siteObj = (Site__c)stdController.getRecord();
// Cus_obj_Record_ID=siteObj.Id;
List<Account> acctList = new List<account>();
public void convertbutton()

Account acc = new Account();
acc.Name = siteObj.Company_Name__c;
acctList = [select id from Account where Name = :acc.Name];
if (acctList.size() > 0)

ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Account Name ' + acc.Name + ' already exists,Duplicate Account Name not allowed');
acc.BillingStreet = siteObj.Company_Address__c;
acc.BillingCity = siteObj.Company_City__c;
acc.BillingState= siteObj.Company_State__c;
acc.BillingPostalCode = siteObj.Company_Postal_Code__c;
acc.Phone = siteObj.Company_Phone__c;
acc.Website = siteObj.Company_Email__c;
acc.Fax = siteObj.Company_Fax__c;
// acc.CurrencyIsoCode = siteObj.CurrencyIsoCode;

insert acc;
ApexPages.Message successmsg = new ApexPages.Message(ApexPages.severity.confirm,'New Account ' + acc.Name + ' is created, Related Contact and Opporunity also created' );
Catch (Exception ex1)
Contact cc = new Contact();
cc.LastName = siteObj.Contact_Name__c;
cc.Email = siteObj.Contact_Email__c;
cc.Phone = siteObj.Contact_Mobile__c;
cc.AccountId = acc.Id;

insert cc;
Catch (Exception ex2)


Opportunity opp = new Opportunity();
opp.Name = siteObj.Opp_Name__c;
opp.AccountId= acc.Id;
opp.CloseDate = siteObj.Opp_CloseDate__c;
opp.StageName = siteObj.Opp_stage__c;
if(opp.StageName == 'Needs Analysis')
opp.Probability= 20;
else if(opp.StageName == 'Prospecting')
opp.Probability= 10;
else if(opp.StageName == 'Negotiation/Review')
opp.Probability= 90;
else if(opp.StageName == 'Closed Won')
opp.Probability= 100;
else if(opp.StageName == 'Closed Lost')
opp.Probability= 0;

insert opp;
Catch (Exception ex3)



VF page code for "convert"button:

<apex:page standardController="Site__c" cache="true" action="{!convertbutton}" extensions="Convert" >
<apex:messages style="color:red; font-weight:bold; text-align:center;"/>
<apex:form >
<!--<apex:pagemessages />-->
<apex:outputField value="{!Site__c.Company_Name__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_Address__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_City__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_State__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_Postal_Code__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_Phone__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_Email__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Company_Fax__c}" rendered="false"/>

<apex:outputField value="{!Site__c.Contact_Name__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Contact_Email__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Contact_Mobile__c}" rendered="false"/>

<!--<apex:outputField value="{!Site__c.CurrencyIsoCode}" rendered="false"/>-->
<apex:outputField value="{!Site__c.ConvertedSite__c}" rendered="false"/>

<apex:outputField value="{!Site__c.Opp_Name__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Opp_CloseDate__c}" rendered="false"/>
<apex:outputField value="{!Site__c.Opp_stage__c}" rendered="false"/>

<apex:commandLink value="Redirect to Site" style="color:blue; font-weight:bold;" action="{!RedirecttoSite}"/>




Any one tell how to do this. kindly give me any idea for this and tell me any sample code for this.Waitng for your reply