You need to sign in to do that
Don't have an account?
How to avoid governor limits in execute anonymous?
I'm trying to build a class that will put data into my sandbox for testing and training. I'll put the code in below. When I ran it in Execute Anonymous the other day it all seemed to go fine. But today it only got part way. I got governer limit warnings (but not actually exceeding. Is there a difference?) Code is below. I imagine it's not the most efficient (I'm new at this), but I don't think it's bad. I'm on NPSP, so perhaps the NPSP triggers are having something to do with it?
Here's my class:
Here's my class:
public class tempSparkTestDataUtil { //Method to get a single recordTypeId by inputting Object and Record Type Name (NOT API Name -- use spaces not underscores!) Public static Id recordTypeId(string obj, string recTypeName){ Id recTypeId; if(obj!= null && recTypeName != null){ recTypeId = Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recTypeName).getRecordTypeId(); System.debug('Got record type id' + recTypeId + ' for object ' + obj + ' and record type name ' + recTypeName); } return recTypeId; } //Gets the current year, gives last two digits for FY and SY usage public static string giveFY() { integer currentfiscalorschoolyear; if (Date.Today().Month() >= 7){ currentfiscalorschoolyear = Date.Today().Year() + 1; } else { currentfiscalorschoolyear = Date.Today().Year(); } //Get the last two digits string fy = (string.valueof(currentfiscalorschoolyear).right(2)); return fy; } // Creates the Star Wars School District // This could be refactored to take a region and name so that you could use it to create other regions. public static Id createStarWarsSchoolDistrict(string region) { //Create a School District Account Account schoolDistrict = new Account ( Name = 'Tatooine School District', School_Region__c = region, RecordTypeId = recordTypeId('Account', 'School District') ); insert schoolDistrict; return schoolDistrict.Id; } //Creates Jedi Middle School // This could be refactored to take region, school name, and district name so you could use it to create multiple regions public static Id createSchool(string region) { //Create a School on a School District Id district = createStarWarsSchoolDistrict(region); Account school = new Account ( Name = 'Jedi Middle School', School_Region__c = region, School_District__c = district, RecordTypeId = recordTypeId('Account', 'Partner School') ); insert school; return school.Id; } //Method to call create students //First calls school creation, which calls district creation, then puts four students with parents onto the school public static void createJediStudents() { //create school Id jediSchool = createSchool ('Star Wars (test data)'); list <Contact> jediKids = new list <Contact>(); Contact luke = new Contact ( FirstName = 'Luke', LastName = 'Skywalker', Predicted_Graduation_Year_hs__c = String.ValueOf((Integer.ValueOf(giveFY())+2005)), Gender__c = 'Man', Race__c = 'MultiRacial', Birthdate = (Date.today()-(13*365)), Family_Application_Submitted__c = true, Date_of_Application__c = (Date.today() - 10), npsp__Primary_Affiliation__c = jediSchool, Parent_Consent_for_Evaluation__c = true, Date_of_parent_release_and_waiver__c = (Date.today() - 10), Release_Signed_By__c = 'Aunt Beroo', Eval_Release_Signature__c = 'Aunt Beroo', Date_of_parent_evaluation_consent__c = (Date.today() - 10), Media_and_Photo_Release__c = true, Media_Release_Signature__c = 'Aunt Beroo', Date_of_parent_media_release__c = (Date.today() - 10), RecordTypeId = recordTypeId('Contact', 'Spark Student') ); jediKids.add(luke); Contact leia = new Contact ( FirstName = 'Princess Leia', LastName = 'Organa', Predicted_Graduation_Year_hs__c = String.ValueOf((Integer.ValueOf(giveFY())+2005)), Gender__c = 'Woman', Race__c = 'MultiRacial', Birthdate = (Date.today()-(13*365)), Family_Application_Submitted__c = true, Date_of_Application__c = (Date.today() - 10), npsp__Primary_Affiliation__c = jediSchool, Parent_Consent_for_Evaluation__c = true, Date_of_parent_release_and_waiver__c = (Date.today() - 10), Release_Signed_By__c = 'Queen Amidala', Eval_Release_Signature__c = 'Queen Amidala', Date_of_parent_evaluation_consent__c = (Date.today() - 10), Media_and_Photo_Release__c = true, Media_Release_Signature__c = 'Queen Amidala', Date_of_parent_media_release__c = (Date.today() - 10), RecordTypeId = recordTypeId('Contact', 'Spark Student') ); jediKids.add(leia); Contact han = new Contact ( FirstName = 'Han', LastName = 'Solo', Predicted_Graduation_Year_hs__c = String.ValueOf((Integer.ValueOf(giveFY())+2005)), Gender__c = 'Man', Race__c = 'MultiRacial', Birthdate = (Date.today()-(13*365)), Family_Application_Submitted__c = true, Date_of_Application__c = (Date.today() - 10), npsp__Primary_Affiliation__c = jediSchool, Parent_Consent_for_Evaluation__c = true, Date_of_parent_release_and_waiver__c = (Date.today() - 10), Release_Signed_By__c = 'Mrs. Solo', Eval_Release_Signature__c = 'Mrs. Solo', Date_of_parent_evaluation_consent__c = (Date.today() - 10), Media_and_Photo_Release__c = true, Media_Release_Signature__c = 'Mrs. Solo', Date_of_parent_media_release__c = (Date.today() - 10), RecordTypeId = recordTypeId('Contact', 'Spark Student') ); jediKids.add(han); Contact chewie = new Contact ( FirstName = 'Chewbacca', LastName = 'Wookie', Predicted_Graduation_Year_hs__c = String.ValueOf((Integer.ValueOf(giveFY())+2005)), Gender__c = 'Man', Race__c = 'MultiRacial', Birthdate = (Date.today()-(13*365)), Family_Application_Submitted__c = true, Date_of_Application__c = (Date.today() - 10), npsp__Primary_Affiliation__c = jediSchool, Parent_Consent_for_Evaluation__c = true, Date_of_parent_release_and_waiver__c = (Date.today() - 10), Release_Signed_By__c = 'Mama Wookie', Eval_Release_Signature__c = 'Mama Wookie', Date_of_parent_evaluation_consent__c = (Date.today() - 10), Media_and_Photo_Release__c = true, Media_Release_Signature__c = 'Mama Wookie', Date_of_parent_media_release__c = (Date.today() - 10), RecordTypeId = recordTypeId('Contact', 'Spark Student') ); jediKids.add(chewie); Contact beroo = new Contact ( FirstName = 'Aunt', LastName = 'Beroo', npsp__Primary_Affiliation__c = jediSchool, RecordTypeId = recordTypeId('Contact', 'Student Family Member') ); jediKids.add(beroo); Contact amidala = new Contact ( FirstName = 'Queen', LastName = 'Amidala', npsp__Primary_Affiliation__c = jediSchool, RecordTypeId = recordTypeId('Contact', 'Student Family Member') ); jediKids.add(amidala); Contact wookie = new Contact ( FirstName = 'Mrs.', LastName = 'Wookie', npsp__Primary_Affiliation__c = jediSchool, RecordTypeId = recordTypeId('Contact', 'Student Family Member') ); jediKids.add(wookie); Contact solo = new Contact ( FirstName = 'Mrs.', LastName = 'Solo', npsp__Primary_Affiliation__c = jediSchool, RecordTypeId = recordTypeId('Contact', 'Student Family Member') ); jediKids.add(solo); insert jediKids; //need to loop through putting the parents onto the kids' records Id auntBeroo = [SELECT Id from Contact WHERE LastName = 'Beroo'].Id; Id queenAmidala = [SELECT Id from Contact WHERE LastName = 'Amidala'].Id; Id mrsWookie = [SELECT Id from Contact WHERE LastName = 'Wookie' AND FirstName = 'Mrs.'].Id; Id mrsSolo = [SELECT Id from Contact WHERE LastName = 'Solo' AND FirstName = 'Mrs.'].Id; Contact lukeCreated = [SELECT Id from Contact WHERE LastName = 'Skywalker']; Contact leiaCreated = [SELECT Id from Contact WHERE LastName = 'Organa']; Contact chewieCreated = [SELECT Id from Contact WHERE LastName = 'Wookie' AND FirstName = 'Chewbacca']; Contact hanCreated = [SELECT Id from Contact WHERE LastName = 'Solo' AND FirstName = 'Han']; lukeCreated.Primary_Parent__c = auntBeroo; update lukeCreated; leiaCreated.Primary_Parent__c = queenAmidala; update leiaCreated; hanCreated.Primary_Parent__c = mrsSolo; update hanCreated; chewieCreated.Primary_Parent__c = mrsWookie; update chewieCreated; } //Method to create a budget campaign hierarchy for the current FY public static void createCampaignHierarchy(){ string fy = giveFY(); Campaign camptop = new Campaign ( Name = ('Hierarchy Top FY' + fy), RecordTypeId = recordTypeId('Campaign', 'Parent Campaign') ); insert camptop; List<Campaign> allcamps = new List<Campaign>(); Campaign corpParts = new Campaign ( Name = ('Corporate Partners FY'+ fy), ParentId = camptop.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(corpParts); Campaign indivs = new Campaign ( Name = ('Individuals FY'+ fy), ParentId = camptop.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(indivs); Campaign schoolFees = new Campaign ( Name = ('School Fees FY' + fy), ParentId = camptop.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(schoolFees); insert allcamps; } //Method to create a budget campaign hierarchy for the next FY public static void createNextYrCampaignHierarchy(){ integer nextfy = Integer.ValueOf(giveFY())+1; string fytouse = string.valueOf(nextfy); Campaign camptopnextyr = new Campaign ( Name = ('Hierarchy Top FY' + fytouse), RecordTypeId = recordTypeId('Campaign', 'Parent Campaign') ); insert camptopnextyr; List<Campaign> allcamps = new List<Campaign>(); Campaign corpParts = new Campaign ( Name = ('Corporate Partners FY'+ fytouse), ParentId = camptopnextyr.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(corpParts); Campaign indivs = new Campaign ( Name = ('Individuals FY'+ fytouse), ParentId = camptopnextyr.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(indivs); Campaign schoolFees = new Campaign ( Name = ('School Fees FY' + fytouse), ParentId = camptopnextyr.Id, RecordTypeId = recordTypeId('Campaign', 'Primary Campaign') ); allcamps.add(schoolFees); insert allcamps; } //Method to create two Accounts in Star Wars land, with two mentors on each. // This could be refactored to take region and a couple of contact names, then it could create companies in multiple regions. // To do this you'll also have to introduce some variable into contact and account names. // Another option to to leave this a method specific to Star Wars contacts. Then make a method for creating generic contacts with accounts. public static void createStarWarsCompaniesWithMentors(){ //Create Companies list <Account> accts = new list <Account>(); Account acct1 = new Account ( Name='Death Star Demolition', Account_Region__c = 'Star Wars (test data)', RecordTypeId = recordTypeId('Account', 'Corporate') ); accts.add (acct1); Account acct2 = new Account ( Name='Jedi Airlines', Account_Region__c = 'Star Wars (test data)', RecordTypeId = recordTypeId('Account', 'Corporate') ); accts.add (acct2); insert accts; //Create Mentors on Companies list <Contact> mentors = new list <Contact>(); Contact contact1 = new Contact ( FirstName = 'Blasty', LastName = 'Explosion', Volunteer_Region__c = 'Star Wars (test data)', npsp__Primary_Affiliation__c = acct1.Id, Email = 'blasty@deathstar.com', Creativity__c = 2, Communication__c = 2, Following_directions__c = 1, Helping_Others__c = 3, Influencing_others__c = 3, Operating_Machines__c = 2, Planning__c = 2, Reading_and_Writing__c = 2, Research_and_Logic__c = 3, Singing_and_acting__c = 1, Solving_problems__c = 1, Understanding_Shapes__c = 3, Working_with_others__c = 1, Attention_to_detail__c = 1, Career_Area_Industry__c = 'Entertainment', RecordTypeId = recordTypeId('Contact', 'Individual') ); mentors.add (contact1); Contact contact2 = new Contact ( FirstName = 'Heroic', LastName = 'Knight', Volunteer_Region__c = 'Star Wars (test data)', npsp__Primary_Affiliation__c = acct2.Id, Email = 'hero@jedi.com', Creativity__c = 3, Communication__c = 2, Following_directions__c = 3, Helping_Others__c = 3, Influencing_others__c = 3, Operating_Machines__c = 1, Planning__c = 2, Reading_and_Writing__c = 2, Research_and_Logic__c = 1, Singing_and_acting__c = 1, Solving_problems__c = 1, Understanding_Shapes__c = 1, Working_with_others__c = 1, Attention_to_detail__c = 1, Career_Area_Industry__c = 'Entertainment', RecordTypeId = recordTypeId('Contact', 'Individual') ); mentors.add (contact2); Contact contact3 = new Contact ( FirstName = 'Smash', LastName = 'Stuff', Volunteer_Region__c = 'Star Wars (test data)', npsp__Primary_Affiliation__c = acct1.Id, Email = 'smash@deathstar.com', Creativity__c = 1, Communication__c = 1, Following_directions__c = 3, Helping_Others__c = 1, Influencing_others__c = 1, Operating_Machines__c = 3, Planning__c = 2, Reading_and_Writing__c = 2, Research_and_Logic__c = 3, Singing_and_acting__c = 1, Solving_problems__c = 1, Understanding_Shapes__c = 3, Working_with_others__c = 3, Attention_to_detail__c = 1, Career_Area_Industry__c = 'Entertainment', RecordTypeId = recordTypeId('Contact', 'Individual') ); mentors.add (contact3); Contact contact4 = new Contact ( FirstName = 'Wave', LastName = 'McLightsaber', Volunteer_Region__c = 'Star Wars (test data)', npsp__Primary_Affiliation__c = acct2.Id, Email = 'wave@jedi.com', Creativity__c = 3, Communication__c = 3, Following_directions__c = 3, Helping_Others__c = 3, Influencing_others__c = 3, Operating_Machines__c = 1, Planning__c = 1, Reading_and_Writing__c = 1, Research_and_Logic__c = 3, Singing_and_acting__c = 3, Solving_problems__c = 3, Understanding_Shapes__c = 1, Working_with_others__c = 1, Attention_to_detail__c = 1, Career_Area_Industry__c = 'Entertainment', RecordTypeId = recordTypeId('Contact', 'Individual') ); mentors.add (contact4); insert mentors; } //Method to create the Star Wars Recruitment Campaigns for students and mentors public static void createStarWarsMentorRecruitCamp(){ string sy = giveFY(); List<Campaign> recruitCamps = new List<Campaign>(); Campaign mentrec = new Campaign ( Name = ('Star Wars (test) Mentor Recruitment Spring SY'+ sy), Type = 'Volunteer Recruitment', RecordTypeId = recordTypeId('Campaign', 'Mentor Recruitment'), School_Year__c = sy, Region__c = 'Star Wars (test)', Description = 'Mentor recruitment in the star wars region', Status = 'In Progress', StartDate = Date.today(), EndDate = Date.today()+180, IsActive = true ); recruitCamps.add(mentrec); Campaign sturec = new Campaign ( Name = ('Star Wars (test) Student Recruitment SY'+ sy), Type = 'Student Enrollment', RecordTypeId = recordTypeId('Campaign', 'Student Enrollment'), School_Year__c = sy, Region__c = 'Star Wars (test)', Description = 'student enrollment in the star wars region', Status = 'In Progress', StartDate = Date.today(), EndDate = Date.today()+180, IsActive = true ); recruitCamps.add(sturec); insert recruitCamps; //in Prod, statuses are set by AAKonsult. Need the statuses of Qualified and Cleared on the campaign. List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>(); CampaignMemberStatus qual = new CampaignMemberStatus ( CampaignID = mentrec.Id, Label = 'Qualified', HasResponded = true ); CampaignMemberStatus clear = new CampaignMemberStatus ( CampaignID = mentrec.Id, Label = 'Cleared', HasResponded = true ); cms.add(clear); cms.add(qual); insert cms; } public static void createStarWarsPrograms(){ List<yat__Program__c> progs = new List<yat__Program__c>(); Campaign stuEnrollCamp = [SELECT Id FROM Campaign WHERE Name LIKE '%Star Wars (test) Student Recruitment SY%']; yat__Program__c labProg = new yat__Program__c ( Name = ('Jedi Middle School Fall SY' + giveFY()), RecordTypeID = recordTypeId('yat__Program__c', 'Mentorship'), School__c = [SELECT Id FROM Account WHERE Name = 'Jedi Middle School'].Id, yat__Program_Season__c = 'Fall', yat__Program_Year__c = ('SY' + giveFY()), Student_Enrollment_Campaign__c = stuEnrollCamp.Id ); progs.add(labProg); yat__Program__c mentorship = new yat__Program__c ( Name = ('Jedi Middle School Spring SY' + giveFY()), RecordTypeID = recordTypeId('yat__Program__c', 'Spark Lab'), School__c = [SELECT Id FROM Account WHERE Name = 'Jedi Middle School'].Id, yat__Program_Season__c = 'Spring', yat__Program_Year__c = ('SY' + giveFY()), Student_Enrollment_Campaign__c = stuEnrollCamp.Id ); progs.add(mentorship); yat__Program__c hspProg = new yat__Program__c ( Name = ('Jedi Middle School HSP SY' + giveFY()), RecordTypeID = recordTypeId('yat__Program__c', 'HSP'), School__c = [SELECT Id FROM Account WHERE Name = 'Jedi Middle School'].Id, yat__Program_Year__c = ('SY' + giveFY()), Student_Enrollment_Campaign__c = stuEnrollCamp.Id ); progs.add(hspProg); insert progs; } //Put mentors onto campaign in status Qualified. (This will create Catalog Entries for them because there is a Process Builder.) public static void putMentorsOnCampaign(){ string mentorRecType = recordTypeId('Contact', 'Individual'); list<Contact> mentors = [SELECT Id from Contact WHERE RecordTypeId = '%mentorRecType%']; system.debug('mentors are ' + mentors); Campaign mentEnrollCamp = [SELECT Id FROM Campaign WHERE Name LIKE '%Star Wars (test) Mentor Recruitment Spring SY%']; system.debug('campaign is ' + mentEnrollCamp); //you've got the list of mentors, now loop through putting them on the campaign list<CampaignMember> cMembers = new list<CampaignMember>(); for (Contact currentMentor:mentors) { CampaignMember campMemb = new CampaignMember( ContactId = currentMentor.Id, CampaignID = mentEnrollCamp.Id, Status = 'Qualified' ); cMembers.add(campMemb); } system.debug('About to insert the list of campaign members, one for each mentor'); insert cMembers; } //Put students onto campaign. public static void putStudentsOnCampaign(){ string stuRecordType = recordTypeId('Contact', 'Spark Student'); list<Contact> students = [SELECT Id from Contact WHERE RecordTypeId = '%stuRecordType%']; system.debug('students are ' + students); Campaign stuEnrollCamp = [SELECT Id FROM Campaign WHERE Name LIKE '%Star Wars (test) Student Recruitment SY%']; system.debug('campaign is ' + stuEnrollCamp); //you've got the list of students, now loop through putting them on the campaign list<CampaignMember> cStudents = new list<CampaignMember>(); for (Contact currentStudent:students) { CampaignMember campMemb = new CampaignMember( ContactId = currentStudent.Id, CampaignID = stuEnrollCamp.Id ); cStudents.add(campMemb); } system.debug('About to insert the list of campaign members, one for each student'); insert cStudents; } public static void putProgramOnCatEnts(){ yat__Program__c prog = [SELECT Id from yat__Program__c WHERE Name LIKE '%Jedi Middle School Spring%' LIMIT 1]; system.debug('prog is ' + prog); list<Catalog_Entry__c> catentsForProgs = [SELECT Id from Catalog_Entry__c]; system.debug('catentsForProgs is ' + catentsForProgs); system.debug('About to update Assigned Programs on each catentForProg'); for (Catalog_Entry__c currentCatEnt:catentsForProgs) { currentCatEnt.Assigned_Program__c = prog.Id; update currentCatEnt; } } public static void putMentorships(){ string stuRecordType = recordTypeId('Contact', 'Spark Student'); yat__Program__c prog = [SELECT Id from yat__Program__c WHERE Name LIKE '%Jedi Middle School Spring%' LIMIT 1]; list<Contact> students = [SELECT Id, Full_Name__c, Spark_ID__c, npsp__Primary_Affiliation__c from Contact WHERE RecordTypeId = '%stuRecordType%']; system.debug('students are ' + students); list<Apprenticeship__c> apprents = new list<Apprenticeship__c>(); for (Contact currentStudent:students) { Apprenticeship__c mentorship = new Apprenticeship__c( Name = currentStudent.Full_Name__c + 'mentorship', School__c = currentStudent.npsp__Primary_Affiliation__c, Program__c = prog.Id, StageName__c = 'Student Requested', Creativity__c = 2, Attention_to_detail__c = 2, Communication__c = 3, Reading_and_Writing__c = 1, Planning__c = 3 ); apprents.add(mentorship); } insert apprents; } }And here's the execute anonymous I used to run it and create actual records:
tempSparkTestDataUtil.createJediStudents(); tempSparkTestDataUtil.createCampaignHierarchy(); tempSparkTestDataUtil.createNextYrCampaignHierarchy(); tempSparkTestDataUtil.createStarWarsCompaniesWithMentors(); tempSparkTestDataUtil.createStarWarsMentorRecruitCamp(); tempSparkTestDataUtil.createStarWarsPrograms(); tempSparkTestDataUtil.putMentorsOnCampaign(); tempSparkTestDataUtil.putStudentsOnCampaign(); tempSparkTestDataUtil.putProgramOnCatEnts(); tempSparkTestDataUtil.putMentorships();