visual force page
<apex:page controller="IMScitycrudoperation">
<apex:form>
<!-- Insert Section -->
<apex:pageBlock title="Insert Record">
<apex:inputText value="{!newRecord.Name}" />
<apex:commandButton value="Insert" action="{!insertRecord}" rerender="recordsTable" />
</apex:pageBlock>
<!-- Update Section -->
<apex:pageBlock title="Update Record">
<apex:pageBlockTable value="{!customObjectRecords}" var="record">
<apex:column headerValue="Name">
<apex:inputText value="{!record.Name}" />
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Update" action="{!updateRecord}" rerender="recordsTable">
<apex:param name="recordId" value="{!record.Id}" assignTo="{!recordIdToUpdate}" />
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<!-- Delete Section -->
<apex:pageBlock title="Delete Record">
<apex:pageBlockTable value="{!customObjectRecords}" var="record">
<apex:column headerValue="Name">
{!record.Name}
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!deleteRecord}" rerender="recordsTable">
<apex:param name="recordId" value="{!record.Id}" assignTo="{!recordIdToDelete}" />
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex class
public class IMScitycrudoperation {
public City__c newRecord { get; set; }
public List<City__c> customObjectRecords { get; set; }
public Id recordIdToUpdate { get; set; }
public Id recordIdToDelete { get; set; }
public IMScitycrudoperation() {
newRecord = new City__c();
customObjectRecords = [SELECT Id, Name FROM City__c];
}
public PageReference insertRecord() {
insert newRecord;
newRecord = new City__c(); // Clear the input fields after insert
return null; // You can navigate to a different page if needed.
}
public PageReference updateRecord() {
City__c recordToUpdate = [SELECT Id, Name FROM City__c WHERE Id = :recordIdToUpdate];
update recordToUpdate;
return null; // You can navigate to a different page if needed.
}
public PageReference deleteRecord() {
City__c recordToDelete = [SELECT Id FROM City__c WHERE Id = :recordIdToDelete];
delete recordToDelete;
return null; // You can navigate to a different page if needed.
}
}