You need to sign in to do that
Don't have an account?
domdick
Help on apex class for parent-child
Hello,
I am trying to insert 2 parents and multiple parent's child records but it ended with an error "cannot specify Id in an insert call".
Can anyone add some inputs?
Thanks!
I am trying to insert 2 parents and multiple parent's child records but it ended with an error "cannot specify Id in an insert call".
Can anyone add some inputs?
Thanks!
public class Notes{ public Id scId { get; set; } public Id newScId { get; set; } public Date newScDate { get; set; } public Date firstDayScDate { get; set; } private Subscription__c sc { get; set; } public Notes(ApexPages.StandardController controller) { newScId = ApexPages.currentPage().getParameters().get('newSc'); newScDate = Date.valueOf(ApexPages.currentPage().getParameters().get('newScDate')); } public PageReference saveNotes() { Savepoint spCredit = Database.setSavepoint(); try{ /******* the following query is returning two records *********/ for(Parent__c pc : [Select Id, Name, Quantity, Price__c, Discount from Parent__c Where Subscription__c =: newScId]) { Parent__c newPc = pc.clone(false); newPc.Subscription__c = newScId; newPc.ServiceDate = Date.today(); insert newPc; // Lets insert Parent's children // the following query is returning multiple records. List<Childs__c> seChilds = new List<Childs__c>(); List<Childs__c> getChilds = [Select Id, Type__c, Quantity, ExpiaryDate From Childs__c Where Parent__c =: pc.Id]; for(Childs__c ChildsSch : getChilds) { Childs__c newChilds = ChildsSch.clone(false); newChilds.Parent__c = newPc.Id; newChilds.Type__c = 'Rollup'; seChilds.add(newChilds); } insert seChilds; } } catch (Exception ex) { Database.rollback(spCredit); ApexPages.addMessages(ex); return null; } return new PageReference('/'); } }
As of now everything looks good to me.. except custom fields mentioned in SQL query without using"__c" as suffix.
try with below code.. hope this should sovle your problem:
try{
/******* the following query is returning two records *********/
for(Parent__c pc : [Select Id, Name, Quantity__c, Price__c, Discount__c from Parent__c Where Subscription__c =: newScId]) {
Parent__c newPc = pc.clone(false);
newPc.Subscription__c = newScId;
newPc.ServiceDate__c = Date.today();
insert newPc;
// Lets insert Parent's children
// the following query is returning multiple records.
List<Childs__c> seChilds = new List<Childs__c>();
List<Childs__c> getChilds = [Select Id, Type__c, Quantity__c, ExpiaryDate__c From Childs__c Where Parent__c =: pc.Id];
for(Childs__c ChildsSch : getChilds) {
Childs__c newChilds = ChildsSch.clone(false);
newChilds.Parent__c = newPc.Id;
newChilds.Type__c = 'Rollup';
seChilds.add(newChilds);
}
insert seChilds;
}
} catch (Exception ex) {
Database.rollback(spCredit);
ApexPages.addMessages(ex);
return null;
}
public class Notes{
public Id scId { get; set; }
public Id newScId { get; set; }
public Date newScDate { get; set; }
public Date firstDayScDate { get; set; }
private Subscription__c sc { get; set; }
public Notes(ApexPages.StandardController controller) {
newScId = ApexPages.currentPage().getParameters().get('newSc');
newScDate = Date.valueOf(ApexPages.currentPage().getParameters().get('newScDate'));
}
public PageReference saveNotes() {
Savepoint spCredit = Database.setSavepoint();
try{
/******* the following query is returning two records *********/
for(Parent__c pc : [Select Id, Name, Quantity, Price__c, Discount from Parent__c Where Subscription__c =: newScId]) {
Parent__c new Pc=new Parent__c(); // instantiate new parent__c object
newPc = pc.clone(false);
newPc.Subscription__c = newScId;
newPc.ServiceDate = Date.today();
insert newPc;
// Lets insert Parent's children
// the following query is returning multiple records.
List<Childs__c> seChilds = new List<Childs__c>();
List<Childs__c> getChilds = [Select Id, Type__c, Quantity, ExpiaryDate From Childs__c Where Parent__c =: pc.Id];
for(Childs__c ChildsSch : getChilds) {
Childs__c newChilds = ChildsSch.clone(false);
newChilds.Parent__c = newPc.Id;
newChilds.Type__c = 'Rollup';
seChilds.add(newChilds);
}
insert seChilds;
}
} catch (Exception ex) {
Database.rollback(spCredit);
ApexPages.addMessages(ex);
return null;
}
return new PageReference('/');
}
}
** Also try moving the SOQL query out of the for loop as it might hit govenor limit exception