You need to sign in to do that
Don't have an account?
louisa barrett 7
Deleting related objects in a trigger
I have a custom object(Opportunity_Summaries__c) which is related to an Opportunity. When the Opportunity is saved or updated I create 4 instances of this object. On the trigger I want to ensure that I delete all the custom object instances first, adn then re-cerate them if necessary. I want to futrure proof it just incase one of the records get accidently deleted etc. as I always need the 4 records.
My issue is whe I delete the existing Opportunity_Summaries__c records, the list still has 4 in, so they are never being re-created if I'm deleting them first off.
Can anyone suggest a way I get refresh the list without hitting the database again.
Any guidance would be appreciated.
Trigger:
trigger InsertOpportunitySummaries on Opportunity (after insert, after update) {
List<Opportunity_Summary__C> oppSummariesToInsert = new List<Opportunity_Summary__C>();
Map<id, List<Opportunity_Summary__C>> mapOfOppSummaries = new Map<id, List<Opportunity_Summary__C>>();
Set<id> oppIDs = new set<id>();
oppIDs = trigger.newMap.KeySet();
List<Opportunity> associatedOppSummaries = [SELECT id, (SELECT id, Name, Family_Description__C, Family_Code__c, Value__c FROM Opportunities_Summaries__r) FROM Opportunity WHERE id IN: oppIDs];
For(Opportunity opp:associatedOppSummaries){
mapOfOppSummaries.put(opp.id,opp.Opportunities_Summaries__r);
}
system.debug('Start');
system.debug('MapOfOppSummaries is empty = '+ mapOfOppSummaries.isEmpty());
if(Trigger.IsAfter)
{
for(Opportunity opp:trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() != 0){
for (Opportunity_Summary__c os:linkedOS){
system.debug('Deleted Linked OS ID = ' + os.Id);
system.debug('Deleted Linked OS Family = ' + os.Family_Description__c);
system.debug('Deleted Linked OS Family Code = ' + os.Family_Code__c);
system.debug('Deleted Linked OS Value = ' + os.Value__c);
database.delete(os, false);
}
}
}
system.debug('Is after');
if(trigger.IsInsert){
system.debug('Is insert');
for(Opportunity opp: Trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() == 0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('I-OS1 name =' + os1.Name);
system.debug('I-OS1 Family code = ' + os1.Family_Code__c);
system.debug('I-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('I-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('I-OS2 name =' + os2.Name);
system.debug('I-OS2 Family code = ' + os2.Family_Code__c);
system.debug('I-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('I-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('I-OS3 name =' + os3.Name);
system.debug('I-OS3 Family code = ' + os3.Family_Code__c);
system.debug('I-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('I-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('I-OS4 name =' + os4.Name);
system.debug('I-OS4 Family code = ' + os4.Family_Code__c);
system.debug('I-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('I-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
if(trigger.isUpdate){
system.debug('is update');
for(Opportunity opp: Trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('U-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() ==0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('U-OS1 name =' + os1.Name);
system.debug('U-OS1 Family code = ' + os1.Family_Code__c);
system.debug('U-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('U-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('U-OS2 name =' + os2.Name);
system.debug('U-OS2 Family code = ' + os2.Family_Code__c);
system.debug('U-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('U-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('U-OS3 name =' + os3.Name);
system.debug('U-OS3 Family code = ' + os3.Family_Code__c);
system.debug('U-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('U-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('U-OS4 name =' + os4.Name);
system.debug('U-OS4 Family code = ' + os4.Family_Code__c);
system.debug('U-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('U-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
}
system.Debug('after if statement');
system.debug(oppSummariesToInsert.size());
if(oppSummariesToInsert.size() >0){
Database.upsert(oppSummariesToInsert, false);
system.debug('os inserted');
}
}
Many thanks
My issue is whe I delete the existing Opportunity_Summaries__c records, the list still has 4 in, so they are never being re-created if I'm deleting them first off.
Can anyone suggest a way I get refresh the list without hitting the database again.
Any guidance would be appreciated.
Trigger:
trigger InsertOpportunitySummaries on Opportunity (after insert, after update) {
List<Opportunity_Summary__C> oppSummariesToInsert = new List<Opportunity_Summary__C>();
Map<id, List<Opportunity_Summary__C>> mapOfOppSummaries = new Map<id, List<Opportunity_Summary__C>>();
Set<id> oppIDs = new set<id>();
oppIDs = trigger.newMap.KeySet();
List<Opportunity> associatedOppSummaries = [SELECT id, (SELECT id, Name, Family_Description__C, Family_Code__c, Value__c FROM Opportunities_Summaries__r) FROM Opportunity WHERE id IN: oppIDs];
For(Opportunity opp:associatedOppSummaries){
mapOfOppSummaries.put(opp.id,opp.Opportunities_Summaries__r);
}
system.debug('Start');
system.debug('MapOfOppSummaries is empty = '+ mapOfOppSummaries.isEmpty());
if(Trigger.IsAfter)
{
for(Opportunity opp:trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() != 0){
for (Opportunity_Summary__c os:linkedOS){
system.debug('Deleted Linked OS ID = ' + os.Id);
system.debug('Deleted Linked OS Family = ' + os.Family_Description__c);
system.debug('Deleted Linked OS Family Code = ' + os.Family_Code__c);
system.debug('Deleted Linked OS Value = ' + os.Value__c);
database.delete(os, false);
}
}
}
system.debug('Is after');
if(trigger.IsInsert){
system.debug('Is insert');
for(Opportunity opp: Trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() == 0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('I-OS1 name =' + os1.Name);
system.debug('I-OS1 Family code = ' + os1.Family_Code__c);
system.debug('I-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('I-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('I-OS2 name =' + os2.Name);
system.debug('I-OS2 Family code = ' + os2.Family_Code__c);
system.debug('I-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('I-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('I-OS3 name =' + os3.Name);
system.debug('I-OS3 Family code = ' + os3.Family_Code__c);
system.debug('I-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('I-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('I-OS4 name =' + os4.Name);
system.debug('I-OS4 Family code = ' + os4.Family_Code__c);
system.debug('I-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('I-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
if(trigger.isUpdate){
system.debug('is update');
for(Opportunity opp: Trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('U-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() ==0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('U-OS1 name =' + os1.Name);
system.debug('U-OS1 Family code = ' + os1.Family_Code__c);
system.debug('U-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('U-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('U-OS2 name =' + os2.Name);
system.debug('U-OS2 Family code = ' + os2.Family_Code__c);
system.debug('U-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('U-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('U-OS3 name =' + os3.Name);
system.debug('U-OS3 Family code = ' + os3.Family_Code__c);
system.debug('U-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('U-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('U-OS4 name =' + os4.Name);
system.debug('U-OS4 Family code = ' + os4.Family_Code__c);
system.debug('U-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('U-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
}
system.Debug('after if statement');
system.debug(oppSummariesToInsert.size());
if(oppSummariesToInsert.size() >0){
Database.upsert(oppSummariesToInsert, false);
system.debug('os inserted');
}
}
Many thanks
So this is checking the map to see if it contains an entry for the particular opp. If it doesn't, you create the new entries.
All Answers
Thank you for replying. I understand that they are still in the mapOfOppSummaries, but I was wondering what was the best way to go about removing/deleting them. If I add the Bold and Underlined line of 'mapOfOppSummaries.remove(opp.id); ' I then get a
System.NullPointerException: Attempt to de-reference a null object: Trigger.InsertOpportunitySummaries: error on the bold and underlined line further down the trigger.
If I declare the map as empty, then to re-populate it wouldn't I need to run the select again that populates the associatedOppSummaries which in turn populates the mapOfOppSummaries?
Apologies if these are simple questions, I'm just getting my head around triggers etc.
trigger InsertOpportunitySummaries on Opportunity (after insert, after update) {
List<Opportunity_Summary__C> oppSummariesToInsert = new List<Opportunity_Summary__C>();
Map<id, List<Opportunity_Summary__C>> mapOfOppSummaries = new Map<id, List<Opportunity_Summary__C>>();
Set<id> oppIDs = new set<id>();
oppIDs = trigger.newMap.KeySet();
List<Opportunity> associatedOppSummaries = [SELECT id, (SELECT id, Name, Family_Description__C, Family_Code__c, Value__c FROM Opportunities_Summaries__r) FROM Opportunity WHERE id IN: oppIDs];
For(Opportunity opp:associatedOppSummaries){
mapOfOppSummaries.put(opp.id,opp.Opportunities_Summaries__r);
}
system.debug('Start');
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
if(Trigger.IsAfter)
{
for(Opportunity opp:trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() != 0){
for (Opportunity_Summary__c os:linkedOS){
system.debug('Deleted Linked OS ID = ' + os.Id);
system.debug('Deleted Linked OS Family = ' + os.Family_Description__c);
system.debug('Deleted Linked OS Family Code = ' + os.Family_Code__c);
system.debug('Deleted Linked OS Value = ' + os.Value__c);
database.delete(os, false);
mapOfOppSummaries.remove(opp.id);
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
}
}
}
system.debug('Is after');
if(trigger.IsInsert){
system.debug('Is insert');
for(Opportunity opp: Trigger.new){
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() == 0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('I-OS1 name =' + os1.Name);
system.debug('I-OS1 Family code = ' + os1.Family_Code__c);
system.debug('I-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('I-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('I-OS2 name =' + os2.Name);
system.debug('I-OS2 Family code = ' + os2.Family_Code__c);
system.debug('I-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('I-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('I-OS3 name =' + os3.Name);
system.debug('I-OS3 Family code = ' + os3.Family_Code__c);
system.debug('I-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('I-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('I-OS4 name =' + os4.Name);
system.debug('I-OS4 Family code = ' + os4.Family_Code__c);
system.debug('I-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('I-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
if(trigger.isUpdate){
system.debug('is update');
for(Opportunity opp: Trigger.new){
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('U-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() ==0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('U-OS1 name =' + os1.Name);
system.debug('U-OS1 Family code = ' + os1.Family_Code__c);
system.debug('U-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('U-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('U-OS2 name =' + os2.Name);
system.debug('U-OS2 Family code = ' + os2.Family_Code__c);
system.debug('U-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('U-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('U-OS3 name =' + os3.Name);
system.debug('U-OS3 Family code = ' + os3.Family_Code__c);
system.debug('U-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('U-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('U-OS4 name =' + os4.Name);
system.debug('U-OS4 Family code = ' + os4.Family_Code__c);
system.debug('U-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('U-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
}
system.Debug('after if statement');
system.debug(oppSummariesToInsert.size());
if(oppSummariesToInsert.size() >0){
Database.upsert(oppSummariesToInsert, false);
system.debug('os inserted');
}
}
:)
When it hits the update if statement, would I not get the null reference because it cannot get the opp.id(bold and underlined line) because I've removed it from the map previously?
I'm not entirely sure what the trigger.new returns, so I could be completely wrong.
trigger InsertOpportunitySummaries on Opportunity (after insert, after update) {
List<Opportunity_Summary__C> oppSummariesToInsert = new List<Opportunity_Summary__C>();
Map<id, List<Opportunity_Summary__C>> mapOfOppSummaries = new Map<id, List<Opportunity_Summary__C>>();
Set<id> oppIDs = new set<id>();
oppIDs = trigger.newMap.KeySet();
List<Opportunity> associatedOppSummaries = [SELECT id, (SELECT id, Name, Family_Description__C, Family_Code__c, Value__c FROM Opportunities_Summaries__r) FROM Opportunity WHERE id IN: oppIDs];
For(Opportunity opp:associatedOppSummaries){
mapOfOppSummaries.put(opp.id,opp.Opportunities_Summaries__r);
}
system.debug('Start');
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
if(Trigger.IsAfter)
{
for(Opportunity opp:trigger.new){
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() != 0){
for (Opportunity_Summary__c os:linkedOS){
system.debug('Deleted Linked OS ID = ' + os.Id);
system.debug('Deleted Linked OS Family = ' + os.Family_Description__c);
system.debug('Deleted Linked OS Family Code = ' + os.Family_Code__c);
system.debug('Deleted Linked OS Value = ' + os.Value__c);
database.delete(os, false);
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
}
}
mapOfOppSummaries.remove(opp.id);
}
system.debug('Is after');
if(trigger.IsInsert){
system.debug('Is insert');
for(Opportunity opp: Trigger.new){
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('I-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() == 0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('I-OS1 name =' + os1.Name);
system.debug('I-OS1 Family code = ' + os1.Family_Code__c);
system.debug('I-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('I-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('I-OS2 name =' + os2.Name);
system.debug('I-OS2 Family code = ' + os2.Family_Code__c);
system.debug('I-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('I-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('I-OS3 name =' + os3.Name);
system.debug('I-OS3 Family code = ' + os3.Family_Code__c);
system.debug('I-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('I-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('I-OS4 name =' + os4.Name);
system.debug('I-OS4 Family code = ' + os4.Family_Code__c);
system.debug('I-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('I-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
if(trigger.isUpdate){
system.debug('is update');
for(Opportunity opp: Trigger.new){
system.debug('MapOfOppSummaries is empty? = '+ mapOfOppSummaries.isEmpty());
system.debug('MapOfOppSummaries size = '+ mapOfOppSummaries.size());
List<Opportunity_Summary__c> linkedOS = mapOfOppSummaries.get(opp.id);
system.debug('U-Linked OS list size = '+ linkedOS.size());
if(linkedOS.size() ==0){
Opportunity_Summary__c os1 = new Opportunity_Summary__c();
os1.Name = 'Opp Summary 1-'+ opp.Name;
os1.Opportunity_Summary__c = opp.Id;
os1.Family_Code__c = 1;
os1.Family_Description__c = 'Hardware';
os1.Value__c = opp.Net_Hardware_Total__c;
oppSummariesToInsert.add(os1);
system.debug('U-OS1 name =' + os1.Name);
system.debug('U-OS1 Family code = ' + os1.Family_Code__c);
system.debug('U-OS1 Family Description = '+ os1.Family_Description__c);
system.debug('U-OS1 Value =' + opp.Net_Hardware_Total__c);
Opportunity_Summary__c os2 = new Opportunity_Summary__c();
os2.Name = 'Opp Summary 2-'+ opp.Name;
os2.Opportunity_Summary__c = opp.Id;
os2.Family_Code__c = 2;
os2.Family_Description__c = 'Licence';
os2.Value__c = opp.Net_Licence_Total__c;
oppSummariesToInsert.add(os2);
system.debug('U-OS2 name =' + os2.Name);
system.debug('U-OS2 Family code = ' + os2.Family_Code__c);
system.debug('U-OS2 Family Description = '+ os2.Family_Description__c);
system.debug('U-OS2 Value =' + opp.Net_Licence_Total__c);
Opportunity_Summary__c os3 = new Opportunity_Summary__c();
os3.Name = 'Opp Summary 3-'+ opp.Name;
os3.Opportunity_Summary__c = opp.Id;
os3.Family_Code__c = 3;
os3.Family_Description__c = 'Professional Services';
os3.Value__c = opp.Net_PS_Total__c;
oppSummariesToInsert.add(os3);
system.debug('U-OS3 name =' + os3.Name);
system.debug('U-OS3 Family code = ' + os3.Family_Code__c);
system.debug('U-OS3 Family Description = '+ os3.Family_Description__c);
system.debug('U-OS3 Value =' + opp.Net_PS_Total__c);
Opportunity_Summary__c os4 = new Opportunity_Summary__c();
os4.Name = 'Opp Summary 4-'+ opp.Name;
os4.Opportunity_Summary__c = opp.Id;
os4.Family_Code__c = 4;
os4.Family_Description__c = 'Maintenance';
os4.Value__c = opp.Net_Maintenance_Total__c;
oppSummariesToInsert.add(os4);
system.debug('U-OS4 name =' + os4.Name);
system.debug('U-OS4 Family code = ' + os4.Family_Code__c);
system.debug('U-OS4 Family Description = '+ os4.Family_Description__c);
system.debug('U-OS4 Value =' + opp.Net_Maintenance_Total__c);
}
}
}
}
system.Debug('after if statement');
system.debug(oppSummariesToInsert.size());
if(oppSummariesToInsert.size() >0){
Database.upsert(oppSummariesToInsert, false);
system.debug('os inserted');
}
}
So this is checking the map to see if it contains an entry for the particular opp. If it doesn't, you create the new entries.
I've removed the list variable completely, as it's redundant now.
Thanks again