You need to sign in to do that
Don't have an account?
Sam Kapoor
Sequence number of child records during insert or update operation
Hi Everyone,
Plz tell me how to add child records in sequence.Here is the code.Suppose in 3 contact associated with account (Ex Contact name Contact0,Contact1,Contact2).On Account field Number of Location field value is 3.if i change to 6 then (Contact0, Contact1,Contact3 ) inserting again.I need to add Contact4,Contact5,Contact 6 while Updating.
Here is the code:-
trigger ContactCreation on Account (after insert,after update) {
List<Contact> conList = new List<Contact>();
Map<Id,decimal> mapAcc = new Map<Id,decimal>();
for(Account a : trigger.new){
mapAcc.Put(a.Id,a.NumberofLocations__c);
}
if(mapAcc.size() > 0 && mapAcc != null){
for(Account acc : trigger.new){
for(Integer i=0;i<mapAcc.get(acc.id);i++){
Contact con = new Contact();
con.AccountId = acc.Id;
con.LastName = 'Contact'+i;
conList.add(con);
}
}
}
if(conList.size() > 0 && conList != null){
insert conList;
}
List<Contact> listcon = new List<Contact>([Select Id,Name From Contact]);
for(Contact c : listCon){
}
}
Thanks in Advance !!
Plz tell me how to add child records in sequence.Here is the code.Suppose in 3 contact associated with account (Ex Contact name Contact0,Contact1,Contact2).On Account field Number of Location field value is 3.if i change to 6 then (Contact0, Contact1,Contact3 ) inserting again.I need to add Contact4,Contact5,Contact 6 while Updating.
Here is the code:-
trigger ContactCreation on Account (after insert,after update) {
List<Contact> conList = new List<Contact>();
Map<Id,decimal> mapAcc = new Map<Id,decimal>();
for(Account a : trigger.new){
mapAcc.Put(a.Id,a.NumberofLocations__c);
}
if(mapAcc.size() > 0 && mapAcc != null){
for(Account acc : trigger.new){
for(Integer i=0;i<mapAcc.get(acc.id);i++){
Contact con = new Contact();
con.AccountId = acc.Id;
con.LastName = 'Contact'+i;
conList.add(con);
}
}
}
if(conList.size() > 0 && conList != null){
insert conList;
}
List<Contact> listcon = new List<Contact>([Select Id,Name From Contact]);
for(Contact c : listCon){
}
}
Thanks in Advance !!
Please find the code below. To achieve your solution we have to take one additional field on contact "auto_creation_no__c" of type number(18,0). Below logic will work for creating contacts as well as deletion of contacts. If you update Account.NumberofLocations__c field's value from 17 to 15 below code will delete contact15 and contact16.
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Regards,
Kapil
(forcecube@gmail.com)
All Answers
You need to check whether the Id is present in Map using ContainsKey. The code goes like below.
Let me know if this helps. Mark this as your best answer if this solves to help others.
Regards,
Avishek
Please find the code below. To achieve your solution we have to take one additional field on contact "auto_creation_no__c" of type number(18,0). Below logic will work for creating contacts as well as deletion of contacts. If you update Account.NumberofLocations__c field's value from 17 to 15 below code will delete contact15 and contact16.
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Regards,
Kapil
(forcecube@gmail.com)