You need to sign in to do that
Don't have an account?
SFDC Dummy
Papulate all record in another object when trigger fire
Hi Friends
I have facing a challenge long time but i am not getting the solution.
I have two custom object.(object1 and object2).
object1 have some reocrd where in object2 there no record initially.when i am creating record or updating record it will be papulate all record of object1 to object2 .
like if object1 have recrod
1---100-----1/2/2010
2---200-----2/2/2010
3---300-----3/2/2010........when i am updating record on 1---100-----1/2/2010 to 1---500-----10/2/2010 on object1
it will be papulate in object2 like
1---500-----10/2/2010
2---200-----10/2/2010
3---300-----10/2/2010........means it will papulate all existing reccord with updated date field.if i am upadting again with same date it
will be updated on same record.if i am creating record with diffrent date means date not same as before it will be again papulate all existing field with updated date field..
like when i am updating record on 1---100-----10/2/2010 to 1---1000-----15/2/2010 on object1
it will be papulate in object2 like
1---1000-----15/2/2010
2---200-----15/2/2010
3---300-----15/2/2010......
i have created a trigger but it will be created or upadted only one record which i am creating.if i have 10 record in object1 it will be papulate all 10 record in
object2 when trigger fire...
I have facing a challenge long time but i am not getting the solution.
I have two custom object.(object1 and object2).
object1 have some reocrd where in object2 there no record initially.when i am creating record or updating record it will be papulate all record of object1 to object2 .
like if object1 have recrod
1---100-----1/2/2010
2---200-----2/2/2010
3---300-----3/2/2010........when i am updating record on 1---100-----1/2/2010 to 1---500-----10/2/2010 on object1
it will be papulate in object2 like
1---500-----10/2/2010
2---200-----10/2/2010
3---300-----10/2/2010........means it will papulate all existing reccord with updated date field.if i am upadting again with same date it
will be updated on same record.if i am creating record with diffrent date means date not same as before it will be again papulate all existing field with updated date field..
like when i am updating record on 1---100-----10/2/2010 to 1---1000-----15/2/2010 on object1
it will be papulate in object2 like
1---1000-----15/2/2010
2---200-----15/2/2010
3---300-----15/2/2010......
i have created a trigger but it will be created or upadted only one record which i am creating.if i have 10 record in object1 it will be papulate all 10 record in
object2 when trigger fire...
trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) { Set<Date> dateSet = new Set<Date>(); for(Debtors_Ledger__c debtor : trigger.new) { dateSet.add(debtor.DailyUpdate__c); } List<MasterCopy__c> allMasterData = new List<MasterCopy__c>([Select Name,Date__c,Group__c,Master_Code__c,Master_Name__c,Credit__c,Debit__c from MasterCopy__c where Date__c IN : dateSet]); Map<Date,MasterCopy__c> masterDataMap = new Map<Date,MasterCopy__c>(); for(MasterCopy__c masData : allMasterData){ masterDataMap.put(masData.Date__c,masData); } List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>(); MasterCopy__c master; for(Debtors_Ledger__c debtor : trigger.new){ if(masterDataMap.containsKey(debtor.DailyUpdate__c)){ masterDataMap.get(debtor.DailyUpdate__c).Master_Code__c= debtor.Name; // masterDataMap.get(debtor.DailyUpdate__c).Name + ' ' + masterDataMap.get(debtor.DailyUpdate__c).Credit__c= debtor.Credit_formula__c; masterDataMap.get(debtor.DailyUpdate__c).Debit__c= debtor.Debit_formula__c; masterDataMap.get(debtor.DailyUpdate__c).Group__c= debtor.Group__c; masterDataMap.get(debtor.DailyUpdate__c).Master_Name__c= debtor.Debtor_Name__c; masterDataMap.get(debtor.DailyUpdate__c).Date__c = debtor.DailyUpdate__c; } else{ master = new MasterCopy__c(); master.Master_Code__c= debtor.Name; master.Date__c = debtor.DailyUpdate__c; master.Master_Name__c= debtor.Debtor_Name__c; master.Credit__c= debtor.Credit_formula__c; master.Debit__c= debtor.Debit_formula__c; master.Group__c= debtor.Group__c; insertMasterList.add(master); } } insertMasterList.addAll(masterDataMap.values()); upsert insertMasterList; }
Please find your required code :
trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) {
List<Debtors_Ledger__c> allDebtorData = new List<Debtors_Ledger__c>([Select Name,DailyUpdate__c,Debtor_Name__c,Credit_formula__c,Debit_formula__c,Group__c from Debtors_Ledger__c]);
List<MasterCopy__c> allMasterData = new List<MasterCopy__c>([Select Name,Date__c,Group__c,Master_Code__c,Master_Name__c,Credit__c,Debit__c from MasterCopy__c]);
List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>();
MasterCopy__c newMaster;
Date updatedDate = trigger.new[0].DailyUpdate__c;
if(allMasterData.size() == 0){
for(Debtors_Ledger__c debtor : allDebtorData){
newMaster = new MasterCopy__c();
newMaster.Master_Code__c= debtor.Name;
newMaster.Date__c = updatedDate;
newMaster.Master_Name__c= debtor.Debtor_Name__c;
newMaster.Credit__c= debtor.Credit_formula__c;
newMaster.Debit__c= debtor.Debit_formula__c;
newMaster.Group__c= debtor.Group__c;
insertMasterList.add(master);
}
}
else {
if(trigger.isUpdate){
for(Debtors_Ledger__c debtor : trigger.new){
if(trigger.oldmap.get(debtor.id).DailyUpdate__c != debtor.DailyUpdate__c){
for(MasterCopy__c master : allMasterData){
master.Date__c = debtor.DailyUpdate__c;
}
}
}
}
if(trigger.isInsert){
for(Debtors_Ledger__c debtor : trigger.new){
if(allMasterData[0].Date__c != debtor.DailyUpdate__c){
for(MasterCopy__c master : allMasterData){
master.Date__c = debtor.DailyUpdate__c;
}
}
}
}
}
allMasterData.addAll(insertMasterList);
upsert allMasterData;
}
One more thing, If u still find any issues or any case that is not working than please explain the case which is not working properly and also the case which is working fine so that i can have a close look and will update the code accordingly.
Regards,
Abhishek.
All Answers
Please explain me form where the records have created in Object2 if there were no records initially.
According to your requirement there will be only a single record in object2 which will be updated each and every time when a new object1 record is created or updated.
Please clear out your requirement in a better way.
1. Initially when there are no records on Object2 and Any record of object1 is changed (only date is changed) than you will create 10 records in object 2 and all 10 records of obejct will have same date.
2. Now both object1 and object2 have same no. of records i.e. 10. If i again change the data value in any record of object 1 than again 10 records will be created in obejct 2 with the changed date. That means that obejct 1 still have 10 records and object 2 have 20 records.
Now please clear following points :
1. What should be done when object2 have 10 records and Any record of obejct1 is updated but date is not changed.
2. What happen in the case of insertion. Please explain all cases like:
- When there are no records in object2 and a new object1 record is inserted ?
- When Object2 have 10 records than a new record with same date is inserted ?
- When Object2 have 10 records than a new record with different date is inserted ?
Please clear above queries.Regards,
Abhishek.
Please find your required code :
trigger UpsertMasterNew on Debtors_Ledger__c(after insert,after update) {
List<Debtors_Ledger__c> allDebtorData = new List<Debtors_Ledger__c>([Select Name,DailyUpdate__c,Debtor_Name__c,Credit_formula__c,Debit_formula__c,Group__c from Debtors_Ledger__c]);
List<MasterCopy__c> allMasterData = new List<MasterCopy__c>([Select Name,Date__c,Group__c,Master_Code__c,Master_Name__c,Credit__c,Debit__c from MasterCopy__c]);
List<MasterCopy__c> insertMasterList = new List<MasterCopy__c>();
MasterCopy__c newMaster;
Date updatedDate = trigger.new[0].DailyUpdate__c;
if(allMasterData.size() == 0){
for(Debtors_Ledger__c debtor : allDebtorData){
newMaster = new MasterCopy__c();
newMaster.Master_Code__c= debtor.Name;
newMaster.Date__c = updatedDate;
newMaster.Master_Name__c= debtor.Debtor_Name__c;
newMaster.Credit__c= debtor.Credit_formula__c;
newMaster.Debit__c= debtor.Debit_formula__c;
newMaster.Group__c= debtor.Group__c;
insertMasterList.add(master);
}
}
else {
if(trigger.isUpdate){
for(Debtors_Ledger__c debtor : trigger.new){
if(trigger.oldmap.get(debtor.id).DailyUpdate__c != debtor.DailyUpdate__c){
for(MasterCopy__c master : allMasterData){
master.Date__c = debtor.DailyUpdate__c;
}
}
}
}
if(trigger.isInsert){
for(Debtors_Ledger__c debtor : trigger.new){
if(allMasterData[0].Date__c != debtor.DailyUpdate__c){
for(MasterCopy__c master : allMasterData){
master.Date__c = debtor.DailyUpdate__c;
}
}
}
}
}
allMasterData.addAll(insertMasterList);
upsert allMasterData;
}
One more thing, If u still find any issues or any case that is not working than please explain the case which is not working properly and also the case which is working fine so that i can have a close look and will update the code accordingly.
Regards,
Abhishek.