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
Venky5599Venky5599 

Editing the Account using the check box

Hi all 

 

I want to Edit the Account by using the Check Box .

 

 

here is my visual force page 

 

<apex:page controller="CustomAccountLookupController2"
title="Search"
showHeader="false"
sideBar="false"
tabStyle="Account"
id="pg">

<script>
function validateandclose(name,RecId){

if(document.getElementById("RecordContent")){
var errorString = document.getElementById("RecordContent").innerHTML;
if(errorString.indexOf('Error:')==-1){//alert("in Validate");
fillIn(name,RecId);
}
}

}
function fillIn(name,id)
{
var winMain=window.opener;
if (null==winMain)
{
winMain=window.parent.opener;
}

var ele=winMain.document.getElementById('{!$CurrentPage.parameters.txt}');
//alert(id + '####' + ele);
ele.value=name;
var idval = '{!$CurrentPage.parameters.txt}' + '_lkid';

ele=winMain.document.getElementById(idval);
//alert(ele)
ele.value=id;

CloseWindow();
}
function CloseWindow()
{
var winMain=window.opener;
if (winMain != null) {
window.close();
if (null==winMain)
{
winMain=window.parent.opener;
}
//winMain.closeLookupPopup();
}

}
</script>


<apex:form id="f">
<apex:outputPanel id="page" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
<apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">

<!-- SEARCH TAB -->
<apex:tab label="Search" name="tab1" id="tabOne">

<apex:actionRegion >
<apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
<apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
<apex:inputText id="txtSearch" value="{!searchString}" />
<span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
</apex:outputPanel>

<apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
<apex:pageBlock id="searchResults">
<apex:pageBlockButtons>
<apex:commandButton value="Edit"/>
<apex:commandButton value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!results}" var="a" id="tblResults">
<apex:column >

<apex:inputCheckbox/>


