• Akanksha
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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;
        }
    }
}
Hello,

I have recently stumbled upon a rather interesting problem on my end. I have set up an integration with external system, and it responds me with a Base64 encoded Hex code of a PDF document whenever I make a call with a single input string. I'm supposed to obtain the Hex code after Base64 decoding, then cast the hex code into a blob & save it as an attachment body.

The problem here is: The Hex code itself and its length does not match when I debug. The string is one character less from the size, and the conversion from hex to blob fails because of odd numbered size of input string - it turns out that the size is correct here.

Below is the code:
//Proforma response = Base64 encoded hex string
Blob afterblob = EncodingUtil.base64Decode(proformaResponse);

string HexCode = afterblob.toString();

Debug.Proforma2__c = HexCode;

Debug.Debug1__c = string.valueOf(Hexcode.length());

update Debug;

Note that I use a custom debug object because of sandbox environment's incapability on debugging webservice calls...

When I make the call, HexCode string length is 52312, and Debug1 field value is 52313. Later I submit the HexCode to the EncodingUtil class' ConvertFromHex function, but it fails while saying "input string char count is an odd number"... When I discarded the endmost string of HexCode, it worked, but that's not an approach I want to adopt.

I appreciate any clarification on this issue.
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;
        }
    }
}