You need to sign in to do that
Don't have an account?
Akanksha
Trigger written to create multiple records but is creating a single one
There is a junction object New Sponsorship between 2 Masters Recurring Gift and Child. Also, one of master record Recurring gift is related to Contact through look up. We need to create sponsorship when an opportunity (related to Recurring Gift, New Sponsorship) is created. If the child Id field on Opportunity record contains multiple ids then multiple New Sponsorship records should be created. I have a for loop for the list of Child ids.
The trigger is firing but is creating only a single record rahter than multiple records. Any help would be great !
The code is
trigger CreateNewSponsorship on Opportunity (before insert , before update) {
RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
Set<Id> sponsorIds = new Set<Id>();
for (Opportunity opp : trigger.new) {
sponsorIds.add(opp.cv__Contact__c);
}
// R00NU0000000sKlMMAU represent relation ship between recurring gift and contact object
Map<Id, Contact> sponsors = new Map<Id, Contact>([SELECT Id, (SELECT cv__Contact__c FROM R00NU0000000sKlMMAU) FROM Contact WHERE Id IN :sponsorIds]);
Map<Id, New_Sponsorship__c> sponsorships = new Map<Id, New_Sponsorship__c>();
for (Opportunity opp : trigger.new) {
System.debug('Inside for loop');
System.debug('Opportunity Child Id is : '+opp.Child_ID__c);
// String ch = opp.Child_ID__c.substring(0,15);
List<String> strChildIds = opp.Child_ID__c.split(',');
if(trigger.isInsert && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
}
}
if(trigger.isUpdate && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null && trigger.oldmap.get(opp.id).Child_ID__c==null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
}
}
}
Map<Id, cv__Recurring_Gift__c> recurringGifts = new Map<Id, cv__Recurring_Gift__c>([SELECT Id, Sponsorship__c FROM cv__Recurring_Gift__c WHERE Id IN :sponsorships.keySet()]);
insert sponsorships.values();
// update gift sponsorships
// List<cv__Recurring_Gift__c> giftsToUpdate = new List<cv__Recurring_Gift__c>();
// for (Id giftId : sponsorships.keySet()) {
// giftsToUpdate.add(new cv__Recurring_Gift__c(Id = giftId, Sponsorship__c = sponsorships.get(giftId).Id));
// }
// update giftsToUpdate;
// update donation sponsorships
for (Opportunity opp : trigger.new) {
if (sponsorships.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = sponsorships.get(opp.cv__Recurring_Gift__c).Id;
} else if (recurringGifts.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = recurringGifts.get(opp.cv__Recurring_Gift__c).Sponsorship__c;
}
}
}
The trigger is firing but is creating only a single record rahter than multiple records. Any help would be great !
The code is
trigger CreateNewSponsorship on Opportunity (before insert , before update) {
RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
Set<Id> sponsorIds = new Set<Id>();
for (Opportunity opp : trigger.new) {
sponsorIds.add(opp.cv__Contact__c);
}
// R00NU0000000sKlMMAU represent relation ship between recurring gift and contact object
Map<Id, Contact> sponsors = new Map<Id, Contact>([SELECT Id, (SELECT cv__Contact__c FROM R00NU0000000sKlMMAU) FROM Contact WHERE Id IN :sponsorIds]);
Map<Id, New_Sponsorship__c> sponsorships = new Map<Id, New_Sponsorship__c>();
for (Opportunity opp : trigger.new) {
System.debug('Inside for loop');
System.debug('Opportunity Child Id is : '+opp.Child_ID__c);
// String ch = opp.Child_ID__c.substring(0,15);
List<String> strChildIds = opp.Child_ID__c.split(',');
if(trigger.isInsert && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
}
}
if(trigger.isUpdate && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null && trigger.oldmap.get(opp.id).Child_ID__c==null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
}
}
}
Map<Id, cv__Recurring_Gift__c> recurringGifts = new Map<Id, cv__Recurring_Gift__c>([SELECT Id, Sponsorship__c FROM cv__Recurring_Gift__c WHERE Id IN :sponsorships.keySet()]);
insert sponsorships.values();
// update gift sponsorships
// List<cv__Recurring_Gift__c> giftsToUpdate = new List<cv__Recurring_Gift__c>();
// for (Id giftId : sponsorships.keySet()) {
// giftsToUpdate.add(new cv__Recurring_Gift__c(Id = giftId, Sponsorship__c = sponsorships.get(giftId).Id));
// }
// update giftsToUpdate;
// update donation sponsorships
for (Opportunity opp : trigger.new) {
if (sponsorships.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = sponsorships.get(opp.cv__Recurring_Gift__c).Id;
} else if (recurringGifts.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = recurringGifts.get(opp.cv__Recurring_Gift__c).Sponsorship__c;
}
}
}
//start the loop for multiple sponsorship creationfor( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
for( String str : strChildIds){
if(sponsorships.containsKey(opp.cv__Recurring_Gift__c))
sponsorships.get(opp.cv__Recurring_Gift__c).add(new New_Sponsorship__c(Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id , Recurring_Gift_2__c = opp.cv__Recurring_Gift__c, Start_Date__c = opp.CloseDate))
else
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
}
Let me know if this works for you.
Now I need to check duplicates opportunity if sponsorship already exists for same Recurring gift and Child Ids combination.
any help for checking the child id(break the string into individual ids) of opportunity already present in another opportunity for same recurring gift.
trigger CreateNewSponsorship on Opportunity (before insert , before update) {
RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
Set<Id> sponsorIds = new Set<Id>();
for (Opportunity opp : trigger.new) {
sponsorIds.add(opp.cv__Contact__c);
}
Map<Id, Contact> sponsors = new Map<Id, Contact>([SELECT Id, (SELECT cv__Contact__c FROM R00NU0000000sKlMMAU) FROM Contact WHERE Id IN :sponsorIds]);
Map<Id, New_Sponsorship__c> sponsorships = new Map<Id, New_Sponsorship__c>();
for (Opportunity opp : trigger.new) {
System.debug('Inside for loop');
System.debug('Opportunity Child Id is : '+opp.Child_ID__c);
// String ch = opp.Child_ID__c.substring(0,15);
List<String> strChildIds = opp.Child_ID__c.split(',');
if(trigger.isInsert && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
System.debug('Inside for loop : Child Id : '+str);
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
insert sponsorships.values();
}
}
}
if(trigger.isUpdate && trigger.isBefore){
if (opp.RecordTypeId == rd.Id && opp.cv__Recurring_Gift__c != null && opp.cv__Contact__c != null && opp.Child_ID__c != null && trigger.oldmap.get(opp.id).Child_ID__c==null
&& (!sponsorships.containsKey(opp.cv__Recurring_Gift__c) || sponsorships.get(opp.cv__Recurring_Gift__c).Start_Date__c > opp.CloseDate)) {
Contact sponsor = sponsors.get(opp.cv__Contact__c);
//start the loop for multiple sponsorship creation
for( String str : strChildIds){
sponsorships.put(
opp.cv__Recurring_Gift__c,
new New_Sponsorship__c(
// Sponsor__c = opp.cv__Contact__c,
Child__c = [SELECT Id FROM Child__c WHERE Id = :str LIMIT 1 ].Id ,
Recurring_Gift_2__c = opp.cv__Recurring_Gift__c,
Start_Date__c = opp.CloseDate
// Assignment_type__c = sponsor != null && sponsor.New_Sponsorships__r.size() > 0 ? 'Additional' : 'New Enroll'
)
);
insert sponsorships.values();
}
}
}
}
Map<Id, cv__Recurring_Gift__c> recurringGifts = new Map<Id, cv__Recurring_Gift__c>([SELECT Id, Sponsorship__c FROM cv__Recurring_Gift__c WHERE Id IN :sponsorships.keySet()]);
// insert sponsorships.values();
// update gift sponsorships
// List<cv__Recurring_Gift__c> giftsToUpdate = new List<cv__Recurring_Gift__c>();
// for (Id giftId : sponsorships.keySet()) {
// giftsToUpdate.add(new cv__Recurring_Gift__c(Id = giftId, Sponsorship__c = sponsorships.get(giftId).Id));
// }
// update giftsToUpdate;
// update donation sponsorships
for (Opportunity opp : trigger.new) {
if (sponsorships.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = sponsorships.get(opp.cv__Recurring_Gift__c).Id;
} else if (recurringGifts.containsKey(opp.cv__Recurring_Gift__c)) {
opp.New_Sponsorship__c = recurringGifts.get(opp.cv__Recurring_Gift__c).Sponsorship__c;
}
}
}