function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LavanyaLavanya 

Creating new visual force page for button"convert" same as lead

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)
{
System.debug('******sai******');
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');
ApexPages.addMessage(errormsg);
return;
}
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;

try
{
insert acc;
ApexPages.Message successmsg = new ApexPages.Message(ApexPages.severity.confirm,'New Account ' + acc.Name + ' is created, Related Contact and Opporunity also created' );
ApexPages.addMessage(successmsg);
}
Catch (Exception ex1)
{
ex1.getmessage();
}
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;

try
{
insert cc;
}
Catch (Exception ex2)
{

ex2.getmessage();
}

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;

try
{
insert opp;
}
Catch (Exception ex3)
{
 ex3.getmessage();
}

}
 }

 

VF page code for "convert"button:

<apex:page standardController="Site__c" cache="true" action="{!convertbutton}" extensions="Convert" >
<br/><br/>
<apex:messages style="color:red; font-weight:bold; text-align:center;"/>
<apex:form >
<center>
<!--<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}"/>

</center>

</apex:form>
</apex:page>

 

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

 

Regards,

Lavanya.

 

Jagannatha Reddy BandaruJagannatha Reddy Bandaru
Hi Lavanya,

Did you get any solutions for this issue, i am also facing same problem.
Kindly let me know if you have any solution.

Thanks,
Jagan
jagan.srit@gmail.com
harish r 20harish r 20
Hi   Lavanya,

                            Lavanya did you get any solutions for this issue, i am also facing same problem. Please  send me code if it solve.
my email id harishramachandruni04@gmail.com pllease send me i am waiting for your replay. 


Thanks,
harish.R
harishramachandruni04@gmail.com

                
Shekhar Chaudhary 6Shekhar Chaudhary 6
Hi  Lavanya 
                           Lavanya did you get any solution for this issue , because i'm also facing the same problem .Please send me the code if you sove it .My emailid is shekharror19@gmail.com