• Sergey Bushyn
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Error:
Error Occurred: This error occurred when the flow tried to create records: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: PlacementTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PlacementTrigger: execution of AfterUpdate caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times) Trigger.PlacementTrigger: line 43, column 1: [] Trigger.PlacementTrigger: line 33, column 1. You can look up ExceptionCode values in the SOAP API Developer Guide.

APEX:
1    trigger PlacementTrigger on Placement__c (after insert, after update) {
2        
3        List<Placement_Schedule__c> schedules = new List<Placement_Schedule__c>();
4        Date start = date.today();
5        Integer scheduleLength = 0;
6        Decimal value = 0;
7        Decimal units = 0;
8        
9        if(Trigger.isInsert){
10            for(Placement__c P : Trigger.New){
11                if(P.IO__r.Opportunity__r.StageName != 'Closed Won'){
12                    Start = P.Start_Date__c;
13                scheduleLength = Start.daysBetween(P.End_Date__c)+1;
14                    System.debug('Placement schedule is ' + scheduleLength +' days long.');
15                value = P.Total_Amount__c;
16                units = P.Quantity__c;
17                
18                // create new revenue schedules for date range specified 
19                for(integer i = 0; i<scheduleLength; i++){
20                   Placement_Schedule__c PS = new Placement_Schedule__c(
21                       Revenue__c = value/scheduleLength,
22                       Forecast_Amount__c = value/scheduleLength,
23                       Quantity__c = units/scheduleLength,
24                       Placement__c = P.Id,
25                       Date__c = P.Start_Date__c + i,
26                       Name__c = null
27                   );
28                    schedules.add(PS);  
29                } // end loop to build Revenue Schedules
30              }// end check for closed won
31            } // end for loop over Placement__c
32            
33            insert schedules;
34            
35        } // end if insert check
36       
37        if(Trigger.isUpdate){
38            // Boolean to check if any of the Revenue Schedules have been actualized. If TRUE then code will later be blocked
39            Boolean actualized = FALSE;
40            
41            // Set to hold IDs of all placements with actualized revenue schedules
42            Set<Placement__c> actualizedPlacements = new Set<Placement__c>();
43        List<Placement__c> actualizedPlacementList = new List<Placement__c>([
44                        SELECT Id FROM Placement__c WHERE Id IN (
45                                        SELECT Placement__c FROM Placement_Schedule__c
46                                        WHERE Actualized__c = TRUE)]);
47           
48            for(Placement__c placement : actualizedPlacementList){
49                actualizedPlacements.add(placement);
50            }                                                         
51            // list for what the new schedule should be to check if an insert is necessary
52            List<Placement_Schedule__c> newSchedules = new List<Placement_Schedule__c>();
53            // list for revenue schedules that need to be deleted
54            List<Placement_Schedule__c> forDelete = new List<Placement_Schedule__c>();  
55            
56            // loop through Placements to look for valid edits
57            for(Placement__c PN : Trigger.New){
58               actualized = actualizedPlacementList.contains(PN);
59               Placement__c PO = Trigger.oldmap.get(PN.Id); 
60              // check if there is a delay and that the delay is not prior to the original start date
61               if(PN.Delayed_Start_Date__c != null && PN.Delayed_Start_Date__c > PN.Start_Date__c){
62                   Start = PN.Delayed_Start_Date__c;
63               } else {
64                   Start = PN.Start_Date__c;
65               }
66                
67               Integer originalLength = 0;
68               originalLength = PN.Start_Date__c.daysBetween(PN.End_Date__c)+1;
69               scheduleLength = Start.daysBetween(PN.End_Date__c)+1;
70                    System.debug('Placement schedule is ' + scheduleLength +' days long.');
71               value = PN.Total_Amount__c;
72               units = PN.Quantity__c;
73                
74            // checks if changes need to be made to the Revenue Schedules   
75                if(actualized == FALSE && PN.IO__r.Opportunity__r.StageName != 'Closed Won' &&
76                   (PN.Start_Date__c != PO.Start_Date__c || 
77                   PN.Delayed_Start_Date__c != PO.Delayed_Start_Date__c ||
78                   PN.Total_Amount__c != PO.Total_Amount__c ||
79                   PN.End_Date__c != PO.End_Date__c               )){
80    
81            // build list of new proper schedule in order to ensure there are no edits required
82                 for(integer i = 0; i<originalLength; i++){
83                   Placement_Schedule__c PSNew = new Placement_Schedule__c(
84                       Revenue__c = value/originalLength,
85                       Forecast_Amount__c = value/scheduleLength,
86                       Quantity__c = units/scheduleLength,
87                       Placement__c = PN.Id,
88                       Date__c = PN.Start_Date__c + i,
89                       Name__c = null
90                   );
91                    newSchedules.add(PSNew);  
92                 } // end loop to build proper schedule
93                            
94                       
95              // get schedules already generated
96              List<Placement_Schedule__c> oldSchedules = new List<Placement_Schedule__c>(
97              [SELECT Id, Date__c, Revenue__c, Forecast_Amount__c FROM Placement_Schedule__c
98              WHERE Placement__c =: PN.Id]
99              );        
100                       
101                for(Placement_Schedule__c OS : oldSchedules){
102                  
103                    // check for deletes
104                    if(OS.Date__c < PN.Start_Date__c || OS.Date__c > PN.End_Date__c){
105                       forDelete.add(OS); 
106                    }
107                    // check for valid delays
108                    if(OS.Date__c < Start){
109                       OS.Forecast_Amount__c = 0;
110                       OS.Revenue__c = value/originalLength;
111                       OS.Quantity__c = 0;
112                       schedules.add(OS);
113                     } else {
114                       OS.Forecast_Amount__c = value/scheduleLength;
115                       OS.Revenue__c = value/originalLength;
116                       OS.Quantity__c = units/scheduleLength;
117                       schedules.add(OS);
118                     }// check for if revenue schedule is no longer in placement date range
119                   
120                 
121                   } // end loop over schedules  
122                       
123                // check for inserts needed
124                Set<Placement_Schedule__c> alreadyExist = new Set<Placement_Schedule__c>();
125                       for(Placement_Schedule__c RS : newSchedules){
126                           for(Placement_Schedule__c RSOld : oldSchedules){
127                               if(RS.Date__c == RSOld.Date__c){
128                                   alreadyExist.add(RS);
129                                   System.debug('Revenue Schedule already exists with date '+RSOld.Date__c);
130                               } // end if check
131                           } // end loop over old schedules
132                           if(!alreadyExist.contains(RS)){
133                           schedules.add(RS);     
134                           }
135                       } // end loop over new schedules
136                       
137                } // end if valid update check       
138            } // end loop over Placement__c 
139            
140          upsert schedules;
141            
142            try{ 
143            delete forDelete;
144            System.debug('Deleting '+ forDelete.size() +' Revenue Schedules.');
145            } catch (Exception e) {
146            System.debug('Unable to delete Revenue Schedules: '+e);
147            }
148            
149        } // end of update check 
150        
151    } // end class