0

But it return expected records when run on query editor on developer console using the triggered record Id. Debug log also shows no records returned. Is there any reason for this? when run the query on developer console with conId it return records.

Aggregateresult query do not return any rows.

trigger PopulateFundingMonthsFromTP on Training_Plan__c (after Update) {
  //  if(checkRecursive.runOnce()){  
    Set<Id> TAIds = new Set<Id>();
    Set<Id> ConId = new Set<Id> ();
    List<Funding_Cycles_TA__c> FCTAToUpdate = new List<Funding_Cycles_TA__c>();
    Training_Plan__c[] TPs = null;

    //select the Training plans
    for (Training_Plan__c TP : Trigger.new) 
    {
       // if((TP.Training_Agreement__c != null)||(TP.Select_for_Funding_Mths_for_TA__c==True)||(TP.Select_for_Funding_Mths_for_TA_NZA__c==True))
       // {
            TAIds.add(TP.Training_Agreement__c);
            ConId.add(Tp.TraineeRef__c);
       // }
    }

    //Aggregate SOQL from STM Computations
    List<AggregateResult> agrResults =
        [SELECT 
         Reporting_Year__c reportingYear, Reporting_Year_End__c rptend,
         Count(Id) fundedmonths, Training_Plan__r.TraineeRef__c contactId,
         Training_Plan__r.Training_Agreement__c ita
         FROM STM_Computation__c
         //WHERE Training_Plan__r.Training_Agreement__c in :TAIds 
         WHERE Trainee_Id__c in :ConId  
         AND Projected_Actual_STM__c='2 - Actual STM' 
         GROUP BY Reporting_Year__c, Reporting_Year_End__c,Training_Plan__r.TraineeRef__c, Training_Plan__r.Training_Agreement__c
         Order BY Reporting_Year__c];


    System.debug('*****SFDC TEST : 8 : agrResults  : ' + agrResults );
    System.debug('*****SFDC TEST : 8 : agrResults  : ' + conId );
    //Corrosponding Funding_Cycles_TA__c(Child records)need to be updated from Training Agreement
    List<Funding_Cycles_TA__c>  fctasToUpdate = [Select id,Reporting_Year__c, Reporting_Year_End__c, 
                                                 Funded_Months__c,
                                                 Training_Agreement__c, Training_Agreement__r.TraineeMaster__c 
                                                 From Funding_Cycles_TA__c
                                                 where Training_Agreement__c in: TAIds order by Reporting_Year__c ];


    for(Funding_Cycles_TA__c setzero :fctasToUpdate ){
        setzero.Funded_Months__c=0;
    }
        System.debug('*****FundingMonths 1 : ' + fctasToUpdate );

    if(!fctasToUpdate.isEmpty()){
        update fctasToUpdate;
    }

    for (AggregateResult ar: agrResults)
    {  
        for(Funding_Cycles_TA__c eachFCTA : fctasToUpdate)
        {  
            if(eachFCTA.Reporting_Year__c == (Date)ar.get('reportingYear'))
                //&& eachFCTA.Reporting_Year__c != Null)
            {  
                eachFCTA.Funded_Months__c = (Decimal)ar.get('fundedmonths');
            }
        }
        if(!fctasToUpdate.isEmpty()){
            update fctasToUpdate; 
        }    
          System.debug('*****FundingMonths 2 : ' + fctasToUpdate );
    }
    //populate funding months of Fcc from TP
    List<Funding_Cycles_Contacts__c> fcctoUpdate =[Select Id, Reporting_Year__c, Reporting_Year_End__c, Funded_Months__c 
                                                   from Funding_Cycles_Contacts__c 
                                                   where Contact__c IN: conId order by Reporting_Year__c ];

    list<Funding_Cycles_Contacts__c>  fccNewRd = new list<Funding_Cycles_Contacts__c>();

    for (AggregateResult ar: agrResults)
    { 
        if(fcctoUpdate.size()==0){
            Funding_Cycles_Contacts__c fcc = new Funding_Cycles_Contacts__c(
                Reporting_Year__c=(Date)ar.get('reportingYear'),
                Reporting_Year_End__c=(Date)ar.get('rptend'),
                Funded_Months__c=(Decimal)ar.get('fundedmonths'),
                Contact__c=(Id)ar.get('contactId'));
            fccNewRd.add(fcc);
            system.debug('=============fccNewRdfromTP=== '+ fcctoUpdate);
            system.debug('=============fccNewRdfromTP=== '+ fcc.contact__c);
        } 
    }
    if(!fccNewRd.isEmpty()){
        upsert fccNewRd; 
    } 
}
4
  • Is your trigger on before update by any chance? If yes, then the update is not committed to database yet.
    – Raul
    Jul 26, 2017 at 7:31
  • No, it is after update trigger
    – Jay Weera
    Jul 26, 2017 at 8:01
  • I don't see anything wrong, are you sure you're using the correct IDs?
    – EricSSH
    Jul 26, 2017 at 21:23
  • It sounds to me like this is a permissions issue. Does your user account have the permissions to see the Training_Plan__c record you're testing with? What about the TraineeRef__c field? Do you have permission to view the record that TraineeRef__c points to?
    – Derek F
    Jul 27, 2017 at 12:53

1 Answer 1

0

Without seeing your code I'm gonna have to go with one of 2 things:

  1. You're trying to pull data from the DB that doesn't exist (trigger.isBefore).
  2. You're code is running with poor permissions. Try removing the global keyword (if it exists) or with sharing (if they exist).
1
  • Thanks for you time for helping me. I managed to code in different way and now all good
    – Jay Weera
    Jul 27, 2017 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .