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 

Checking duplicate Account name when click Custom button"convert"

Hi All, I am having a custom button "convert" on my custom object, whencilck the convert button it must create a new account, contact and opportunity.Before it convert it into Account,Contact and Opportunity it must check the all Account name in "Account" tab. If the Account Name is exist means it must display the error message"Account Name exist" . Other wise it must create new Account, Contact and Opportunity. For "convert " i am using Apex code and VF page. I tried using a checkbox for this but when uncheck the checkbox it is creating a new account in the same name, that is same account name which is already exist. Kindly tell me now to resolve this

 

Apex code:

public class Convert {

public PageReference RedirecttoLead()
{
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('******Voltum******');
siteObj = (Site__c)stdController.getRecord();
// Cus_obj_Record_ID=siteObj.Id;
}

public void convertbutton(){

Account acc = new Account();
acc.Name = siteObj.Company_Name__c;
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;
}
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;
//temp_siteObj = [select AccountId from Contact where Id ='Cus_obj_Record_ID'];
//Cus_Account_ID = temp_siteObj.AccountId;

//Id accountId = [select AccountId from Contact where Id = {ID}][0].AccountId;
//System.Debug(accountId);

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;

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

ex3.getmessage();
}




// try
// {
// Delete siteObj;
//}
//catch(Exception ex4)
// {
// ex4.getmessage();
// }

}

}

 

VF Page Code:

<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: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 Lead" style="color:blue; font-weight:bold;" action="{!RedirecttoLead}"/>

</center>

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

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

Lavanya,

 

Sorry - I think I'm understanding your code a little better now. Just check for the existence of the account with the same name in the convertButton method.

 

