function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bpolbpol 

Converting from Single Trigger to Bulk

This works fine as a single trigger.  I'm new to writing bulk triggers. 

 

How do I go about bulk-ifying this trigger?

 

Ben

 

 

temp_studentid = sa[0].StudentID_SIS__c;
if (temp_studentid <> null) {
temp_recordtype = 'onsite - coaching';
try {
RelatedStudentId = [select
id, Extreme_Student_ID__c
from Student__c where Extreme_Student_ID__c = :temp_studentid].id;
} catch (system.Exception e4){
}

}

//----------------------------------------------------------------
// Set fields for Updating
//----------------------------------------------------------------

sa[0].Related_School_Account__c = RelatedSchoolid;
sa[0].Related_District_Account__c = RelatedDistrictid;
sa[0].Related_Student__c = RelatedStudentid;
sa[0].Record_Type__c = temp_recordtype;

 

 

 

sunil316sunil316

Can you plzz briefly explain what this trigger is doing, then i might be able to give you some suggestion on this.

 

 

-Sunil 

bpolbpol

Thanks! 

 

I'm expect to do a bulk load and in order to get the info ready for analysis, I need to link each record to the appropriate objects/records upon upload.

 

I would like to do the following:

 

1) do a test -- then based on the result, set a field value.

 

In this case, if sa[0].StudentID_SIS__c = XXX, then set a field sa[0].Record_Type__c to YYYY).  In other triggers I have several tests... but basically its the same type of test and do

 

2)  I also want to set the values of a field based on a field in a related object

 

In this case, I basically want to set sa[0].Related_District_Account__c and sa[0].Related_School_Account__c. 

 

  • Set the field sa[0].District_Account__c to the Account.Id where
sa[0].Account_D_code__c = Account.Account_D_Code AND
Account.Account_S_code = '0000000'. 

  • Set the field sa[0].School_Account__c to the Account.id where
sa[0].Account_S_code__c = Account.Account_S_code__c

 

Any help would be great!!

 

 

Here's the entire trigger if it helps.

 

 

trigger UpdateStudentAttendance_Before on Student_Attendance__c (before insert, before update) { //--------------------------------------------------------------- // Establish variables //---------------------------------------------------------------- String saID = null, saDistrict = null, saSchool = null, saCDS = null; String RelatedSchoolId = null, RelatedDistrictId = null, RelatedStudentId = null; String temp_studentid = null, temp_recordtype = null; Integer saCDSlength = 0; Student_Attendance__c[] sa = trigger.new; saID = sa[0].Id; saDistrict = sa[0].Account_D_Code__c; saSchool = sa[0].Account_S_Code__c; saCDS = sa[0].Account_CDE_Code__c; if (saCDS <> null) { saCDSlength = saCDS.length(); } //---------------------------------------------------------------- // Update Related School and District //---------------------------------------------------------------- // If CDS code exists then parse it for the District and School Accounts if (saCDSlength == 13) { saCDS = '0' + saCDS; saCDSlength = saCDS.length(); } if (saCDSlength == 14) { saDistrict = string.valueof(saCDS.substring (2, 7)); saSchool = string.valueof(saCDS.substring (7,14)); try { RelatedDistrictid = [select id, CDS_Code__c,Account_D_code__c from Account where (Account_D_code__c = :saDistrict and Account_S_code__c = '0000000')][0].id; RelatedSchoolid = [select id, CDS_Code__c, Account_S_code__c from Account where Account_S_code__c = :saSchool][0].id; } catch (system.Exception e1) { } } // If the District code exits then parse for the District Account else if (saDistrict <> null) { try { RelatedDistrictid = [select id, CDS_Code__c,Account_D_code__c from Account where (Account_D_code__c = :saDistrict and Account_S_code__c = '0000000')][0].id; } catch (system.Exception e2) { } } else { } //---------------------------------------------------------------- // Update Related Student & Record Type //---------------------------------------------------------------- temp_studentid = sa[0].StudentID_LMS__c; if (temp_studentid <> null) { temp_recordtype = 'online - coaching'; try { RelatedStudentId = [select id, Extreme_Student_ID__c from Student__c where Extreme_Student_ID__c = :temp_studentid].id; } catch (system.Exception e3){ } } temp_studentid = sa[0].StudentID_SIS__c; if (temp_studentid <> null) { temp_recordtype = 'onsite - coaching'; try { RelatedStudentId = [select id, Extreme_Student_ID__c from Student__c where Extreme_Student_ID__c = :temp_studentid].id; } catch (system.Exception e4){ } } //---------------------------------------------------------------- // Set fields for Updating //---------------------------------------------------------------- sa[0].Related_School_Account__c = RelatedSchoolid; sa[0].Related_District_Account__c = RelatedDistrictid; sa[0].Related_Student__c = RelatedStudentid; sa[0].Record_Type__c = temp_recordtype;

 

 

 

sunil316sunil316

Hello,

ya. what i understood is, you want to change the value  Record_Type__c, when StudentID_SIS__c is XXX(say).

 

for this:

 

 trigger UpdateStudentAttendance_Before on Student_Attendance__c (before insert)

{

for( Student_Attendance__c objstud: Trigger.new)

{

if(objstud.StudentID_SIS__c = 'XXX')  

{

objstud.Record_Type__c = 'YYYY';

}

 

this should work for 1st part. 

 

 

 

sunil316sunil316

For second part, Im not understanding the certain things,

1) How the two objects are related to each other,

2) which field is related to to which object,

3) what exactly we need to update

 

 

 

-Sunil 

bpolbpol

I finally was able to write a bulk trigger!!

 

Thanks!