</apex:column>
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Name</apex:outputPanel>
</apex:facet>
<apex:outputLink value="javascript&colon;top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>
</apex:column>
<apex:column >
<apex:facet name="header">
<apex:outputPanel >Business AddressLine1</apex:outputPanel>
</apex:facet> {!a.Main_Address_Line1__c}
</apex:column>
<apex:column>
<apex:facet name="header">
<apex:outputPanel>Business AddressLine2</apex:outputPanel>
</apex:facet>{!a.Main_Address_Line2__c}
</apex:column>
<apex:column>
<apex:facet name="header">
<apex:outputPanel>Business AddressLine3</apex:outputPanel>
</apex:facet> {!a.Main_Address_Line3__c}
</apex:column>
<apex:column>
<apex:facet name="header">
<apex:outputPanel>City</apex:outputPanel>
</apex:facet> {!a.Main_City__c}
</apex:column>
<apex:column>
<apex:facet name="header">
<apex:outputPanel>State</apex:outputPanel>
</apex:facet>{!a.Main_State__c}
</apex:column>
<apex:column>
<apex:facet name="header">
<apex:outputPanel>Country</apex:outputPanel>
</apex:facet>{!a.Main_Country__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:actionRegion>

</apex:tab>

<!-- NEW ACCOUNT TAB -->
<apex:tab label="New Account" name="tab2" id="tabTwo">

<apex:pageBlock id="newAccount" title="New Account" >
<div id="RecordContent">
<apex:pageBlockButtons >
<!--<apex:commandButton action="{!saveAccount}" value="Save" />-->
<apex:commandButton action="{!saveAccount}" value="Save" oncomplete="validateandclose('{!insertedRecName}','{!insertedRecId}')" reRender="newAccount"/>

</apex:pageBlockButtons>
<apex:pageMessages />

<apex:pageBlockSection columns="2" id="pbs">
<apex:inputField value="{!Account.Name}" required="true" id="aname" taborderhint="1"/>
<apex:inputField value="{!Account.Type}" id="type" taborderhint="6"/>
<apex:inputField value="{!Account.Main_Address_Line1__c}" id="line1" taborderhint="2"/>
<apex:inputField value="{!Account.Main_City__c}" id="city" taborderhint="7"/>
<apex:inputField value="{!Account.Main_Address_Line2__c}" id="line2" taborderhint="3"/>
<apex:inputField value="{!Account.Main_State__c}" id="state" taborderhint="8"/>

<apex:inputField value="{!Account.Main_Address_Line3__c}" id="line3" taborderhint="4"/>
<apex:inputField value="{!Account.Main_Country__c}" id="cntry" taborderhint="9"/>
<apex:inputField value="{!Account.Business_Zip_Code__c}" id="code" taborderhint="5"/>
<apex:inputField value="{!Account.Region__c}" id="reg" taborderhint="10"/>


<!-- <apex:repeat value="{!$ObjectType.Account.FieldSets.CustomAccountLookup}" var="f">

<apex:inputField value="{!Account[f]}"/>
</apex:repeat>-->
</apex:pageBlockSection>
</div>
</apex:pageBlock>

</apex:tab>
</apex:tabPanel>
</apex:outputPanel>
</apex:form>
</apex:page>

 

Controller 

 

public with sharing class CustomAccountLookupController2 {

public Account account {get;set;} // new account to create
public List<Account> results{get;set;} // search results
public string searchString{get;set;} // search keyword
public String insertedRecId{get;set;}//inserted Record Id
public String insertedRecName{get;set;}//inserted Record Name

public CustomAccountLookupController2() {
account = new Account();
// get the current search string
searchString = System.currentPageReference().getParameters().get('lksrch');
runSearch();
}

// performs the keyword search
public PageReference search() {
runSearch();
return null;
}

// prepare the query and issue the search command
private void runSearch() {
// TODO prepare query string for complex serarches & prevent injections
results = performSearch(searchString);
}

// run the search and return the records found.
private List<Account> performSearch(string searchString) {

String soql = 'select id, name , type ,Main_Address_Line1__c,Main_Address_Line2__c,Main_Address_Line3__c,Main_City__c,Main_Country__c,Main_State__c,Business_Zip_Code__c from account';
if(searchString != '' && searchString != null)
soql = soql + ' where name LIKE \'%' + searchString +'%\'' ;
soql = soql + ' limit 25';
System.debug(soql);
return database.query(soql);

}

// save the new account record
public PageReference saveAccount() {

// save the new contactrecord
if(account.name<>''){
insert account;
List<sObject> Resultlist = new List<sObject>();
if(account.Id!=null){
string soql = 'select Name from Account where Id = \'' + account.Id + '\'';
Resultlist = database.query(soql); // Required ResultList.
for(sObject sObj : Resultlist){
insertedRecName= (String) sObj.get('name');
insertedRecId = (String) sObj.get('id');

}
}
// reset the contact
account= new Account ();
return null;} else{return null;}
}


// used by the visualforce page to send the link to the right dom element
public string getFormTag() {
return System.currentPageReference().getParameters().get('frm');
}

// used by the visualforce page to send the link to the right dom element for the text box
public string getTextBox() {
return System.currentPageReference().getParameters().get('txt');
}

}my require ment is when i click on the check box and click on the Edit Button i has to check how many Conatct are there under this Account if Contacts are more than one i has to thrown Error like 'Do not edit this Account'. if it is having only one Accoutn then user can Edit the Account.

nick1505nick1505

Hi,

You will need to take all the code below form in one outputPanel and the PageMessage in one outputPanel
Something like this:
Page :
<apex:page>
<apex:form>
<apex:outputPanel rendered="{!!showAccount}">
<apex:pageMessage summary="Do not edit this Account" severity="warning" strength="3"/>
</apex:outputPanel>

<apex:outputPanel rendered="{!showAccount}">
//Your existing code...
</apex:outputPanel>

</apex:form>
<apex:page>

Controller:

You will need to check for the count of Contacts related to the Account in constructor itself when yuo are in Edit case:
create a new Boolean Variable:
public Boolean showAccount {get;set;}

assign showAccount = true if count of Contact is 1
else assign showAccount = false.

Let me know if this work's for you!

Thanks,
nicks1505
Certified Developer