You need to sign in to do that
Don't have an account?

how to delete selected records
hi group,
can any one help me to solve my error i.e, i want to delete selected records in my pageblock table.
pls check my below code and correct it .......
-----------------------------------------------------------------------------------------------------------------------------------------------------
<apex:page tabStyle="Campaign__c" controller="Camp" showHeader="false">
<script language="javascript">
<apex:includeScript value="/soap/ajax/15.0/connection.js"/>
<apex:includeScript value="/soap/ajax/15.0/apex.js"/>
var idsToDelete = {!GETRECORDIDS( $ObjectType.Campaign__c)};
var deleteWarning= 'Are you sure you wish to delete ' +idsToDelete.length+ ' Records?';
if(idsToDelete.length && (window.confirm(deleteWarning))) {
sforce.connection.deleteIds(idsToDelete,function(){
navigateToUrl(window.location.href);
});
}
else if (idsToDelete.length == 0){
alert("Please select the contacts you wish to delete.!!");
}
</script>
<!-- Javascript function to check all rows in the table -->
<script>
function checkAll(cb){
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++){
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>
<!-- End of Javascript function -->
<apex:form >
<apex:pageMessages ></apex:pageMessages>
<apex:sectionHeader title="Campaign"/>
<apex:pageBlock title="RecentCampaigns" rendered="{!RecentCampaignforms}">
<apex:pageBlockButtons location="top">
<div>
<left>
<apex:commandButton value="New" action="{!campaignnew}"/>
<apex:commandButton value="Delete Selected" action="{!deleteselected}"/>
</left>
</div>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!CampaignList}" var="c" >
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!deleteselected}" onsubmit="checkAll(this)" reRender="Campaign__c" />
</apex:inputCheckbox>
</apex:facet>
<apex:inputCheckbox id="checkedone" >
<apex:actionSupport event="onclick" action="{!deleteselected}" reRender="Campaign__c"/>
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Action">
<apex:commandLink value="Edit" action="{!Edit}" id="edit"/>
<!--<apex:outputLink value="/{!c.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold" >Edit</apex:outputLink>-->
<!--<a href="javascript:if (window.confirm('Are you sure?')) Deletecampaign('{!cl.Id}');" style="font-weight:bold" >Del</a>-->
</apex:column>
<apex:column headerValue="Campaign Name">
<apex:outputField value="{!c.Name}"/>
</apex:column>
<apex:column headerValue="Adress">
<apex:outputField value="{!c.Address__c}"/>
</apex:column>
<apex:column headerValue="Status">
<apex:outputField value="{!c.Status__c}"/>
</apex:column>
<apex:column headerValue="Start Date">
<apex:outputField value="{!c.Start_date__c}"/>
</apex:column>
<apex:column headerValue="End Date">
<apex:outputField value="{!c.End_date__c}"/>
</apex:column>
<apex:column headerValue="Campaign Budget">
<apex:outputField value="{!c.Campaign_budget__c}"/>
</apex:column>
<apex:column headerValue="Type">
<apex:outputField value="{!c.Type__c}"/>
</apex:column>
<apex:column headerValue="Campaign Duration">
<apex:outputField value="{!c.Campaign_Duration__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Campaign Edit" rendered="{!Newcampaignsform}" mode="Edit">
<apex:pageBlockButtons >
<apex:commandButton value="save" action="{!save}"/>
<apex:commandButton value="save&new" action="{!quicksave}"/>
<apex:commandButton value="cancel" action="{!cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information" columns="1" >
<apex:inputField value="{!cmp.Name}" required="true"/>
<apex:inputField value="{!cmp.Address__c}"/>
<apex:inputField value="{!cmp.Status__c}"/>
<apex:inputField value="{!cmp.Start_date__c}"/>
<apex:inputField value="{!cmp.End_date__c}"/>
<apex:inputField value="{!cmp.Campaign_budget__c}"/>
<apex:inputField value="{!cmp.Type__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
---------------------------------------------------------------------------------------------------------------------------------------------------
public class Camp {
public boolean RecentCampaignforms{get;set;}
public boolean Newcampaignsform{set;get;}
public list<Campaign__c> dellist;
public Campaign__c cmp{get;set;}
public boolean selected{set;get;}
public list<Campaign__C> edit;
public Camp (){
RecentCampaignforms = true;
Newcampaignsform = false;
getCampaignList();
}
public void Campaignnew(){
RecentCampaignforms = false;
Newcampaignsform = true;
cmp = new Campaign__c();
}
Public pageReference Edit(){
cmp = new Campaign__c();
pagereference pageref = new pagereference('https://c.ap1.visual.force.com/apex/camp'); //acc.id is the current record for update
pageref.setredirect(true);
return pageref;
}
public void Save(){
insert cmp;
RecentCampaignforms=true;
NewCampaignsForm=false;
}
public void QuickSave(){
insert cmp;
RecentCampaignforms=false;
NewCampaignsForm=true;
}
public void Cancel(){
RecentCampaignforms=true;
NewCampaignsForm=false;
}
//-----------Code for RecentCampaigns
public list<Campaign__c> CampaignList = new list<Campaign__c>();
public list<Campaign__c> getCampaignList(){
CampaignList=[SELECT Name,Address__c,Campaign_budget__c,Campaign_Duration__c,End_date__c,Start_date__c,Status__c,Type__c FROM Campaign__c];
return CampaignList;
}
//--- code for delete
public list<cCampaign> cmpList {get;set;}
public List<cCampaign> getcampaigns() {
if(cmpList == null) {
cmpList = new List<cCampaign>();
for(Campaign__c c: [SELECT Name,Address__c,Campaign_budget__c,Campaign_Duration__c,End_date__c,Start_date__c,Status__c,Type__c FROM Campaign__c]) {
cmpList.add(new cCampaign(c));
}
}
return cmpList;
}
public PageReference deleteselected() {
return null;
}
/*List<Campaign__c> selectedcampaigns = new List<Campaign__c>();
for(cCampaign cCam: getCampaigns()) {
if(cCam.selected == true) {
selectedcampaigns.add(cCam.cam);
dellist = [SELECT Name,Address__c,Campaign_budget__c,Campaign_Duration__c,End_date__c,Start_date__c,Status__c,Type__c FROM Campaign__c];
delete dellist;
}
}
System.debug('These are the selected records...');
for(Campaign__c cam: selectedcampaigns) {
system.debug(cam);
return null;
}*/
public class cCampaign {
public Campaign__c cam {get; set;}
public Boolean selected {get; set;}
public cCampaign(Campaign__c c) {
cam = c;
selected =false;
}
}
/*public PageReference deleteselected() {
dellist = [SELECT Name,Address__c,Campaign_budget__c,Campaign_Duration__c,End_date__c,Start_date__c,Status__c,Type__c FROM Campaign__c];
// delete dellist;
PageReference d1 = new PageReference('https://c.ap1.visual.force.com/apex/camp');
return d1;
}*/
}
---------------------------------------------------------------------------------------------------------------------------------------------------
thanks for giving reply(in advance)
Hi,
Please let me know the error you are getting.
Regards,
Sridhar Bonagiri
hi Sridhar,
thanks for giving reply,
when i click 'Delete Selected' button nothing is happening in my page.
so i came to know that hav to write action method in wrapper class....
i am not able to write it....... But my requirement is when i click the check box asscociated with record that one only should delete.........
hi sridhar,
/*List<Campaign__c> selectedcampaigns = new List<Campaign__c>();
for(cCampaign cCam: getCampaigns()) {
if(cCam.selected == true) {
selectedcampaigns.add(cCam.cam);
dellist = [SELECT Name,Address__c,Campaign_budget__c,Campaign_Durati
on__c,End_date__c,Start_date__c,Status__c,Type__c FROM Campaign__c];
delete dellist;
}
}
System.debug('These are the selected records...');
for(Campaign__c cam: selectedcampaigns) {
system.debug(cam);
return null;
}*/
i think in this area i hav to make changes to achieve my task..........
need ur help
thanks and regards
Ravi varma
You should never put SOQL or DML in a loop statement for starters.
Your next issue is that you are choosing to delete every campaign record in your org.
DML should be used as if you were passing it records to delete. It won't understand what to delete otherwise.
For instance, you need to loop through your campaigns and decide what you want to delete. Add each campaign you need to delete into a list of it's own. Then use the DML on that list to delete those selected records.
Hi,
I agree with admintrmp, you should not include DML and SOQL statements in side for loop , as these can hit governor limits and against SFDC coding best practices.
Regards,
Sridhar Bonagiri