+ Start a Discussion
paddingtonpaddington 

Bulk Trigger Problems

The boards have been really supportive of late, so, firstly, thanks, and, secondly, any help with the following problem would be much appreciated.

 

I have an object (Assessment__c) related to Account from which I am triggering code that executes on lists of accounts. The way the system is set up means that each account has two Assessment objects, which start off with the Recommendation__c picklist at --None selected--. When this changes, i.e. someone makes a recommendation, I want the trigger to fire and put the related account in the appropriate list of accounts and send that list to the appropriate method in the Assessments class.

 

I've tested the methods that I'm calling using system log and everything's good with them, but I can't get the following trigger to fire. I have a sneaking suspicion that it may be something to do with either:

  1. Recommendation__c is a picklist and therefore non-nillable, although I've dealt with that by using '', which I've read on the boards is equivalent to null for picklists.
  2. The condition on the related account - am I allowed to do this with Apex, or should I be getting a list of related accounts and iterating through those?

Thanks in advance for any help!

 

 

trigger RecommendationMade on Assessment__c (after update) { //Create list of account IDs for which a second recommendation has been made List<ID> SecondRecIDs = new List<ID> (); //Create list of account IDs for which only one recommendation has been made List<ID> FirstRecIDs = new List<ID> (); //Iterate through trigger set for(Integer i=0; i<Trigger.new.size(); i++){ if(Trigger.new[i].RecordTypeId == '01280000000BR0VAAW' && Trigger.old[i].Recommendation__c == '' && Trigger.new[i].Recommendation__c != '' && Trigger.new[i].Account__r.Initial_Enquiry_Sub_stage__c == 'Awaiting one assessment'){ SecondRecIDs.add(Trigger.new[i].Account__c); } else if(Trigger.new[i].RecordTypeId == '01280000000BR0VAAW' && Trigger.old[i].Recommendation__c == '' && Trigger.new[i].Recommendation__c != '' && Trigger.new[i].Account__r.Initial_Enquiry_Sub_stage__c == 'Awaiting two assessments'){ FirstRecIDs.add(Trigger.new[i].Account__c); } } //Create list of accounts for which the second recommendation has been made List<Account> SecondRec = new List<Account>([SELECT Name FROM Account WHERE Id IN :SecondRecIDs]); //Pass this list to the Assessments class Assessments.decideActionIEF(SecondRec); //Create list of accounts for which only the first recommendation has been made List<Account> FirstRec = new List<Account>([SELECT Name FROM Account WHERE Id IN :FirstRecIDs]); //Pass this list to the Assessments class Assessments.updateIEFsubStage(FirstRec); }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

I would write a quick test script to create an account, and then insert some recommendation records, and see what the values returned are... 

 

Re point one, I am pretty sure you can use != null, == null rather than the '' syntax - but you can check that with debug messages...

 

Re point 2, I don't think you do can that, but again, I would just check if a value is being returned in a test script with debug statements . If not, you'll need to just create a map of the Account Id and Initial Enquiry stage to check the value...post back if you aren't sure how to do that...

 

Something else to consider is that you can probably use regular workflow with the ischanged() formula to do the initial processing on the Requirements field and then set a field that is 'Requires First Processing'/'Requires Second Processing' and just build the trigger based on that field...   

All Answers

BritishBoyinDCBritishBoyinDC

I would write a quick test script to create an account, and then insert some recommendation records, and see what the values returned are... 

 

Re point one, I am pretty sure you can use != null, == null rather than the '' syntax - but you can check that with debug messages...

 

Re point 2, I don't think you do can that, but again, I would just check if a value is being returned in a test script with debug statements . If not, you'll need to just create a map of the Account Id and Initial Enquiry stage to check the value...post back if you aren't sure how to do that...

 

Something else to consider is that you can probably use regular workflow with the ischanged() formula to do the initial processing on the Requirements field and then set a field that is 'Requires First Processing'/'Requires Second Processing' and just build the trigger based on that field...   

This was selected as the best answer
paddingtonpaddington

Thanks BritishBoyinDC. It turns out that you can use null with picklist values and that I needed to use a map to access the related account fields. The next thing on my to-do is to start using debug messages and change them to asserts when fully tested for bullet-proof code. Getting there.

 

For the record, the finished code looks like this (BTW there are two kinds of recommendation, IEF and SAQ. Note that I haven't written the SAQ methods in the Assessments class yet, hence the commenting out):

 

 

trigger RecommendationMade on Assessment__c (after update) { //IEF 1 //Create list of account IDs from updated assessments of IEF record type List<ID> IEFids = new List<ID>(); //IEF 2 //Iterate through trigger set and add account IDs to list when //a recommendation has been made and the assessment is of IEF record type for(Integer i=0; i<Trigger.new.size(); i++){ if(Trigger.new[i].RecordTypeId == '01280000000BR0VAAW' && Trigger.old[i].Recommendation__c == null && Trigger.new[i].Recommendation__c != null){ IEFids.add(Trigger.new[i].Account__c); } } //SAQ 1 //Create list of account IDs from updated assessments of SAQ record type List<ID> SAQids = new List<ID>(); //SAQ 2 //Iterate through trigger set and add account IDs to list when //a recommendation has been made and the assessment is of SAQ record type for(Integer j=0; j<Trigger.new.size(); j++){ if(Trigger.new[j].RecordTypeId == '01280000000BR0aAAG' && Trigger.old[j].Recommendation__c == null && Trigger.new[j].Recommendation__c != null){ SAQids.add(Trigger.new[j].Account__c); } } //IEF 3 //Create map of account ID to account with SOQL for Initial Enquiry Sub-stage Map<ID,Account> IEFmap = new Map<ID,Account>([SELECT Id, Initial_Enquiry_Sub_stage__c FROM Account WHERE Id IN :IEFids]); //SAQ 3 //Create map of account ID to account with SOQL for Initial Enquiry Sub-stage Map<ID,Account> SAQmap = new Map<ID,Account>([SELECT Id, Initial_Enquiry_Sub_stage__c FROM Account WHERE Id IN :SAQids]); //IEF 4 //Create account lists for IEF first and second recommendations List<Account> IEFfirstRec = new List<Account>(); List<Account> IEFsecondRec = new List<Account>(); for(ID id :IEFids){ Account a=IEFmap.get(id); if(a.Initial_Enquiry_Sub_stage__c == 'Awaiting two assessments'){ IEFfirstRec.add(a); } else if(a.Initial_Enquiry_Sub_stage__c == 'Awaiting one assessment'){ IEFsecondRec.add(a); } } //IEF 5 //Pass the IEF account lists to the appropriate methods in the Assessments class Assessments.updateIEFsubStage(IEFfirstRec); Assessments.decideActionIEF(IEFsecondRec); //SAQ 4 //Create account lists for SAQ first and second recommendations List<Account> SAQfirstRec = new List<Account>(); List<Account> SAQsecondRec = new List<Account>(); for(ID id :SAQids){ Account z=SAQmap.get(id); if(z.Initial_Enquiry_Sub_stage__c == 'Awaiting two assessments'){ SAQfirstRec.add(z); } else if(z.Initial_Enquiry_Sub_stage__c == 'Awaiting one assessment'){ SAQsecondRec.add(z); } } //SAQ 5 //Pass the SAQ account lists to the appropriate methods in the Assessments class //Assessments.updateIEFsubStage(IEFfirstRec); //Assessments.decideActionIEF(IEFsecondRec); }

 

 

 

KVPKVP

HI I have written a triggen which is goving governor limit exception. can anyone please help to bulkify this?

 

Here is my code

 

trigger ClinicConsultation on ROTA__c (after insert, after update) {
List<Monthly_Schedule__c> M = new List<Monthly_Schedule__c>();

For (ROTA__c Oc : trigger.New){If(Oc.ROta_for__c=='Clinic Consultation') 

 {       Date d;   d= Oc.From_Date__c;      date dd=d; 

 List<Resource_Management__c> R = [Select Id, Related_Center_Name__r.Id, Home_Visit_Status__c from Resource_Management__c where On_Clinic_Consultation_Duty__c= True  and Related_Center_Name__c=:Oc.Hub_Name__c ]; 

//Resource_Management__c [] R = [Select Id, Related_Center_Name__r.Id, Home_Visit_Status__c from Resource_Management__c where On_Clinic_Consultation_Duty__c= True and Related_Center_Name__c=:Oc.Hub_Name__c ]; 

 

integer count= [select count() from Resource_Management__c where On_Clinic_Consultation_Duty__c= True and Related_Center_Name__c=:Oc.Hub_Name__c]; 

  For(integer i=0;i<count;i++)    {      

       d=dd;     

 While (d<=Oc.TO_Date__C)         

 {                   Integer MSched = [Select count() from Monthly_Schedule__c where Doctor_Name__c=:R[i].id and date__c=:d];                            

    if(MSched == 0)             

{                        Monthly_Schedule__c MS = new Monthly_Schedule__c();                                                      

 ms.Doctor_Name__c= R[i].id;                                                     

 ms.Center_Name__c = R[i].Related_Center_Name__r.Id;  

                                                   ms.Clinic_Consultation__c=true;                 

                                     ms.Available__c = TRUE;                               

                       ms.Date__c=d;           

             M.add(MS);                          

                                 }                                    

                                           Date dt = date.newinstance(d.year(), d.month(), d.day());         

    datetime myDate = datetime.newInstance(dt.year(), dt.month(), dt.day());           

  String day = myDate.format('EEEE');            

 If(Day=='Saturday')               

 {                       d=d+2;                                       }         

   else                 {                      d=d+1;                 }                       

 

      }         }   } }  insert m;}