I have a Purchase related list under a Custom Object. This related list has all the details of the purchases made Like "Name", "Amount","Purchase Date" etc..
This related list has a Custom button Upload. Once records are approved,when users clicks on the button, all the records will get updated to other Custom Object related list on the same Account layout.
So here I am able to insert every record in to the related. I want to summarize them instead of showing all records. For eg: In the Purchase related list if it has 4 records with same Name - ABC and Amount=300. Once the user upload. Only one record with ABC should get inserted with Amount 1200 .
Is it possible ?
Here is the part of code:
public ID AccId {get;set;}
public Purchase__c pur{get;set;}
//constructor
public className(ApexPages.StandardSetController controller){
AccId=ApexPages.currentPage().getParameters().get('id');
}
public PageReference addPurchase(){
List<ApprovedData__c> apd = new List<ApprovedData__c>();
List<Purchase__c> plist = [Select Id, Name,Amount__c,PurchaseDate__c,Company__c from Purchase__c where Account__C=:AccId];
for(Purchase__c pur: plist){
ApprovedData__c ap=new ApprovedData__c();
ap.PName__c=pur.Name;
ap.pAmount__c=pur.Amount__c;
apd.add(ap);
}
insert apd;
}
else{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'NO DATA FOUND');
ApexPages.addMessage(errormsg);
return null;
}
PurchaseDate__c
for records whose name is the same?