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
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12 

I have Created an account search page & also Create new Record if search fail, i t working fine, now i want to search it and create it with particular Account Record Type{var,ciz}, can anyone help where i need to Modify my code?

Controller class:
public with sharing class AccountPageSearchController
{
    public String SearchField1{get;set;}
    public String SearchField2{get;set;}
    public String SearchField3{get;set;}
    public String SearchField4{get;set;}
    public Boolean CreateNewAccount{get;set;}
    public List<Account> AccountResult{get;set;}
    public String ContractQueryString{get;set;}
    public String AccountQueryString{get;set;}
    //public RecordType RTID;
    public string RecordType;
  
    public AccountPageSearchController(ApexPages.StandardController controller)
    {
        CreateNewAccount = false;
        AccountResult = new List<Account>();
        SearchField1='';
        SearchField2='';
        SearchField3='';
        SearchField4='';
    }
   
public list<selectOption> getRecordTypes{
    get{
    if(getRecordTypes == null)
    {

             String selectedRecordType ;
            getRecordTypes = new list<selectOption>();
            Schema.DescribeSObjectResult d = Schema.SObjectType.Account;
            Map<Id,Schema.RecordTypeInfo> rtMapById = d.getRecordTypeInfosById();
            for(RecordType rt:[SELECT Id,Name FROM RecordType WHERE SobjectType='Account'])
            {
            Schema.RecordTypeInfo rtInfo =  rtMapById.get(rt.id);
            if(rtInfo.isAvailable())
            {
            getRecordTypes.add(new selectOption(rtInfo.getRecordTypeId(),rtInfo.getName()));
             if(rtInfo.isDefaultRecordTypeMapping())
             {
             selectedRecordType = rtInfo.getRecordTypeId();
             }
                }
            }
        }
             return getRecordTypes;
    }
    private set;
    }

    public pagereference searchit()
    {
        AccountQueryString='Select id,name,(select id,name,StageName from opportunities) from account where';
        String ContractQueryString='Select id,name,AccountId from contact where';
        String IdString='';
        List<id> AccId = new List<id>();
        if(SearchField2!='' && SearchField2!=null && SearchField2.length() >2)
            ContractQueryString += ' name LIKE \''+ SearchField2 +'%\'' +' and';
        if(SearchField3!='' && SearchField3!=null && SearchField3.length() >2)
            ContractQueryString += ' email = \''+ SearchField3 +'\' and';
        if(SearchField4!='' && SearchField4!=null && SearchField4.length() >2)
            ContractQueryString += ' phone= \''+ SearchField4 +'\'';
        else
            ContractQueryString=ContractQueryString.removeend(' and');
        if(SearchField1 != '' && SearchField1 != null)
            AccountQueryString += ' name LIKE \''+SearchField1+'%\'' +' and';
        system.debug('################'+ContractQueryString);
        if(SearchField2!='' || SearchField3!='' || SearchField4!='')
        {
            for(Contact Ctemp: database.query(ContractQueryString))
            {
                AccId.add(Ctemp.AccountId);
        
            }
            if(!AccId.isEmpty())
            {
                for(String Aid :AccId)
                    if(Aid !=null)
                        IdString += '\''+Aid+'\',';
                    else
                        continue;
                if(IdString == '' )
                    IdString ='\'\'';
            }
            else
                IdString ='\'\'';
            IdString = IdString.removeend(',');
                AccountQueryString +=  ' id IN('+IdString+')';
           
        }
        else if(SearchField1 == '' || SearchField1 == null)
            return null;
        else
            AccountQueryString = AccountQueryString.removeend(' and');
        System.debug(ContractQueryString+'***************'+AccountQueryString);
       
        System.debug('***************'+AccountQueryString);
        AccountResult = new List<Account>((List<Account>)database.query(AccountQueryString));
        if(AccountResult.isEmpty())
            CreateNewAccount=true;
        else
            CreateNewAccount=false;
        return null;
    }

}

Vf Page:

<apex:page standardController="Account" extensions="AccountPageSearchController" >
    <apex:outputPanel >
        <apex:form >
            <apex:pageBlock >
               
                Account Name  <apex:inputText value="{!SearchField1}"  />
                Contact Name  <apex:inputText value="{!SearchField2}" />
                Contact Email  <apex:inputText value="{!SearchField3}" />
                Contact Number  <apex:inputText value="{!SearchField4}" />
                {!ContractQueryString}
                <apex:commandButton value="Search" action="{!searchit}"/>
            </apex:pageBlock>
            <apex:pageBlock rendered="{!IF(AccountResult.size > 0,true,false)}" >
                <apex:pageBlockTable var="Acc" value="{!AccountResult}">
                    <apex:column width="10%">
                        <apex:outputLink value="/{!Acc.id}/e?retURL={!Acc.id}">Edit</apex:outputLink>
                    </apex:column>
                    <apex:column value="{!Acc.Name}" width="30%" />
                    <apex:column >

                    <apex:pageBlockTable var="Opp" value="{!Acc.Opportunities}" rendered="{!IF(Acc.Opportunities.size >0,true,false)}">
                        <apex:column width="10%">
                            <apex:outputLink value="/{!Opp.id}/e?retURL={!Opp.id}">Edit</apex:outputLink>
                        </apex:column>
                        <apex:column value="{!Opp.Name}"/>
                    </apex:pageBlockTable>
                    <apex:outputLink value="/setup/ui/recordtypeselect.jsp?ent=Opportunity&retURL=/apex/SOSL&save_new_url=%2F006%2Fe%3FretURL%3D%252F006%252Fo" rendered="{!IF(Acc.Opportunities.size == 0,true,false)}">Create Opportunity</apex:outputLink>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
            <apex:pageBlock rendered="{!CreateNewAccount}" >
                <apex:pageBlockSection >
                    <apex:inputField value="{!Account.Name}"/>
                    <apex:inputField value="{!Account.Business_Type__c}"/>
                    <apex:inputField value="{!Account.Email__c}"/>
                   
                </apex:pageBlockSection>
                <apex:pageBlockButtons >
                        <apex:commandButton value="Save" action="{!Save}"/>
                    </apex:pageBlockButtons>
            </apex:pageBlock>
        </apex:form>
    </apex:outputPanel>
</apex:page>
Ramu_SFDCRamu_SFDC
Currently I see that you are using standard controller for account and there is no field on visualforce page that has record type information. In that case, you would need to create a custom save method and pass the inputfield values to that method and save it with any record type of your choice.
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12
Hi , my record type is of {viz, & var}, i am not getting where to code, can you help me out?