-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
26Questions
-
57Replies
Help With Previously Working Apex Trigger
Good Morning:
I created the follwing Apex trigger back in June and it was working wonderfully (creating/deleting child records depending on some field updates from the parent record) and all of a sudden about two weeks ago, the trigger stopped creating the child records.
I opened the developer console and got the following error:
I have done some reading on other posts with the same error message and understand that it means nothing is being passed from the map over to the creation portion of the record. I guess I need to understand why and why it all of a sudden happened when it was working fine for four months.
Any help is greatly appreciated.
trigger RepSummaryCreation on Video_Turf__c (after update) { Map<ID, String> releaseName = new Map<ID, String>(); for (Video_Turf__c newDoor2: Trigger.new){ if(newDoor.Assigned__c == TRUE) releaseName.put(newDoor2.ID, newDoor.Name); } List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>(); for(Video_Turf__c turf1 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_One__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_One__c !='']){ newSummary.add(new Rep_Summary__c( Date__c = turf1.Date__c, Release__c = turf1.ID, Doors__c = turf1.Doors_Per_Rep__c, Name = turf1.Rep_One__c)); } insert newSummary; List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>(); for(Video_Turf__c turf2 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Two__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Two__c !='']){ newSummary2.add(new Rep_Summary__c( Date__c = turf2.Date__c, Release__c = turf2.ID, Doors__c = turf2.Doors_Per_Rep__c, Name = turf2.Rep_Two__c)); } insert newSummary2; List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>(); for(Video_Turf__c turf3 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Three__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Three__c !='']){ newSummary3.add(new Rep_Summary__c( Date__c = turf3.Date__c, Release__c = turf3.ID, Doors__c = turf3.Doors_Per_Rep__c, Name = turf3.Rep_Three__c)); } insert newSummary3; List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>(); for(Video_Turf__c turf4 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Four__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Four__c !='']){ newSummary4.add(new Rep_Summary__c( Date__c = turf4.Date__c, Release__c = turf4.ID, Doors__c = turf4.Doors_Per_Rep__c, Name = turf4.Rep_Four__c)); } insert newSummary4; List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>(); for(Video_Turf__c turf5 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Five__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Five__c !='']){ newSummary5.add(new Rep_Summary__c( Date__c = turf5.Date__c, Release__c = turf5.ID, Doors__c = turf5.Doors_Per_Rep__c, Name = turf5.Rep_Five__c)); } insert newSummary5; List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>(); for(Video_Turf__c turf6 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Six__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Six__c !='']){ newSummary6.add(new Rep_Summary__c( Date__c = turf6.Date__c, Release__c = turf6.ID, Doors__c = turf6.Doors_Per_Rep__c, Name = turf6.Rep_Six__c)); } insert newSummary6; List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>(); for(Video_Turf__c turf7 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Seven__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Seven__c !='']){ newSummary7.add(new Rep_Summary__c( Date__c = turf7.Date__c, Release__c = turf7.ID, Doors__c = turf7.Doors_Per_Rep__c, Name = turf7.Rep_Seven__c)); } insert newSummary7; List<Rep_Summary__c> newSummary8 = new List<Rep_Summary__c>(); for(Video_Turf__c turf8 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Eight__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Eight__c !='']){ newSummary8.add(new Rep_Summary__c( Date__c = turf8.Date__c, Release__c = turf8.ID, Doors__c = turf8.Doors_Per_Rep__c, Name = turf8.Rep_Eight__c)); } insert newSummary8; List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>(); for(Video_Turf__c turf9 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Nine__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Nine__c !='']){ newSummary9.add(new Rep_Summary__c( Date__c = turf9.Date__c, Release__c = turf9.ID, Doors__c = turf9.Doors_Per_Rep__c, Name = turf9.Rep_Nine__c)); } insert newSummary9; List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>(); for(Video_Turf__c turf10 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Ten__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Ten__c !='']){ newSummary10.add(new Rep_Summary__c( Date__c = turf10.Date__c, Release__c = turf10.ID, Doors__c = turf10.Doors_Per_Rep__c, Name = turf10.Rep_Ten__c)); } insert newSummary10; }
Thanks,
Hampton
- Hampton
- October 21, 2013
- Like
- 0
Help With Creation of Child Record
Hello:
I am having issues with an Apex trigger designed to create a child records (Rep_Summary__c) after update of parent record (Video_Turf__c) where certain conditions are met.
Here is my trigger:
trigger RepSummaryCreation on Video_Turf__c (after update) { List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>(); for(Video_Turf__c a: trigger.new){ if(a.Assigned__c==TRUE && a.Rep_One__c!=NULL){ newSummary.add(new Rep_Summary__c( Date__c = a.Date__c, Release__c = a.ID, Doors__c = a.Doors_Per_Rep__c, Name = a.Rep_One__c)); } } insert newSummary; List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>(); for(Video_Turf__c b: trigger.new){ if(b.Assigned__c==TRUE && b.Rep_Two__c!=NULL){ newSummary2.add(new Rep_Summary__c( Date__c = b.Date__c, Release__c = b.ID, Doors__c = b.Doors_Per_Rep__c, Name = b.Rep_Two__c)); } } insert newSummary2; List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>(); for(Video_Turf__c c: trigger.new){ if(c.Assigned__c==TRUE && c.Rep_Three__c!=''){ newSummary3.add(new Rep_Summary__c( Date__c = c.Date__c, Release__c = c.ID, Doors__c = c.Doors_Per_Rep__c, Name = c.Rep_Three__c)); } } insert newSummary3; List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>(); for(Video_Turf__c d: trigger.new){ if(d.Assigned__c==TRUE && d.Rep_Four__c!=''){ newSummary4.add(new Rep_Summary__c( Date__c = d.Date__c, Release__c = d.ID, Doors__c = d.Doors_Per_Rep__c, Name = d.Rep_Four__c)); } } insert newSummary4; List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>(); for(Video_Turf__c e: trigger.new){ if(e.Assigned__c==TRUE && e.Rep_Five__c!=''){ newSummary5.add(new Rep_Summary__c( Date__c = e.Date__c, Release__c = e.ID, Doors__c = e.Doors_Per_Rep__c, Name = e.Rep_Five__c)); } } insert newSummary5; List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>(); for(Video_Turf__c f: trigger.new){ if(f.Assigned__c==TRUE && f.Rep_Six__c!=''){ newSummary6.add(new Rep_Summary__c( Date__c = f.Date__c, Release__c = f.ID, Doors__c = f.Doors_Per_Rep__c, Name = f.Rep_Six__c)); } } insert newSummary6; List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>(); for(Video_Turf__c g: trigger.new){ if(g.Assigned__c==TRUE && g.Rep_Seven__c!=''){ newSummary7.add(new Rep_Summary__c( Date__c = g.Date__c, Release__c = g.ID, Doors__c = g.Doors_Per_Rep__c, Name = g.Rep_Seven__c)); } } insert newSummary7; List<Rep_Summary__c> newSummary8 = new List<Rep_Summary__c>(); for(Video_Turf__c h: trigger.new){ if(h.Assigned__c==TRUE && h.Rep_One__c!=''){ newSummary8.add(new Rep_Summary__c( Date__c = h.Date__c, Release__c = h.ID, Doors__c = h.Doors_Per_Rep__c, Name = h.Rep_Eight__c)); } } insert newSummary8; List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>(); for(Video_Turf__c i: trigger.new){ if(i.Assigned__c==TRUE && i.Rep_Nine__c!=''){ newSummary9.add(new Rep_Summary__c( Date__c = i.Date__c, Release__c = i.ID, Doors__c = i.Doors_Per_Rep__c, Name = i.Rep_Nine__c)); } } insert newSummary9; List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>(); for(Video_Turf__c j: trigger.new){ if(j.Assigned__c==TRUE && j.Rep_Ten__c!=''){ newSummary10.add(new Rep_Summary__c( Date__c = j.Date__c, Release__c = j.ID, Doors__c = j.Doors_Per_Rep__c, Name = j.Rep_Ten__c)); } } insert newSummary10; }
This seems to be a fairly straight forward trigger. What am I missing here that is not firing to create the records?
Thanks,
Hampton
- Hampton
- October 16, 2013
- Like
- 0
Simple Trigger To Delete Records
Hello:
I am attempting to delete a group of child records if a parent record is updated. I cannot seem to get the trigger to fire. Any thoughts? Am I over-complicating this?
When a Turf__c record is updated, I want to delete all associated Rep_Summary__c records.
trigger RepAssignmentDelete on Video_Turf__c (after update) { Set<ID> turfDoors = new Set<ID>(); for(Video_Turf__c turfRep : trigger.old) (turfDoors.add(turfRep.ID)); Map<String, Video_Turf__c> turfMap = new Map <String, Video_Turf__c>(); for(Video_Turf__c turf1 : [Select ID, Name, Date__c, Doors_Per_Rep__c from Video_Turf__c where ID in : turfDoors]) turfMap.put(turf1.Name, turf1); List<Rep_Summary__c> repsToDelete = new List<Rep_Summary__c>(); for(Rep_Summary__c a :[Select Name, Date__c, Doors__c, Release__c from Rep_Summary__c where Release__c in : turfDoors]) if(turfMap.containskey(a.Release__c)){ repsToDelete.add(a); } delete repsToDelete; }
Thanks,
Hampton
- Hampton
- June 17, 2013
- Like
- 0
Apex Required Field Issue
Hello:
I have the following code in place:
trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) { Map<String, Id> releaseName = new Map<String, Id>(); for (Video_Turf__c newDoor: Trigger.new) { if (newDoor.New_Resweep__c == 'Resweep' && newDoor.RecordTypeID != '012c00000004PLM') releaseName.put(newDoor.Name, newDoor.Id); } List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName.keyset() and Active_Video__c = 'N']){ newAssignment.add(new Assigned_Address__c( Address__c = address1.Unparsed_Address__c, Date_Assigned__c = system.today(), Turf__c = releaseName.get(address1.Release__c), Address_ID__c = address1.ID)); } insert newAssignment; }
The code creates child-records (Assigned_Address__c) to parent (Video_Turf__c) when a Video_Turf__c record is created.
The problem I am having is that depending on what the Name value is in the parent (Video_Turf__c), I get an error.
For example, I created a Video_Turf__c record with Blanchetta and Galbraith as the name. The trigger fired perfectly, the parent saved, the children were created and all was good.
When I tried to create a Video_Turf__c record with McKelvey and Winton as the name, I get an APEX error:
trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) {
Review all error messages below to correct your data.
Apex trigger VideoResweepAddressAssignment caused an unexpected exception, contact your administrator: VideoResweepAddressAssignment: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 24; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Turf]: [Turf]: Trigger.VideoResweepAddressAssignment: line 18, column 1
- Hampton
- June 11, 2013
- Like
- 0
Apex Schedulable Batch Error
Hello:
I have the following class, along with the scheudable class:
global class PenetrationSummaryConversion implements Database.Batchable<sObject>{ public string Query; public Penetration_Summary__c penSum; public List<Penetration_Summary__c> insertPenSumList = new List<Penetration_Summary__c>(); List<Group> QueueList = new List <Group>(); Map<String,Id> QueueMap = new Map<String,Id>(); List<Penetration_Staging_Records__c> addStagingList = new List<Penetration_Staging_Records__c>(); List<Penetration_Staging_Records__c> deleteStagingList = new List<Penetration_Staging_Records__c>(); global Database.querylocator start(Database.BatchableContext BC) { Query = 'Select Name, Id, OwnerId, LastModifiedDate, Release__c, CBW_Rev__c, CBW_Subs__c, Copper_CBAD_Rev__c, Copper_CBAD_Subs__c,'+ 'Copper_Voice_Rev__c, Copper_Voice_Subs__c, DSL_Rev__c, DSL_Subs__c, Fiber_CBAD_Rev__c,'+ 'Fiber_CBAD_Subs__c, Fiber_Internet_Rev__c, Fiber_Internet_Subs__c, Fiber_Voice_Rev__c, Fiber_Voice_Subs__c,'+ 'Premium_Speed_Rev__c, Premium_Speed_Subs__c, Month__c, Year__c, Video_Subs__c, Video_Rev__c'+ 'Copper_Voice_Penetration__c, Copper_CBAD_Penetration__c, DSL_Penetration__c, Premium_Speed_Penetration__c'+ 'Fiber_Voice_Penetration__c, Fiber_CBAD_Penetration__c, Fiber_Internet_Penetration__c, Video_Penetration__c'+ 'CBW_Penetration__c, Units__c'+ ' From Penetration_Staging_Records__c' + ' Where Batch_Processed__c=false'; return Database.getQueryLocator(Query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { QueueList=[Select Id, Name, DeveloperName, Type from Group where Type = 'Queue']; for(Group qObj:QueueList){ QueueMap.put( qobj.Name, qobj.Id); } for(sObject s: scope) { Penetration_Staging_Records__c tempPenRecords = (Penetration_Staging_Records__c)s; if(tempPenRecords.LastModifiedDate < System.now() || Test.isRunningTest()) addStagingList.add(tempPenRecords); } for(Penetration_Staging_Records__c tempRecord : addStagingList){ penSum = new Penetration_Summary__c(); penSum.Copper_Voice_Subs__c = tempRecord.Copper_Voice_Subs__c; penSum.Copper_CBAD_Subs__c = tempRecord.Copper_CBAD_Subs__c; penSum.DSL_Subs__c = tempRecord.DSL_Subs__c; penSum.Premium_Speed_Subs__c = tempRecord.Premium_Speed_Subs__c; penSum.Fiber_Voice_Subs__c = tempRecord.Fiber_Voice_Subs__c; penSum.Fiber_CBAD_Subs__c = tempRecord.Fiber_CBAD_Subs__c; penSum.Fiber_Internet_Subs__c = tempRecord.Fiber_Internet_Subs__c; penSum.Video_Subs__c = tempRecord.Video_Subs__c; penSum.CBW_Subs__c = tempRecord.CBW_Subs__c; penSum.Copper_Voice_Rev__c = tempRecord.Copper_Voice_Rev__c; penSum.Copper_CBAD_Rev__c = tempRecord.Copper_CBAD_Rev__c; penSum.DSL_Rev__c = tempRecord.DSL_Rev__c; penSum.Premium_Speed_Rev__c = tempRecord.Premium_Speed_Rev__c; penSum.Fiber_Voice_Rev__c = tempRecord.Fiber_Voice_Rev__c; penSum.Fiber_CBAD_Rev__c = tempRecord.Fiber_CBAD_Rev__c; penSum.Fiber_Internet_Rev__c = tempRecord.Fiber_Internet_Rev__c; penSum.Video_Rev__c = tempRecord.Video_Rev__c; penSum.CBW_Rev__c = tempRecord.CBW_Rev__c; penSum.Copper_Voice_Penetration__c = tempRecord.Copper_Voice_Penetration__c; penSum.Copper_CBAD_Penetration__c = tempRecord.Copper_CBAD_Penetration__c; penSum.DSL_Penetration__c = tempRecord.DSL_Penetration__c; penSum.Premium_Speed_Penetration__c = tempRecord.Premium_Speed_Penetration__c; penSum.Fiber_Voice_Penetration__c = tempRecord.Fiber_Voice_Penetration__c; penSum.Fiber_CBAD_Penetration__c = tempRecord.Fiber_CBAD_Penetration__c; penSum.Fiber_Internet_Penetration__c = tempRecord.Fiber_Internet_Penetration__c; penSum.Video_Penetration__c = tempRecord.Video_Penetration__c; penSum.CBW_Penetration__c = tempRecord.CBW_Penetration__c; penSum.Name = tempRecord.Release__c; insertPenSumList .add(penSum); deleteStagingList.add(tempRecord); } insert insertPenSumList; delete deleteStagingList; } global void finish(Database.BatchableContext BC) { } }
global class PenetrationSummaryConversionScheduled implements Schedulable{ global void execute(SchedulableContext SC) { PenetrationSummaryConversion penetrationsummaryinsert = new PenetrationSummaryConversion(); Database.executeBatch(penetrationsummaryinsert); } }
In the Sandbox, everything worked fine. Now that I am in production, the batch was scheduled and the Monitoring log says it ran, however, it did not convert the records. When I try to execute using the developer console I am getting error
line 1, column 14: Global type must be contained inside of a global class
Any thoughts on how to tweak this to work would be appreciated.
Thanks,
Hampton
- Hampton
- June 02, 2013
- Like
- 0
Extension Help
Hello:
I am trying to write an extension on custom object Zip_Code__c where the extension pulls all records from custom object Release__c if Zip_Code__c.Name = Release__c.Zip__c
Below is my code, I cannot figure this out...thoughts?
public class ZipCode { //private final Video_Release__c releases; private List<Video_Release__c> releases = new List<Video_Release__c>(); public ZipCode(Apexpages.StandardController controller) {String zip = controller.addFields(List<String> Name); releases = [Select ID, RecordTypeID, Platform__c, Name, Released__c, Last_Swept__c, Current_Penetration__c, Video_Qualified_Addresses__c From Video_Release__c Where Zip__c = :zip ORDER BY Name]; } public List<Video_Release__c> getreleases() { return releases; } public PageReference save() { update releases; return null; } }
I am getting error Unexpected Token List at Line 6
Thanks,
Hampton
- Hampton
- June 01, 2013
- Like
- 0
Question on Apex & Custom Formula Fields
Hello:
I have the following Apex issue I was looking for some guidance on.
The fields Copper_Voice_Penetration__c, Copper_CBAD_Penetration__c, Fiber_Voice_Penetration__c, Fiber_CBAD_Penetration__c, DSL_Penetration__c, Fiber_Internet_Penetration__c and Video_Penetration__c are all custom formula fields yet I need their values in my IF statements to do the calculations.
I have done some reading and now realize that those formula field values aren't calculated until after insert and I am running a before insert apex. Are there any other workarounds to this or any other suggestions anyone can provide?
Thanks,
Hampton
trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) { Set<String> penSumName = new Set<String>(); for (Penetration_Summary__c pen : Trigger.new) { penSumName.add(pen.Name); } Map<String, Video_Release__c> releaseSumMap = new Map<String, Video_Release__c>(); for(Video_Release__c release1:[Select ID, Name, Video_Qualified_Addresses__c, Copper_Voice_Rev_Share__r.Low_End_Tier_One__c, Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c, Copper_Voice_Rev_Share__r.Low_End_Tier_Three__c, Copper_Voice_Rev_Share__r.High_End_Tier_One__c, Copper_Voice_Rev_Share__r.High_End_Tier_Two__c, Copper_Voice_Rev_Share__r.High_End_Tier_Three__c, Copper_Voice_Rev_Share__r.Tier_One__c, Copper_Voice_Rev_Share__r.Tier_Two__c, Copper_Voice_Rev_Share__r.Tier_Three__c, DSL_Rev_Share__r.Low_End_Tier_One__c, DSL_Rev_Share__r.Low_End_Tier_Two__c, DSL_Rev_Share__r.Low_End_Tier_Three__c, DSL_Rev_Share__r.High_End_Tier_One__c, DSL_Rev_Share__r.High_End_Tier_Two__c, DSL_Rev_Share__r.High_End_Tier_Three__c, DSL_Rev_Share__r.Tier_One__c, DSL_Rev_Share__r.Tier_Two__c, DSL_Rev_Share__r.Tier_Three__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Voice_Rev_Share__r.High_End_Tier_One__c, Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c, Fiber_Voice_Rev_Share__r.High_End_Tier_Three__c, Fiber_Voice_Rev_Share__r.Tier_One__c, Fiber_Voice_Rev_Share__r.Tier_Two__c, Fiber_Voice_Rev_Share__r.Tier_Three__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Internet_Rev_Share__r.High_End_Tier_One__c, Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c, Fiber_Internet_Rev_Share__r.High_End_Tier_Three__c, Fiber_Internet_Rev_Share__r.Tier_One__c, Fiber_Internet_Rev_Share__r.Tier_Two__c, Fiber_Internet_Rev_Share__r.Tier_Three__c,Video_Rev_Share__r.Low_End_Tier_One__c, Video_Rev_Share__r.Low_End_Tier_Two__c, Video_Rev_Share__r.Low_End_Tier_Three__c, Video_Rev_Share__r.Low_End_Tier_Four__c, Video_Rev_Share__r.Low_End_Tier_Five__c,Video_Rev_Share__r.High_End_Tier_One__c, Video_Rev_Share__r.High_End_Tier_Two__c, Video_Rev_Share__r.High_End_Tier_Three__c, Video_Rev_Share__r.High_End_Tier_Four__c, Video_Rev_Share__r.High_End_Tier_Five__c, Video_Rev_Share__r.Tier_One__c, Video_Rev_Share__r.Tier_Two__c, Video_Rev_Share__r.Tier_Three__c, Video_Rev_Share__r.Tier_Four__c, Video_Rev_Share__r.Tier_Five__c, CBW_Rev_Share__r.Tier_One__c, CBW_Rev_Share__r.Low_End_Tier_One__c, CBW_Rev_Share__r.High_End_Tier_One__c from Video_Release__c Where Name IN :penSumName and Rev_Share_Paid__c = TRUE]) { releaseSumMap.put(release1.Name, release1); } List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>(); for(Penetration_Summary__c c : trigger.new) { if(releaseSumMap.containsKey(c.Name)) { if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_One__c != null && c.Copper_Voice_Rev__c != null) { if(c.Copper_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Copper_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_One__c)) c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); else { if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Two__c != null && c.Copper_Voice_Rev__c != null) { if(c.Copper_Voice_Penetration__c >= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Copper_Voice_Penetration__c <= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_Two__c) c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); else { if(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Three__c != null && c.Copper_Voice_Rev__c != null) { c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); } } } } { if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_One__c != null && c.DSL_Rev__c != null) { if(c.DSL_Penetration__c >=(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_One__c) && c.DSL_Penetration__c <= (releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_One__c)) c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_One__c) /100) * c.DSL_Rev__c; else { if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Two__c != null && c.DSL_Rev__c != null) { if(c.DSL_Penetration__c >= releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_Two__c && c.DSL_Penetration__c <= releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_Two__c) c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Two__c) /100) * c.DSL_Rev__c; else { if(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Three__c != null && c.DSL_Rev__c != null) { c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Three__c) /100) * c.DSL_Rev__c; } } } } } } } { if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_One__c != null && c.Fiber_Voice_Rev__c != null) { if(c.Fiber_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_One__c)) c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); else { if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Two__c != null && c.Fiber_Voice_Rev__c != null) { if(c.Fiber_Voice_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Voice_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c) c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); else { if(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Three__c != null && c.Fiber_Voice_Rev__c != null) { c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); } } } } } } { if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_One__c != null && c.Fiber_Internet_Rev__c != null) { if(c.Fiber_Internet_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Internet_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_One__c)) c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_One__c) /100) * c.Fiber_Internet_Rev__c; else { if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Two__c != null && c.Fiber_Internet_Rev__c != null) { if(c.Fiber_Internet_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Internet_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c) c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Two__c) /100) * c.Fiber_Internet_Rev__c; else { if(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Three__c != null && c.Fiber_Internet_Rev__c != null) { c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Three__c) /100) * c.Fiber_Internet_Rev__c; } } } } } } { if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_One__c != null && c.Video_Rev__c != null) { if(c.Video_Penetration__c >=(releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_One__c) && c.Video_Penetration__c <= (releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_One__c)) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_One__c) /100) * c.Video_Rev__c; else { if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Two__c != null && c.Video_Rev__c != null) { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Two__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Two__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Two__c) /100) * c.Video_Rev__c; else { if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Three__c != null && c.Video_Rev__c != null) { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Three__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Three__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).VIDEO_Rev_Share__r.Tier_Three__c) /100) * c.Video_Rev__c; else { if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Four__c != null && c.Video_Rev__c != null) { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Four__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Four__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Four__c) /100) * c.Video_Rev__c; else { if(releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Five__c != null && c.Video_Rev__c != null) { c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Five__c) /100) * c.Video_Rev__c; } } } } } } } } } } if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null) { c.CBW_Rev_Share__c = ((releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c) /100) * c.CBW_Rev__c; } c.Units__c = (releaseSumMap.get(c.Name).Video_Qualified_Addresses__c); penForUpdate.add(c); } } }
- Hampton
- May 26, 2013
- Like
- 0
Need Help Scheduling Batch Process
Good Morning:
I have the following Apex Class which converts records froma temporary staging custom object over to new records in another custom object. I am having issues getting the class set up to schedule this run. Any help is appreciated.
The issue in the scheduleble class is in
Penetration_Summary__c convertBatch = new Penetration_Summary__c(query);
Hampton
global class PenetrationSummaryConversion implements Database.Batchable<sObject>{ public string Query; public Penetration_Summary__c penSum; public List<Penetration_Summary__c> insertPenSumList = new List<Penetration_Summary__c>(); List<Group> QueueList = new List <Group>(); Map<String,Id> QueueMap = new Map<String,Id>(); List<Penetration_Staging_Records__c> addStagingList = new List<Penetration_Staging_Records__c>(); List<Penetration_Staging_Records__c> deleteStagingList = new List<Penetration_Staging_Records__c>(); global Database.querylocator start(Database.BatchableContext BC) { Query = 'Select Name, Id, OwnerId, LastModifiedDate, Release__c, CBW_Rev__c, CBW_Subs__c, Copper_CBAD_Rev__c, Copper_CBAD_Subs__c,'+ 'Copper_Voice_Rev__c, Copper_Voice_Subs__c, DSL_Rev__c, DSL_Subs__c, Fiber_CBAD_Rev__c,'+ 'Fiber_CBAD_Subs__c, Fiber_Internet_Rev__c, Fiber_Internet_Subs__c, Fiber_Voice_Rev__c, Fiber_Voice_Subs__c,'+ 'Premium_Speed_Rev__c, Premium_Speed_Subs__c, Units__c, Video_Subs__c, Video_Rev__c,'+ ' From Penetration_Staging_Records__c' + ' Where Batch_Processed__c=false'; return Database.getQueryLocator(Query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { QueueList=[Select Id, Name, DeveloperName, Type from Group where Type = 'Queue']; for(Group qObj:QueueList){ QueueMap.put( qobj.Name, qobj.Id); } for(sObject s: scope) { Penetration_Staging_Records__c tempPenRecords = (Penetration_Staging_Records__c)s; if(tempPenRecords.LastModifiedDate < System.now() || Test.isRunningTest()) addStagingList.add(tempPenRecords); } for(Penetration_Staging_Records__c tempRecord : addStagingList){ penSum = new Penetration_Summary__c(); penSum.Copper_Voice_Subs__c = tempRecord.Copper_Voice_Subs__c; penSum.Copper_CBAD_Subs__c = tempRecord.Copper_CBAD_Subs__c; penSum.DSL_Subs__c = tempRecord.DSL_Subs__c; penSum.Premium_Speed_Subs__c = tempRecord.Premium_Speed_Subs__c; penSum.Fiber_Voice_Subs__c = tempRecord.Fiber_Voice_Subs__c; penSum.Fiber_CBAD_Subs__c = tempRecord.Fiber_CBAD_Subs__c; penSum.Fiber_Internet_Subs__c = tempRecord.Fiber_Internet_Subs__c; penSum.Video_Subs__c = tempRecord.Video_Subs__c; penSum.CBW_Subs__c = tempRecord.CBW_Subs__c; penSum.Copper_Voice_Rev__c = tempRecord.Copper_Voice_Rev__c; penSum.Copper_CBAD_Rev__c = tempRecord.Copper_CBAD_Rev__c; penSum.DSL_Rev__c = tempRecord.DSL_Rev__c; penSum.Premium_Speed_Rev__c = tempRecord.Premium_Speed_Rev__c; penSum.Fiber_Voice_Rev__c = tempRecord.Fiber_Voice_Rev__c; penSum.Fiber_CBAD_Rev__c = tempRecord.Fiber_CBAD_Rev__c; penSum.Fiber_Internet_Rev__c = tempRecord.Fiber_Internet_Rev__c; penSum.Video_Rev__c = tempRecord.Video_Rev__c; penSum.CBW_Rev__c = tempRecord.CBW_Rev__c; penSum.Units__c = tempRecord.Units__c; penSum.Name = tempRecord.Release__c; insertPenSumList .add(penSum); deleteStagingList.add(tempRecord); } insert insertPenSumList; delete deleteStagingList; } global void finish(Database.BatchableContext BC) { } }
global class PenetrationSummaryConversionScheduled implements Schedulable{ global void execute(SchedulableContext sc){ String query = 'Select Name, Id, OwnerId, LastModifiedDate, Release__c, CBW_Rev__c, CBW_Subs__c, Copper_CBAD_Rev__c, Copper_CBAD_Subs__c,'+ 'Copper_Voice_Rev__c, Copper_Voice_Subs__c, DSL_Rev__c, DSL_Subs__c, Fiber_CBAD_Rev__c,'+ 'Fiber_CBAD_Subs__c, Fiber_Internet_Rev__c, Fiber_Internet_Subs__c, Fiber_Voice_Rev__c, Fiber_Voice_Subs__c,'+ 'Premium_Speed_Rev__c, Premium_Speed_Subs__c, Units__c, Video_Subs__c, Video_Rev__c,'+ ' From Penetration_Staging_Records__c' + ' Where Batch_Processed__c=false'; Penetration_Summary__c convertBatch = new Penetration_Summary__c(query); Id BatchProcessId = Database.ExecuteBatch(convertBatch); } }
- Hampton
- May 26, 2013
- Like
- 0
System.LimitException: Too many SOQL queries: 101 Question
Good Evening All:
I have the code below that is designed to create child records (Assigned_Address__c) on Turf__c when the Turf__c record is created. I am getting the dreaded System.LimitException: Too many SOQL queries: 101error message when I attempt to create a Turf__c record. I thought I had written this to properly handle the creation of child records. Depending on the conditions, the number of child records created at one time would be between 10 and 500. Do I need to explore another option than this?
As always, your help is much appreciated.
Thanks!
Hampton
trigger NewAddressAssignment on Turf__c (after insert) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Turf__c> turfList = [Select ID, Name, New_Resweep__c from Turf__c where Name in : releaseName and New_Resweep__c = 'New']; List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : turfList) { Assigned_Address__c a = new Assigned_Address__c(); a.Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c; a.Turf__c = newRelease.ID; a.Address_ID__c = addressmap.get(newRelease.Name).ID; newAssignment.add(a); } insert newAssignment; } } }
- Hampton
- May 20, 2013
- Like
- 0
Need Help With Batch Apex
Hello:
I have the following batch process written:
global class BatchToCreateAssignedAddress implements Database.Batchable<sObject>, Database.stateful { global string strqry=''; global BatchToCreateAssignedAddress(String q) { strqry=q; } global Database.Querylocator start (Database.Batchablecontext BC) { return Database.getQueryLocator(strqry); } global void execute (Database.Batchablecontext BC, list<sObject> scope) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: [Select Name from Turf__c ]) { releaseName.add(newDoor.Name); } system.debug('*************releaseName :'+ releaseName); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1);} system.debug('*************addressMap size :'+ addressMap.size()); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : [Select ID, Name, New_Resweep__c from Turf__c where Name = : releaseName] ) { system.debug('*************newRelease Name :'+ newRelease.Name); if(addressMap.containsKey(newRelease.Name)){ if(newRelease.New_Resweep__c == 'New'){ newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, Turf__c = newRelease.ID, Address_ID__c = addressMap.get(newRelease.Name).ID)); system.debug('*************newAssignmentList Size :'+ newAssignment.size()); } } insert newAssignment; } } global void finish(Database.Batchablecontext BC) { } }
trigger NewAddressAssignment on Turf__c (after insert){ Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); string strquery= 'Select Id from Turf__c where ID in : releaseName'; BatchToCreateAssignedAddress objBatchToCreateAssignedAddress=new BatchToCreateAssignedAddress(strquery); Database.executeBatch(objBatchToCreateAssignedAddress); } }
According to my debug logs, in the trigger, the variable releaseName does not exist (Line 10). I have been trying to figure this out for hours and have had no luck.
The problem I am trying to solve for, is that when a new Turf__c record is created, there are child records (Assigned_Address__c) created as well. The number of child records created could be anywhere from 10 - 500.
Any help you can provide is much appreciated.
Thanks in advance!
Hampton
- Hampton
- May 19, 2013
- Like
- 0
Need Help with Too Many SOQL Queries
Hello:
Please see my code below. What I am doing is creating child records (Assigned_Address__c) on custom object Turf__c when Turf__c is created. The number of child records can be in the multiple hundreds at times. How can I tweak this Apex to avoid hitting the SOQL Limit (which is currently happening)?
trigger NewAddressAssignment on Turf__c (after insert) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : trigger.new) { if(addressMap.containsKey(newRelease.Name)){ if(newRelease.New_Resweep__c == 'New'){ newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, Turf__c = newRelease.ID, Address_ID__c = addressMap.get(newRelease.Name).ID)); } } insert newAssignment; } } } }
Thanks!
Hampton
- Hampton
- May 19, 2013
- Like
- 0
Help with Recurssion Error
Hello:
I am getting a recurrsion error on the trigger below and am not sure how to correct it? I am trying to update the field Orders__c on custom object Resweep_Assignment__c with a count of child records Assigned_Address__c where Service_Order_Number__c != ''
Any help?
Thanks,
Hampton
trigger OrderCount on Assigned_Address__c (after delete, after update) { Set<ID> ResweepIds = new Set<ID>(); if(trigger.isInsert || trigger.isUpdate){ for(Assigned_Address__c assigned : trigger.new){ if(assigned.Rep__c != '') ResweepIds.add(assigned.Resweep_Assignment__c); } } if(trigger.isDelete){ for(Assigned_Address__c assigned : trigger.old){ ResweepIds.add(assigned.Resweep_Assignment__c); } } Map<ID, Double> ResweepMap = new Map<ID, Double> (); for(AggregateResult ar : [select Resweep_Assignment__c, count(ID) from Assigned_Address__c where Resweep_Assignment__c in : ResweepIds and Service_Order_Number__c != '' and Rep__c != '' group by Resweep_Assignment__c]){ ResweepMap.put((ID)ar.get('Resweep_Assignment__c'),(Double)ar.get('expr0')); } List <Resweep_Assignment__c> ResweepToUpdate = new List<Resweep_Assignment__c>(); for(Resweep_Assignment__c a : [Select Id, Orders__c from Resweep_Assignment__c where Id IN :ResweepIds]){ Double OrderCount = ResweepMap.get(a.Id); a.Orders__c = OrderCount; ResweepToUpdate.add(a); } update ResweepToUpdate; }
- Hampton
- May 17, 2013
- Like
- 0
Apex Record Count
Hello:
I have two unrelated custo objects that share a common key (Service_Order__c.Release__c = Resweep_Assignment__c.Name).
Both custom objects have date fields: Service_Order__c.Order_App_Date__c and Resweep_Assignment__c.Date_Assigned__c
What I am looking to do is to update a number field (Orders__c) with a count of Service_Order__c records where Service_Order__c.Order_App_Date__c is within 30 days of Resweep_Assignment__c.Date_Assigned__c.
Here is what I have, which is not saving. Any suggestions are greatly appreciated.
trigger ServiceOrderCount on Service_Order__c (after insert) { Set<String> serviceOrder = new Set<String>(); for (Service_Order__c sord : Trigger.new) { serviceOrder.add(sord.Release__c); Map<String, Service_Order__c> sordMap = new Map<String,Service_Order__c>(); for(Service_Order__c sord1 : [Select Name, Order_App_Date__c, Release__c from Service_Order__c where Name in : serviceOrder and Video__c=1]); sordMap.put(sord1.Release__c, sord1); Map<String, Resweep_Assignment__c> resweepMap = new Map<String, Resweep_Assignment__c>(); for(Resweep_Assignment__c resweep1: [Select Name, Date_Assigned__c, Orders__c from Resweep_Assignment__c where Name in : serviceOrder]); resweepMap.put(resweep1.Name, resweep1); Set<String> resweepDate = new Set<String>(); resweepDate.add(resweep1.Date_Assigned); integer i = [Select Count() from Service_Order__c where Name =: serviceOrder and (resweepDate < Order_App_Date__c < resweepDate+30)]; List<Resweep_Assignment__c> resweepToUpdate = new List<Resweep_Assignment__c>(); for(Resweep_Assignment__c a : [Select Name, Date_Assigned__c, Orders__c where Name in : serviceOrder]){ if(serviceOrderMap.containskey(a.Name){ a.Orders__c = i; resweepToUpdate.add(a); } } update resweepToUpdate }
Thanks,
Hampton
- Hampton
- May 17, 2013
- Like
- 0
Help With Integer in Apex
Hello:
I have the following code which is designed to insert multiple child recods (Assigned_Address__c) when a parent is created (New_Door_Assigned__c).
What I need to do is after the child records are inserted, update a field on the child record with a value from the parent record, dependent upon what the index/integer value of the row is.
For example, if there are 100 child records, I would want the first 50 to have a Rep__c value = New_Door_Assignment__c.Sales_Rep_One__c, the second 50 to New_Door_Assignment__c.Sales_Rep_Two__c, etc until all child records have a Rep_Name__c value.
The code to create the child records works and I have written test class for it. I am now attempting to add in the logic around this starting with
List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment);
assigned.sort(Address__c);
Attached is my full code. Any guidance is appreciated.
Thanks,
Hampton
trigger AddressAssignment on New_Door_Assignment__c (before update) { Set<String> releaseName = new Set<String>(); for (New_Door_Assignment__c newDoor: Trigger.new) { if(newDoor.Assigned__c = TRUE){ {releaseName.add(newDoor.Name);} } Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(New_Door_Assignment__c newRelease : trigger.new) { if(addressMap.containsKey(newRelease.Name)) newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, New_Door_Assignment__c = newRelease.ID)); Date_Assigned__c = newRelease.Date_Released__c, } List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment); assigned.sort(Address__c); List<Integer> int = new List<Integer>(assigned); if(integer i < newRelease.Doors_Per_Rep__c){ assigned.Rep__c = newRelease.Sales_Rep_One__c else { if(integer i < (2*newRelease.Doors_Per_Rep__c)){ assigned.Rep__c = newRelease.Sales_Rep_Two__c else { assigned.Rep__c = newRelease.Sales_Rep_Three__c; } } } update newAssignment; insert newAssignment; } } }
- Hampton
- May 13, 2013
- Like
- 0
Batch Process/Custom Button to Field Update
Hello:
I currently have the following business situation.
We are running a door to door sales app that has been custom built on the Force.com platform.
I have a custom object (Site__c) with a trigger that when a Site__c record gets created, so does a record in custom object New_Door_Assignment__c. These two records have the same Name.
I have a second trigger that when New_Door_Assignment__c is created, multiple child records of custom object Assigned_Address__c get created.
New_Door_Assignment__c has five custom fields: Sales_Rep_One__c, Sales_Rep_Two__c, Sales_Rep_Three__c, Sales_Rep_Four__c and Sales_Rep_Five__c along with a custom formula field Doors_Per_Rep__c.
For example:
New_Door_Assignment__c.Sales_Rep_One__c = John Doe
New_Door_Assignment__c.Sales_Rep_Two__c = Jane Doe
New_Door_Assignment__c.Doors_Per_Rep__c = 50
What I want to do is to take the 100 child records (Assigned_Address__c), sort by custom field Street_Address__c, and update the custom field Rep__c on the first 50 with New_Door_Assignment__c.Sales_Rep_One__c, the second 50 with New_Door_Assignment__c.Sales_Rep_Two__c
From a business perspective, I would like to do this through a custom button because when a New_Door_Assignment__c record is created, we do not know who will be Sales_Rep_One__c, Sales_Rep_Two__c, etc
Is this something that is doable and any guidance I can get to start would be appreciated.
Thanks!
Hampton
- Hampton
- May 13, 2013
- Like
- 0
Test Class: First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
Hello:
I am getting the above error on my test class. Below are my trigger and test class.
trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) { Set<String> penSumName = new Set<String>(); for (Penetration_Summary__c pen : Trigger.new) { if(pen.Current__c == TRUE) { penSumName.add(pen.Name); } } Map<String, Release__c> releaseSumMap = new Map<String, Release__c>(); for(Release__c release1:[Select ID, Name, Qualified_Addresses__c, Copper_Voice_Rev_Share__r.Low_End_Tier_One__c, Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c, Copper_Voice_Rev_Share__r.Low_End_Tier_Three__c, Copper_Voice_Rev_Share__r.High_End_Tier_One__c, Copper_Voice_Rev_Share__r.High_End_Tier_Two__c, Copper_Voice_Rev_Share__r.High_End_Tier_Three__c, Copper_Voice_Rev_Share__r.Tier_One__c, Copper_Voice_Rev_Share__r.Tier_Two__c, Copper_Voice_Rev_Share__r.Tier_Three__c, DSL_Rev_Share__r.Low_End_Tier_One__c, DSL_Rev_Share__r.Low_End_Tier_Two__c, DSL_Rev_Share__r.Low_End_Tier_Three__c, DSL_Rev_Share__r.High_End_Tier_One__c, DSL_Rev_Share__r.High_End_Tier_Two__c, DSL_Rev_Share__r.High_End_Tier_Three__c, DSL_Rev_Share__r.Tier_One__c, DSL_Rev_Share__r.Tier_Two__c, DSL_Rev_Share__r.Tier_Three__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c, Fiber_Voice_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Voice_Rev_Share__r.High_End_Tier_One__c, Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c, Fiber_Voice_Rev_Share__r.High_End_Tier_Three__c, Fiber_Voice_Rev_Share__r.Tier_One__c, Fiber_Voice_Rev_Share__r.Tier_Two__c, Fiber_Voice_Rev_Share__r.Tier_Three__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c, Fiber_Internet_Rev_Share__r.Low_End_Tier_Three__c, Fiber_Internet_Rev_Share__r.High_End_Tier_One__c, Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c, Fiber_Internet_Rev_Share__r.High_End_Tier_Three__c, Fiber_Internet_Rev_Share__r.Tier_One__c, Fiber_Internet_Rev_Share__r.Tier_Two__c, Fiber_Internet_Rev_Share__r.Tier_Three__c,Video_Rev_Share__r.Low_End_Tier_One__c, Video_Rev_Share__r.Low_End_Tier_Two__c, Video_Rev_Share__r.Low_End_Tier_Three__c, Video_Rev_Share__r.Low_End_Tier_Four__c, Video_Rev_Share__r.Low_End_Tier_Five__c,Video_Rev_Share__r.High_End_Tier_One__c, Video_Rev_Share__r.High_End_Tier_Two__c, Video_Rev_Share__r.High_End_Tier_Three__c, Video_Rev_Share__r.High_End_Tier_Four__c, Video_Rev_Share__r.High_End_Tier_Five__c, Video_Rev_Share__r.Tier_One__c, Video_Rev_Share__r.Tier_Two__c, Video_Rev_Share__r.Tier_Three__c, Video_Rev_Share__r.Tier_Four__c, Video_Rev_Share__r.Tier_Five__c, CBW_Rev_Share__r.Tier_One__c, CBW_Rev_Share__r.Low_End_Tier_One__c, CBW_Rev_Share__r.High_End_Tier_One__c from Release__c Where Name IN :penSumName and Rev_Share_Paid__c = TRUE]) { releaseSumMap.put(release1.Name, release1); } List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>(); for(Penetration_Summary__c c : trigger.new) { if(releaseSumMap.containsKey(c.Name)) { if(c.Copper_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Copper_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_One__c)) c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); else { if(c.Copper_Voice_Penetration__c >= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Copper_Voice_Penetration__c <= releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.High_End_Tier_Two__c) c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); else { c.Copper_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Copper_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Copper_Voice_Rev__c+c.copper_CBAD_Rev__c); } } { if(c.DSL_Penetration__c >=(releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_One__c) && c.DSL_Penetration__c <= (releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_One__c)) c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_One__c) /100) * c.DSL_Rev__c; else { if(c.DSL_Penetration__c >= releaseSumMap.get(c.Name).DSL_Rev_Share__r.Low_End_Tier_Two__c && c.DSL_Penetration__c <= releaseSumMap.get(c.Name).DSL_Rev_Share__r.High_End_Tier_Two__c) c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Two__c) /100) * c.DSL_Rev__c; else { c.DSL_Rev_Share__c = ((releaseSumMap.get(c.Name).DSL_Rev_Share__r.Tier_Three__c) /100) * c.DSL_Rev__c; } } } { if(c.Fiber_Voice_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Voice_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_One__c)) c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_One__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); else { if(c.Fiber_Voice_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Voice_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.High_End_Tier_Two__c) c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Two__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); else { c.Fiber_Voice_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Voice_Rev_Share__r.Tier_Three__c) /100) * (c.Fiber_Voice_Rev__c+c.Fiber_CBAD_Rev__c); } } } { if(c.Fiber_Internet_Penetration__c >=(releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_One__c) && c.Fiber_Internet_Penetration__c <= (releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_One__c)) c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_One__c) /100) * c.Fiber_Internet_Rev__c; else { if(c.Fiber_Internet_Penetration__c >= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Low_End_Tier_Two__c && c.Fiber_Internet_Penetration__c <= releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.High_End_Tier_Two__c) c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Two__c) /100) * c.Fiber_Internet_Rev__c; else { c.Fiber_Internet_Rev_Share__c = ((releaseSumMap.get(c.Name).Fiber_Internet_Rev_Share__r.Tier_Three__c) /100) * c.Fiber_Internet_Rev__c; } } } { if(c.Video_Penetration__c >=(releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_One__c) && c.Video_Penetration__c <= (releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_One__c)) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_One__c) /100) * c.Video_Rev__c; else { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Two__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Two__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Two__c) /100) * c.Video_Rev__c; else { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Three__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Three__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).VIDEO_Rev_Share__r.Tier_Three__c) /100) * c.Video_Rev__c; else { if(c.Video_Penetration__c >= releaseSumMap.get(c.Name).Video_Rev_Share__r.Low_End_Tier_Four__c && c.Video_Penetration__c <= releaseSumMap.get(c.Name).Video_Rev_Share__r.High_End_Tier_Four__c) c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Four__c) /100) * c.Video_Rev__c; else { c.Video_Rev_Share__c = ((releaseSumMap.get(c.Name).Video_Rev_Share__r.Tier_Five__c) /100) * c.Video_Rev__c; } } } } } c.CBW_Rev_Share__c = ((releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c) /100) * c.CBW_Rev__c; c.Units__c = (releaseSumMap.get(c.Name).Qualified_Addresses__c); penForUpdate.add(c); } } }
@isTest private class RevShareSummaryTestClass { static testMethod void validateRevShareSummary() { Release__c a = new Release__c (Name = '123 Main', Qualified_Addresses__c = 100, Rev_Share_Paid__c = FALSE); insert a; Penetration_Summary__c b = new Penetration_Summary__c (Name = '123 Main', Current__c = TRUE, Copper_Voice_Rev__c = 1000, Copper_CBAD_Rev__c = 1000, DSL_Rev__c = 1000, Fiber_Voice_Rev__c = 1000, Fiber_CBAD_Rev__c = 1000, Fiber_Internet_Rev__c = 1000, Video_Rev__c = 1000, CBW_Rev__c = 1000, Copper_Voice_Subs__c = 10, Copper_CBAD_Subs__c = 5, DSL_Subs__c = 2, Fiber_Voice_Subs__c = 10, Fiber_CBAD_Subs__c = 5, Fiber_Internet_Subs__c = 20, Video_Subs__c = 25, CBW_Subs__c = 5, Month__c = '05 May', Year__c = '2013'); insert b; system.assertEquals(NULL, b.Copper_Voice_Rev_Share__c); system.assertEquals(NULL, b.DSL_Rev_Share__c); system.assertEquals(NULL, b.Fiber_Voice_Rev_Share__c); system.assertEquals(NULL, b.Fiber_Internet_Rev_Share__c); system.assertEquals(NULL, b.Video_Rev_Share__c); system.assertEquals(NULL, b.CBW_Rev_Share__c); system.assertEquals(NULL, b.Total_Rev_Share__c); Release__c c = new Release__c (Name = '456 Main', Qualified_Addresses__c = 100, Rev_Share_Paid__c = FALSE); insert c; Penetration_Summary__c d = new Penetration_Summary__c (Name = '456', Current__c = FALSE, Copper_Voice_Rev__c = 1000, Copper_CBAD_Rev__c = 1000, DSL_Rev__c = 1000, Fiber_Voice_Rev__c = 1000, Fiber_CBAD_Rev__c = 1000, Fiber_Internet_Rev__c = 1000, Video_Rev__c = 1000, CBW_Rev__c = 1000, Copper_Voice_Subs__c = 10, Copper_CBAD_Subs__c = 5, DSL_Subs__c = 2, Fiber_Voice_Subs__c = 10, Fiber_CBAD_Subs__c = 5, Fiber_Internet_Subs__c = 20, Video_Subs__c = 25, CBW_Subs__c = 5, Month__c = '05 May', Year__c = '2013'); insert d; system.assertEquals(NULL, d.Copper_Voice_Rev_Share__c); system.assertEquals(NULL, d.DSL_Rev_Share__c); system.assertEquals(NULL, d.Fiber_Voice_Rev_Share__c); system.assertEquals(NULL, d.Fiber_Internet_Rev_Share__c); system.assertEquals(NULL, d.Video_Rev_Share__c); system.assertEquals(NULL, d.CBW_Rev_Share__c); system.assertEquals(NULL, d.Total_Rev_Share__c); Release__c e = new Release__c (Name = '789 Main', Qualified_Addresses__c = 100, Rev_Share_Paid__c = TRUE, Copper_Voice_Rev_Share__c = 'a1Qc0000000E7SG', DSL_Rev_Share__c = 'a1Pc0000000MIPh', Fiber_Voice_Rev_Share__c = 'a1Nc0000000EkxN', Fiber_Internet_Rev_Share__c = 'a1Mc0000000EYX1', Video_Rev_Share__c = 'a1Lc0000000DuYm', CBW_Rev_Share__c = 'a1Oc0000000DHsF'); insert e; Penetration_Summary__c f = new Penetration_Summary__c (Name = '789 Main', Current__c = FALSE, Copper_Voice_Rev__c = 1000, Copper_CBAD_Rev__c = 1000, DSL_Rev__c = 1000, Fiber_Voice_Rev__c = 1000, Fiber_CBAD_Rev__c = 1000, Fiber_Internet_Rev__c = 1000, Video_Rev__c = 1000, CBW_Rev__c = 1000, Copper_Voice_Subs__c = 10, Copper_CBAD_Subs__c = 5, DSL_Subs__c = 2, Fiber_Voice_Subs__c = 10, Fiber_CBAD_Subs__c = 5, Fiber_Internet_Subs__c = 20, Video_Subs__c = 25, CBW_Subs__c = 5, Month__c = '05 May', Year__c = '2013'); insert f; system.assertEquals(NULL, f.Copper_Voice_Rev_Share__c); system.assertEquals(NULL, f.DSL_Rev_Share__c); system.assertEquals(NULL, f.Fiber_Voice_Rev_Share__c); system.assertEquals(NULL, f.Fiber_Internet_Rev_Share__c); system.assertEquals(NULL, f.Video_Rev_Share__c); system.assertEquals(NULL, f.CBW_Rev_Share__c); system.assertEquals(NULL, f.Total_Rev_Share__c); Copper_Voice_Rev_Share__c cvrs = new Copper_Voice_Rev_Share__c (Tier_One__c = 3.25, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 100.00); insert cvrs; DSL_Rev_Share__c dslrs = new DSL_Rev_Share__c (Tier_One__c = 3.25, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 100.00); insert dslrs; Fiber_Voice_Rev_Share__c fvrs = new Fiber_Voice_Rev_Share__c (Tier_One__c = 3.25, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 100.00); insert fvrs; Fiber_Internet_Rev_Share__c firs = new Fiber_Internet_Rev_Share__c (Tier_One__c = 5.00, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 100.00); insert firs; Video_Rev_Share__c videors = new Video_Rev_Share__c (Tier_One__c = 7.00, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 49.99, Tier_Two__c = 15.00, Low_End_Tier_Two__c = 50.00, High_End_Tier_Two__c = 100.00 ); insert videors; CBW_Rev_Share__c cbwrs = new CBW_Rev_Share__c (Tier_One__c = 3.25, Low_End_Tier_One__c = 0.00, High_End_Tier_One__c = 100.00); insert cbwrs; Release__c g = new Release__c (Name = '012 Main St', Qualified_Addresses__c = 100, Rev_Share_Paid__c = TRUE, Copper_Voice_Rev_Share__c = cvrs.ID, DSL_Rev_Share__c = dslrs.ID, Fiber_Voice_Rev_Share__c = fvrs.ID, Fiber_Internet_Rev_Share__c = firs.ID, Video_Rev_Share__c = videors.ID, CBW_Rev_Share__c = cbwrs.ID); insert g; Penetration_Summary__c h = new Penetration_Summary__c (Name = '012 Main St', Current__c = TRUE, Copper_Voice_Rev__c = 1000, Copper_CBAD_Rev__c = 1000, DSL_Rev__c = 1000, Fiber_Voice_Rev__c = 1000, Fiber_CBAD_Rev__c = 1000, Fiber_Internet_Rev__c = 1000, Video_Rev__c = 1000, CBW_Rev__c = 1000, Copper_Voice_Subs__c = 10, Copper_CBAD_Subs__c = 5, DSL_Subs__c = 2, Fiber_Voice_Subs__c = 10, Fiber_CBAD_Subs__c = 5, Fiber_Internet_Subs__c = 20, Video_Subs__c = 75, CBW_Subs__c = 5, Month__c = '05 May', Year__c = '2013'); insert h; system.assertEquals(65.00, h.Copper_Voice_Rev_Share__c); system.assertEquals(32.50, h.DSL_Rev_Share__c); system.assertEquals(65.00, h.Fiber_Voice_Rev_Share__c); system.assertEquals(50.00, h.Fiber_Internet_Rev_Share__c); system.assertEquals(150.00, h.Video_Rev_Share__c); system.assertEquals(32.50, h.CBW_Rev_Share__c); } }
It is giving that error at Line 52 of the Apex Code, which shouldn't even be applicaple because in the test class the value of Copper_Voice_Penetration__c is 10%. Copper_Voice_Penetration__c is a formula field.
The Test class runs if I take out everything after the System.AssertEquals on insert F.
Any ideas?
Thanks,
Hampton
- Hampton
- May 10, 2013
- Like
- 0
Initial term of field expression must be a concrete SObject: MAP<String,Address__c>
Hello:
I am getting the above error at Line 15 of the below code.
Here is what I am looking for:
(1) Address__c has a field called Release__c
(2) There will be multiple Address__c records where Address__c.Release__c.Name = New_Door_Assignment__c.Name
(3) Assigned_Address__c is a child to both Address__c and New_Door_Assignment__c
(4) What I am trying to do is upon creation of a New_Door_Assignment__c record, to create the multiple child records (Assigned_Address__c) related to both New_Door_Assignment__c and Address__c
Here is what I have started and am getting the error above. Any help is moe than appreciated.
trigger AddressAssignment on New_Door_Assignment__c (after insert) { Set<String> releaseName = new Set<String>(); for (Release__c release : Trigger.new) { {releaseName.add(release.Name);} Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select Name, Unparsed_Address_Formula__c, Release__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(New_Door_Assignment__c newRelease : trigger.new) { if(addressMap.containsKey.(newRelease.Name)) newAssignment.add(new Assigned_Address__c( Date_Assigned__c = newRelease.Date_Released__c, New_Door_Assignment__c = newRelease.ID, Rep__c = newRelease.Reps__c)); } insert newAssignment; } } }
Thanks,
Hampton
- Hampton
- May 10, 2013
- Like
- 0
Apex Help
Hello:
I have two unrelated objects that have the same Name value that I am attempting to write a code for to update a field.
I am trying to update the CBW_Rev_Share__c value using a formula with a value from Site__c
Below is my code. I am not getting a value back in the CBW_Rev_Share__c field.
trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) { Set<String> penSumName = new Set<String>(); for (Penetration_Summary__c pen : Trigger.new) { if(pen.Current__c == TRUE) { penSumName.add(pen.Name); } } Set<String> releaseNameStrs = new Set<String>(); Map<String, Release__c> releaseSumMap = new Map<String, Release__c>(); for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN: penSumName ]) { releaseSumMap.put(release1.Name, release1); releaseNameStrs.add(release1.Name); } List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>(); for(Penetration_Summary__c c:[Select Id, Name, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseNameStrs ]) { if(releaseSumMap.containsKey(c.Name)) { if(releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c != null && c.CBW_Rev__c != null) c.CBW_Rev_Share__c = (releaseSumMap.get(c.Name).CBW_Rev_Share__r.Tier_One__c * c.CBW_Rev__c); else c.CBW_Rev_Share__c = 0; penForUpdate.add(c); } } }
Thanks,
Hampton
- Hampton
- May 09, 2013
- Like
- 0
Field Update on Unrelated Object
Hello:
I have to unrelated custom objects - Penetration_Summary__c and Release__c.
Penetration_Summary__c and Release__c will have the same name.
I need to update a field on Penetration_Summary__c using a formula that is:
(Penetration_Summary__c.CBW_Rev__C * Release__c.CBW_Rev_Share__r.Tier_One__c)
Below is the trigger I have written but am getting an error around the map. Any thoughts on how to proceed?
trigger RevShareUpdate on Penetration_Summary__c (before insert, before update) { Set<String> penSumName = new Set<String>(); for (Penetration_Summary__c pen : Trigger.new) { if(pen.Current__c = TRUE) { penSumName.add(pen.Name); } } Map<ID, Name> releaseSumMap = new Map<ID, Name>(); for(Release__c release1:[Select ID, Name, CBW_Rev_Share__c, CBW_Rev_Share__r.Tier_One__c from Release__c Where Name IN: penSumName ]) { releaseSumMap.put(release1.ID, release1.Name); } List<Penetration_Summary__c> penForUpdate = new List<Penetration_Summary__c>(); for(Penetration_Summary__c c:[Select Id, CBW_Rev__c, CBW_Rev_Share__c from Penetration_Summary__c where Name In : releaseSumMap.Name ]) { (c.CBW_Rev_Share__c) == ((releaseSumMap.get.(c.ID).CBW_Rev_Share__r.Tier_One__c)*c.CBW_Rev__c); } update penForUpdate; }
Thanks,
Hampton
- Hampton
- May 08, 2013
- Like
- 0
Code Error: Updating Field on Parent with Value from Child
Good Morning:
I am getting a code error here and not sure where I went wrong.
I have custom object SFU__c which is the parent to Penetration_Summary__c.
I want to take custom fields Non_Video_Subs__c and Video_Penetration__c from Penetration_Summary and update Non_Video_Addresses__c and Current_Penetration__c on SFU__C where Penetration_Summary__c.Current__c=TRUE
Here is the code I have:
trigger SFUMetrics on Penetration_Summary__c (after insert, after update) { Set<Id> PenetrationSummaryId = new Set<Id>(); for (Penetration_Summary__c penetration : Trigger.new) { if(penetration.Current__c = TRUE) { PenetrationSummaryId.add(penetration.Id); } } Set<Id> SFUId = new Set<Id>(); for(Penetration_Summary__c penetration1:[Select ID, SFU__c, Non_Video_Subs__c, Video_Penetration__c from Penetration_Summary__c Where Id IN:PenetrationSummaryId ]) { SFUId.add(penetration1); } List<SFU__c> sfuforupdate = new List<SFU__c>(); for(SFU__c c:[Select Id, Non_Video_Addresses__c , Current_Penetration__c from SFU__c where Id IN:SFU.Id ]) { set.(c.non_video_addresses__c) == penetration1.Non_Video_Subs__c; set.(c.current_penetration__c) == penetration1.Video_Penetration__c; } update sfuforupdate; }
I am getting Error: Compile Error: Variable does not exist: penetration1.Non_Video_Subs__c at line 22 column 39
Suggestions?
Thanks,
Hampton
- Hampton
- May 07, 2013
- Like
- 0
Help With Previously Working Apex Trigger
Good Morning:
I created the follwing Apex trigger back in June and it was working wonderfully (creating/deleting child records depending on some field updates from the parent record) and all of a sudden about two weeks ago, the trigger stopped creating the child records.
I opened the developer console and got the following error:
I have done some reading on other posts with the same error message and understand that it means nothing is being passed from the map over to the creation portion of the record. I guess I need to understand why and why it all of a sudden happened when it was working fine for four months.
Any help is greatly appreciated.
trigger RepSummaryCreation on Video_Turf__c (after update) { Map<ID, String> releaseName = new Map<ID, String>(); for (Video_Turf__c newDoor2: Trigger.new){ if(newDoor.Assigned__c == TRUE) releaseName.put(newDoor2.ID, newDoor.Name); } List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>(); for(Video_Turf__c turf1 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_One__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_One__c !='']){ newSummary.add(new Rep_Summary__c( Date__c = turf1.Date__c, Release__c = turf1.ID, Doors__c = turf1.Doors_Per_Rep__c, Name = turf1.Rep_One__c)); } insert newSummary; List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>(); for(Video_Turf__c turf2 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Two__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Two__c !='']){ newSummary2.add(new Rep_Summary__c( Date__c = turf2.Date__c, Release__c = turf2.ID, Doors__c = turf2.Doors_Per_Rep__c, Name = turf2.Rep_Two__c)); } insert newSummary2; List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>(); for(Video_Turf__c turf3 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Three__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Three__c !='']){ newSummary3.add(new Rep_Summary__c( Date__c = turf3.Date__c, Release__c = turf3.ID, Doors__c = turf3.Doors_Per_Rep__c, Name = turf3.Rep_Three__c)); } insert newSummary3; List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>(); for(Video_Turf__c turf4 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Four__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Four__c !='']){ newSummary4.add(new Rep_Summary__c( Date__c = turf4.Date__c, Release__c = turf4.ID, Doors__c = turf4.Doors_Per_Rep__c, Name = turf4.Rep_Four__c)); } insert newSummary4; List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>(); for(Video_Turf__c turf5 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Five__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Five__c !='']){ newSummary5.add(new Rep_Summary__c( Date__c = turf5.Date__c, Release__c = turf5.ID, Doors__c = turf5.Doors_Per_Rep__c, Name = turf5.Rep_Five__c)); } insert newSummary5; List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>(); for(Video_Turf__c turf6 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Six__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Six__c !='']){ newSummary6.add(new Rep_Summary__c( Date__c = turf6.Date__c, Release__c = turf6.ID, Doors__c = turf6.Doors_Per_Rep__c, Name = turf6.Rep_Six__c)); } insert newSummary6; List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>(); for(Video_Turf__c turf7 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Seven__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Seven__c !='']){ newSummary7.add(new Rep_Summary__c( Date__c = turf7.Date__c, Release__c = turf7.ID, Doors__c = turf7.Doors_Per_Rep__c, Name = turf7.Rep_Seven__c)); } insert newSummary7; List<Rep_Summary__c> newSummary8 = new List<Rep_Summary__c>(); for(Video_Turf__c turf8 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Eight__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Eight__c !='']){ newSummary8.add(new Rep_Summary__c( Date__c = turf8.Date__c, Release__c = turf8.ID, Doors__c = turf8.Doors_Per_Rep__c, Name = turf8.Rep_Eight__c)); } insert newSummary8; List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>(); for(Video_Turf__c turf9 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Nine__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Nine__c !='']){ newSummary9.add(new Rep_Summary__c( Date__c = turf9.Date__c, Release__c = turf9.ID, Doors__c = turf9.Doors_Per_Rep__c, Name = turf9.Rep_Nine__c)); } insert newSummary9; List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>(); for(Video_Turf__c turf10 : [Select ID, Name, Date__c, Doors_Per_Rep__c, Rep_Ten__c from Video_Turf__c where ID in : releaseName.keyset() and Rep_Ten__c !='']){ newSummary10.add(new Rep_Summary__c( Date__c = turf10.Date__c, Release__c = turf10.ID, Doors__c = turf10.Doors_Per_Rep__c, Name = turf10.Rep_Ten__c)); } insert newSummary10; }
Thanks,
Hampton
- Hampton
- October 21, 2013
- Like
- 0
Help With Creation of Child Record
Hello:
I am having issues with an Apex trigger designed to create a child records (Rep_Summary__c) after update of parent record (Video_Turf__c) where certain conditions are met.
Here is my trigger:
trigger RepSummaryCreation on Video_Turf__c (after update) { List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>(); for(Video_Turf__c a: trigger.new){ if(a.Assigned__c==TRUE && a.Rep_One__c!=NULL){ newSummary.add(new Rep_Summary__c( Date__c = a.Date__c, Release__c = a.ID, Doors__c = a.Doors_Per_Rep__c, Name = a.Rep_One__c)); } } insert newSummary; List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>(); for(Video_Turf__c b: trigger.new){ if(b.Assigned__c==TRUE && b.Rep_Two__c!=NULL){ newSummary2.add(new Rep_Summary__c( Date__c = b.Date__c, Release__c = b.ID, Doors__c = b.Doors_Per_Rep__c, Name = b.Rep_Two__c)); } } insert newSummary2; List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>(); for(Video_Turf__c c: trigger.new){ if(c.Assigned__c==TRUE && c.Rep_Three__c!=''){ newSummary3.add(new Rep_Summary__c( Date__c = c.Date__c, Release__c = c.ID, Doors__c = c.Doors_Per_Rep__c, Name = c.Rep_Three__c)); } } insert newSummary3; List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>(); for(Video_Turf__c d: trigger.new){ if(d.Assigned__c==TRUE && d.Rep_Four__c!=''){ newSummary4.add(new Rep_Summary__c( Date__c = d.Date__c, Release__c = d.ID, Doors__c = d.Doors_Per_Rep__c, Name = d.Rep_Four__c)); } } insert newSummary4; List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>(); for(Video_Turf__c e: trigger.new){ if(e.Assigned__c==TRUE && e.Rep_Five__c!=''){ newSummary5.add(new Rep_Summary__c( Date__c = e.Date__c, Release__c = e.ID, Doors__c = e.Doors_Per_Rep__c, Name = e.Rep_Five__c)); } } insert newSummary5; List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>(); for(Video_Turf__c f: trigger.new){ if(f.Assigned__c==TRUE && f.Rep_Six__c!=''){ newSummary6.add(new Rep_Summary__c( Date__c = f.Date__c, Release__c = f.ID, Doors__c = f.Doors_Per_Rep__c, Name = f.Rep_Six__c)); } } insert newSummary6; List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>(); for(Video_Turf__c g: trigger.new){ if(g.Assigned__c==TRUE && g.Rep_Seven__c!=''){ newSummary7.add(new Rep_Summary__c( Date__c = g.Date__c, Release__c = g.ID, Doors__c = g.Doors_Per_Rep__c, Name = g.Rep_Seven__c)); } } insert newSummary7; List<Rep_Summary__c> newSummary8 = new List<Rep_Summary__c>(); for(Video_Turf__c h: trigger.new){ if(h.Assigned__c==TRUE && h.Rep_One__c!=''){ newSummary8.add(new Rep_Summary__c( Date__c = h.Date__c, Release__c = h.ID, Doors__c = h.Doors_Per_Rep__c, Name = h.Rep_Eight__c)); } } insert newSummary8; List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>(); for(Video_Turf__c i: trigger.new){ if(i.Assigned__c==TRUE && i.Rep_Nine__c!=''){ newSummary9.add(new Rep_Summary__c( Date__c = i.Date__c, Release__c = i.ID, Doors__c = i.Doors_Per_Rep__c, Name = i.Rep_Nine__c)); } } insert newSummary9; List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>(); for(Video_Turf__c j: trigger.new){ if(j.Assigned__c==TRUE && j.Rep_Ten__c!=''){ newSummary10.add(new Rep_Summary__c( Date__c = j.Date__c, Release__c = j.ID, Doors__c = j.Doors_Per_Rep__c, Name = j.Rep_Ten__c)); } } insert newSummary10; }
This seems to be a fairly straight forward trigger. What am I missing here that is not firing to create the records?
Thanks,
Hampton
- Hampton
- October 16, 2013
- Like
- 0
Simple Trigger To Delete Records
Hello:
I am attempting to delete a group of child records if a parent record is updated. I cannot seem to get the trigger to fire. Any thoughts? Am I over-complicating this?
When a Turf__c record is updated, I want to delete all associated Rep_Summary__c records.
trigger RepAssignmentDelete on Video_Turf__c (after update) { Set<ID> turfDoors = new Set<ID>(); for(Video_Turf__c turfRep : trigger.old) (turfDoors.add(turfRep.ID)); Map<String, Video_Turf__c> turfMap = new Map <String, Video_Turf__c>(); for(Video_Turf__c turf1 : [Select ID, Name, Date__c, Doors_Per_Rep__c from Video_Turf__c where ID in : turfDoors]) turfMap.put(turf1.Name, turf1); List<Rep_Summary__c> repsToDelete = new List<Rep_Summary__c>(); for(Rep_Summary__c a :[Select Name, Date__c, Doors__c, Release__c from Rep_Summary__c where Release__c in : turfDoors]) if(turfMap.containskey(a.Release__c)){ repsToDelete.add(a); } delete repsToDelete; }
Thanks,
Hampton
- Hampton
- June 17, 2013
- Like
- 0
Apex Required Field Issue
Hello:
I have the following code in place:
trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) { Map<String, Id> releaseName = new Map<String, Id>(); for (Video_Turf__c newDoor: Trigger.new) { if (newDoor.New_Resweep__c == 'Resweep' && newDoor.RecordTypeID != '012c00000004PLM') releaseName.put(newDoor.Name, newDoor.Id); } List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName.keyset() and Active_Video__c = 'N']){ newAssignment.add(new Assigned_Address__c( Address__c = address1.Unparsed_Address__c, Date_Assigned__c = system.today(), Turf__c = releaseName.get(address1.Release__c), Address_ID__c = address1.ID)); } insert newAssignment; }
The code creates child-records (Assigned_Address__c) to parent (Video_Turf__c) when a Video_Turf__c record is created.
The problem I am having is that depending on what the Name value is in the parent (Video_Turf__c), I get an error.
For example, I created a Video_Turf__c record with Blanchetta and Galbraith as the name. The trigger fired perfectly, the parent saved, the children were created and all was good.
When I tried to create a Video_Turf__c record with McKelvey and Winton as the name, I get an APEX error:
trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) {
Review all error messages below to correct your data.
Apex trigger VideoResweepAddressAssignment caused an unexpected exception, contact your administrator: VideoResweepAddressAssignment: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 24; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Turf]: [Turf]: Trigger.VideoResweepAddressAssignment: line 18, column 1
- Hampton
- June 11, 2013
- Like
- 0
Need Help Scheduling Batch Process
Good Morning:
I have the following Apex Class which converts records froma temporary staging custom object over to new records in another custom object. I am having issues getting the class set up to schedule this run. Any help is appreciated.
The issue in the scheduleble class is in
Penetration_Summary__c convertBatch = new Penetration_Summary__c(query);
Hampton
global class PenetrationSummaryConversion implements Database.Batchable<sObject>{ public string Query; public Penetration_Summary__c penSum; public List<Penetration_Summary__c> insertPenSumList = new List<Penetration_Summary__c>(); List<Group> QueueList = new List <Group>(); Map<String,Id> QueueMap = new Map<String,Id>(); List<Penetration_Staging_Records__c> addStagingList = new List<Penetration_Staging_Records__c>(); List<Penetration_Staging_Records__c> deleteStagingList = new List<Penetration_Staging_Records__c>(); global Database.querylocator start(Database.BatchableContext BC) { Query = 'Select Name, Id, OwnerId, LastModifiedDate, Release__c, CBW_Rev__c, CBW_Subs__c, Copper_CBAD_Rev__c, Copper_CBAD_Subs__c,'+ 'Copper_Voice_Rev__c, Copper_Voice_Subs__c, DSL_Rev__c, DSL_Subs__c, Fiber_CBAD_Rev__c,'+ 'Fiber_CBAD_Subs__c, Fiber_Internet_Rev__c, Fiber_Internet_Subs__c, Fiber_Voice_Rev__c, Fiber_Voice_Subs__c,'+ 'Premium_Speed_Rev__c, Premium_Speed_Subs__c, Units__c, Video_Subs__c, Video_Rev__c,'+ ' From Penetration_Staging_Records__c' + ' Where Batch_Processed__c=false'; return Database.getQueryLocator(Query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { QueueList=[Select Id, Name, DeveloperName, Type from Group where Type = 'Queue']; for(Group qObj:QueueList){ QueueMap.put( qobj.Name, qobj.Id); } for(sObject s: scope) { Penetration_Staging_Records__c tempPenRecords = (Penetration_Staging_Records__c)s; if(tempPenRecords.LastModifiedDate < System.now() || Test.isRunningTest()) addStagingList.add(tempPenRecords); } for(Penetration_Staging_Records__c tempRecord : addStagingList){ penSum = new Penetration_Summary__c(); penSum.Copper_Voice_Subs__c = tempRecord.Copper_Voice_Subs__c; penSum.Copper_CBAD_Subs__c = tempRecord.Copper_CBAD_Subs__c; penSum.DSL_Subs__c = tempRecord.DSL_Subs__c; penSum.Premium_Speed_Subs__c = tempRecord.Premium_Speed_Subs__c; penSum.Fiber_Voice_Subs__c = tempRecord.Fiber_Voice_Subs__c; penSum.Fiber_CBAD_Subs__c = tempRecord.Fiber_CBAD_Subs__c; penSum.Fiber_Internet_Subs__c = tempRecord.Fiber_Internet_Subs__c; penSum.Video_Subs__c = tempRecord.Video_Subs__c; penSum.CBW_Subs__c = tempRecord.CBW_Subs__c; penSum.Copper_Voice_Rev__c = tempRecord.Copper_Voice_Rev__c; penSum.Copper_CBAD_Rev__c = tempRecord.Copper_CBAD_Rev__c; penSum.DSL_Rev__c = tempRecord.DSL_Rev__c; penSum.Premium_Speed_Rev__c = tempRecord.Premium_Speed_Rev__c; penSum.Fiber_Voice_Rev__c = tempRecord.Fiber_Voice_Rev__c; penSum.Fiber_CBAD_Rev__c = tempRecord.Fiber_CBAD_Rev__c; penSum.Fiber_Internet_Rev__c = tempRecord.Fiber_Internet_Rev__c; penSum.Video_Rev__c = tempRecord.Video_Rev__c; penSum.CBW_Rev__c = tempRecord.CBW_Rev__c; penSum.Units__c = tempRecord.Units__c; penSum.Name = tempRecord.Release__c; insertPenSumList .add(penSum); deleteStagingList.add(tempRecord); } insert insertPenSumList; delete deleteStagingList; } global void finish(Database.BatchableContext BC) { } }
global class PenetrationSummaryConversionScheduled implements Schedulable{ global void execute(SchedulableContext sc){ String query = 'Select Name, Id, OwnerId, LastModifiedDate, Release__c, CBW_Rev__c, CBW_Subs__c, Copper_CBAD_Rev__c, Copper_CBAD_Subs__c,'+ 'Copper_Voice_Rev__c, Copper_Voice_Subs__c, DSL_Rev__c, DSL_Subs__c, Fiber_CBAD_Rev__c,'+ 'Fiber_CBAD_Subs__c, Fiber_Internet_Rev__c, Fiber_Internet_Subs__c, Fiber_Voice_Rev__c, Fiber_Voice_Subs__c,'+ 'Premium_Speed_Rev__c, Premium_Speed_Subs__c, Units__c, Video_Subs__c, Video_Rev__c,'+ ' From Penetration_Staging_Records__c' + ' Where Batch_Processed__c=false'; Penetration_Summary__c convertBatch = new Penetration_Summary__c(query); Id BatchProcessId = Database.ExecuteBatch(convertBatch); } }
- Hampton
- May 26, 2013
- Like
- 0
System.LimitException: Too many SOQL queries: 101 Question
Good Evening All:
I have the code below that is designed to create child records (Assigned_Address__c) on Turf__c when the Turf__c record is created. I am getting the dreaded System.LimitException: Too many SOQL queries: 101error message when I attempt to create a Turf__c record. I thought I had written this to properly handle the creation of child records. Depending on the conditions, the number of child records created at one time would be between 10 and 500. Do I need to explore another option than this?
As always, your help is much appreciated.
Thanks!
Hampton
trigger NewAddressAssignment on Turf__c (after insert) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Turf__c> turfList = [Select ID, Name, New_Resweep__c from Turf__c where Name in : releaseName and New_Resweep__c = 'New']; List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : turfList) { Assigned_Address__c a = new Assigned_Address__c(); a.Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c; a.Turf__c = newRelease.ID; a.Address_ID__c = addressmap.get(newRelease.Name).ID; newAssignment.add(a); } insert newAssignment; } } }
- Hampton
- May 20, 2013
- Like
- 0
Need Help with Too Many SOQL Queries
Hello:
Please see my code below. What I am doing is creating child records (Assigned_Address__c) on custom object Turf__c when Turf__c is created. The number of child records can be in the multiple hundreds at times. How can I tweak this Apex to avoid hitting the SOQL Limit (which is currently happening)?
trigger NewAddressAssignment on Turf__c (after insert) { Set<String> releaseName = new Set<String>(); for (Turf__c newDoor: Trigger.new) { releaseName.add(newDoor.Name); Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c, Video_Qualification__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(Turf__c newRelease : trigger.new) { if(addressMap.containsKey(newRelease.Name)){ if(newRelease.New_Resweep__c == 'New'){ newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, Turf__c = newRelease.ID, Address_ID__c = addressMap.get(newRelease.Name).ID)); } } insert newAssignment; } } } }
Thanks!
Hampton
- Hampton
- May 19, 2013
- Like
- 0
Help With Integer in Apex
Hello:
I have the following code which is designed to insert multiple child recods (Assigned_Address__c) when a parent is created (New_Door_Assigned__c).
What I need to do is after the child records are inserted, update a field on the child record with a value from the parent record, dependent upon what the index/integer value of the row is.
For example, if there are 100 child records, I would want the first 50 to have a Rep__c value = New_Door_Assignment__c.Sales_Rep_One__c, the second 50 to New_Door_Assignment__c.Sales_Rep_Two__c, etc until all child records have a Rep_Name__c value.
The code to create the child records works and I have written test class for it. I am now attempting to add in the logic around this starting with
List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment);
assigned.sort(Address__c);
Attached is my full code. Any guidance is appreciated.
Thanks,
Hampton
trigger AddressAssignment on New_Door_Assignment__c (before update) { Set<String> releaseName = new Set<String>(); for (New_Door_Assignment__c newDoor: Trigger.new) { if(newDoor.Assigned__c = TRUE){ {releaseName.add(newDoor.Name);} } Map<String, Address__c> addressMap = new Map<String, Address__c>(); for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName]){ addressMap.put(address1.Release__c, address1); List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>(); for(New_Door_Assignment__c newRelease : trigger.new) { if(addressMap.containsKey(newRelease.Name)) newAssignment.add(new Assigned_Address__c( Address__c = addressMap.get(newRelease.Name).Unparsed_Address__c, New_Door_Assignment__c = newRelease.ID)); Date_Assigned__c = newRelease.Date_Released__c, } List<Assigned_Address__c> assigned = new List<Assigned_Address__c>(newAssignment); assigned.sort(Address__c); List<Integer> int = new List<Integer>(assigned); if(integer i < newRelease.Doors_Per_Rep__c){ assigned.Rep__c = newRelease.Sales_Rep_One__c else { if(integer i < (2*newRelease.Doors_Per_Rep__c)){ assigned.Rep__c = newRelease.Sales_Rep_Two__c else { assigned.Rep__c = newRelease.Sales_Rep_Three__c; } } } update newAssignment; insert newAssignment; } } }
- Hampton
- May 13, 2013
- Like
- 0