public void convertbutton(){

Account acc = new Account();
acc.Name = siteObj.Company_Name__c;

 

List<Account> acctList = [select id from Account where Name = :acc.Name];

if (acctList.size() > 0) {

     // add an apex message to indicate the existence of the account

    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;

All Answers

ForcepowerForcepower

Lavanya,

 

You need to make a few changes:

 

1. convertButton should not be tied to your page definition. It should be the action on a command button.

2. You need a form to collect your data. Right now you have a form for an existing record which displays it (and creates a copy) - everything is an outut field - you'll input fields.

 

Best,

Ram

LavanyaLavanya

Hi Ram, Thanks for the reply. Kindly tell me more about this, also give some tips to do this.Waiting for your reply

Regards,

Lavanya.

ForcepowerForcepower

Lavanya,

 

Sorry - I think I'm understanding your code a little better now. Just check for the existence of the account with the same name in the convertButton method.

 

public void convertbutton(){

Account acc = new Account();
acc.Name = siteObj.Company_Name__c;

 

List<Account> acctList = [select id from Account where Name = :acc.Name];

if (acctList.size() > 0) {

     // add an apex message to indicate the existence of the account

    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;

This was selected as the best answer
LavanyaLavanya

Hi Ram, Thanks a lot. Its working. One more debut for example if i am having 2 record type for my cutom object

Customer

partner

when uploading a data from csv file i need to put each record with either one record type. In the coding how can i mention this. Give  me any idea for this.

Eg: if the Ist record has a Record type"Customer", it must be added to "customer"record type only.

        if the 2nd record has a Record type"parner", it must be added to "partner"record type only.

         if the 3rd record has a Record type"parner", it must be added to "partner"record type only.

 

Regards,

Lavanya.

 

 

ForcepowerForcepower

Lavanya,

 

You can try something like this. You'll need to fill in the gaps and change the code to reflect your actual custom object etc.

 

// query your record types for your custom object

List<RecordType> recTypes = [Select r.SobjectType, r.Name,  r.Id, r.DeveloperName, r.Description From RecordType r
where SobjectType = 'MyCustomObject' and IsActive = true];

 

// load them into a map

Map<String, Id> recTypesMap = new <ap<String, Id>();

 

for (RecordType recType : recTypes) {

      recTypesMap.put(recType.DeveloperName, recType.Id);

}

 

// within your for loop to process your custom object

for (...) {

 

     if (customer.....) {

           myCustObj.RecordTypeId = recTypesMap.get('Customer');

    }

   else {

           myCustObj.RecordTypeId = recTypesMap.get('Partner');

  }

}

 

Best,

Ram

LavanyaLavanya

Hi Ram, thanks for the reply. I tried this but i am getting error. Can younplease tell me more on this.

ForcepowerForcepower
Lavanya,

Please post the code and the error you are getting.
Best,
Ram
LavanyaLavanya

Hi Ram, thanks for the reply. check this code pls

code

 

/ query your record types for your custom object
List<RecordType> recTypes = [Select r.SobjectType, r.Name, r.Id, r.DeveloperName, r.Description From RecordType r
where SobjectType = 'Site__c' and IsActive = true];

// load them into a map
Map<String, Id> recTypesMap = new Map<String, Id>();

for (RecordType recType : recTypes) {
recTypesMap.put(recType.DeveloperName, recType.Id);
}

for (Integer i=1;i<filelines.size();i++)
{
String[] inputsitevalues = new String[]{};
inputsitevalues = filelines[i].split(',');

Site__c site= new Site__c();
for(account a: accstoupload)
{
site.Site_Acc__c = accstoupload[i-1].id;

}
if (customer) {
Site__c.RecordTypeId = recTypesMap.get('customer');
}
else if(project) {
Site__c.RecordTypeId = recTypesMap.get('Project');
}

else {
Site__c.RecordTypeId = recTypesMap.get('Partner');
}

if(site.Site_Acc__c!= null)
{


site.Company_Name__c = inputsitevalues[0];
site.Company_Address__c = inputsitevalues[1];
site.Company_City__c= inputsitevalues[2];

sitetoupload.add(site);
}
}
try
{
insert sitetoupload;
}
catch (Exception e3)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
return null;

 

public List<Site__c> getuploadedSite()
{
if (sitetoupload!= NULL)
if (sitetoupload.size() > 0)
return sitetoupload;

else
return null;
else
return null;
}

 

 

ForcepowerForcepower

Lavanya,

 

You cannot use the following literally:

 

if (customer) {
Site__c.RecordTypeId = recTypesMap.get('customer');
}
else if(project) {
Site__c.RecordTypeId = recTypesMap.get('Project');
}

else {
Site__c.RecordTypeId = recTypesMap.get('Partner');
}

 

You need to examine something within the input line that tells you whether an account is for a Partner, Project or a Customer. I don't know what that value is.

 

Also, I have no idea what you're doing with this loop: It does not make sense to me.

 

for(account a: accstoupload)
{
site.Site_Acc__c = accstoupload[i-1].id;}

 

You need to break this down and put comments everywhere to help you (and others) understand what exactly the code is doing.

 

Best,

Ram

 

LavanyaLavanya

Hi,

  Sorry for that, just check the Code and reply me

 

Code:

 

/ query your record types for your custom object
List<RecordType> recTypes = [Select r.SobjectType, r.Name, r.Id, r.DeveloperName, r.Description From RecordType r
where SobjectType = 'Site__c' and IsActive = true];

// load them into a map
Map<String, Id> recTypesMap = new Map<String, Id>();

for (RecordType recType : recTypes) {
recTypesMap.put(recType.DeveloperName, recType.Id);
}

for (Integer i=1;i<filelines.size();i++)
{
String[] inputsitevalues = new String[]{};
inputsitevalues = filelines[i].split(',');

Site__c site= new Site__c();
for(account a: accstoupload)
{

//this is an external id field to get the account id from account to site object field.

site.Site_Acc__c = accstoupload[i-1].id;

}

if(site.Site_Acc__c!= null)
{


site.Company_Name__c = inputsitevalues[0];
site.Company_Address__c = inputsitevalues[1];
site.Company_City__c= inputsitevalues[2];

site.Recordtype__c= inputsitevalues[3];

if (inputsitevalues[3]=='custome'r) {
Site__c.RecordTypeId = recTypesMap.get('customer');
}
else if(inputsitevalues[3]=='project') {
Site__c.RecordTypeId = recTypesMap.get('Project');
}

else  if(inputsitevalues[3]=='Partner'){
Site__c.RecordTypeId = recTypesMap.get('Partner');
}

sitetoupload.add(site);
}
}
try
{
insert sitetoupload;
}
catch (Exception e3)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
return null;

 

public List<Site__c> getuploadedSite()
{
if (sitetoupload!= NULL)
if (sitetoupload.size() > 0)
return sitetoupload;

else
return null;
else
return null;
}

ForcepowerForcepower

Lavanya,

 

Remove the for loop to assign your account id. 

//for(account a: accstoupload)
//{

//this is an external id field to get the account id from account to site object field.

site.Site_Acc__c = accstoupload[i-1].id;

//}

 

Also, make sure to check for 'customer' correctly.

 

if (inputsitevalues[3]=='customer') {
Site__c.RecordTypeId = recTypesMap.get('customer');
}

 

Best,

Ram

LavanyaLavanya

Hi Ram, thanks for the reply. But i am not the record type name in my custom object"Site". I have given the code  like this

 

 

//Getting record type
// query your record types for your custom object
List<RecordType> recTypes = [Select r.SobjectType, r.Name, r.Id, r.DeveloperName, r.Description From RecordType r where SobjectType = 'Site__c' and IsActive = true];
// load them into a map
Map<String, Id> recTypesMap = new Map<String, Id>();

for (RecordType recType : recTypes) {
recTypesMap.put(recType.DeveloperName, recType.Id);
}
//end of getting record type name

for (Integer i=1;i<filelines.size();i++)
{
String[] inputsitevalues = new String[]{};
inputsitevalues = filelines[i].split(',');

Site__c site= new Site__c();
for(account a: accstoupload)
{
site.Site_Acc__c = accstoupload[i-1].id;

}

//site.CompanyName__c = inputsitevalues[0];
if(site.Site_Acc__c!= null)
{

 

site.Name =inputsitevalues[20] ;

if (inputsitevalues[21]=='Cordell Housing') {
site.RecordTypeId = recTypesMap.get('Cordell Housing');
}

 

sitetoupload.add(site);
}
}
try
{
insert sitetoupload;
}
catch (Exception e3)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
ApexPages.addMessage(errormsg);
}
return null;

 

Kindly tell how resolve this. I tried  but can't able to overcome this.

 

             

ForcepowerForcepower

Lavanya,

 

I thought you had record types such as Customer, Partner etc. Do you have a record type for "Cordell Housing"?