You need to sign in to do that
Don't have an account?
Need help with apex map
I change my code because my SOQL limit was always going over and this map/list seemed to be the only way to make it work. Now I'm still getting a error : Compile Error: unexpected token: 'List' at line 50 column 12 (the problem marked red).
global class ServiceAvailabililtyUpdaterForCase implements Schedulable,
Database.Stateful/*,
Database.Batchable<sObject>*/ {
// Implement Schedulable interface function for entry function to schedule this batch
global void execute(SchedulableContext sc){
ServiceAvailabililtyUpdaterForCase updater = new ServiceAvailabililtyUpdaterForCase();
Date currentDate = Date.newinstance(DateTime.Now().Year(), DateTime.Now().Month(), DateTime.Now().Day());
updater.HandleServiceAvailabilityChange(currentDate);
}
/*
* Execute method of Batchable interface
*/
global void execute( Database.BatchableContext BC, List<sObject> records ) {
System.debug('ServiceAvailabililtyUpdaterForCase.execute batch');
}
public void HandleServiceAvailabilityChange(Date currentDate) {
/*
1) Fetch not closed Support Cases
2) Fetch valid Service Availability objects per Case
3) Sort SAs by a) Case b) Name
4) Update last SA with duration until end of the month
5) Create new SA from the beginning of the new month (with same Fault Classification)
*/
RecordType recordType = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
System.Debug('SCHEDULED: SUPPORT CASE REC TYPE ID: ' + recordType.Id);
List<Case> suppportCases =[select Id, Fault_Classification__c, Setup_Name__c, CaseNumber, Related_Setup__c,Status_Explanation__c
from Case
where RecordTypeId =: recordType.Id
and IsClosed =: false
order by CreatedDate desc];
//Create new list which contains supportcases records
List<Id> idList = new List<Id>();
for (Case c : suppportCases) {
idList.add(c.Id);
System.Debug('SCHEDULED: CASE ' + c.CaseNumber + ' HANDLING STARTED');
}
//Creating list which contains service availability + setup records
List <Service_Availability__c> c = [SELECT setup__r.id,setup__r.name, setup__r.service_availability__c,setup__r.contract__c,setup__r.cost_center__c,ID, Name, Status__c, Duration__c, Start_DateTime__c,case__c FROM Service_Availability__c WHERE Case__c IN : idList];
//Create map which has list of sa-records
Map<Id, List<service_availability__c>> service_availability = new Map<Id, List<service_availability__c>>
List value = m.get(Id);
for(service_availability__c d: c) {
if service_availability.containsKey(d.case__c){
service_availability.get(d.case__c).add(d);
} else {
List<service_availability__c> s = new List<service_availability__c>();
s.add(d);
service_availability.put(d.case__c, s);
}
}
RecordType recordTypeIncident = [select Id, Name from RecordType where SObjectType = 'Service_Availability__c' and Name = 'Incident' LIMIT 1];
for (Id currentcase:service_availability.keySet()) {
for (service_availability__c sa:service_availability.get(currentcase)) {
Id hoursToUse = saHelper.GetHoursToUse(c, relatedSetup);
if(hoursToUse != null && relatedSetup != null) {
System.debug('SCHEDULED: DATE IN LAST SA: ' + oldSAsPerCase[0].Start_DateTime__c);
System.debug('SCHEDULED: CURRENT DATE: ' + DateTime.Now());
// Check if month has changed, year not
if (oldSAsPerCase[0].Start_DateTime__c.Month() < DateTime.Now().Month() && oldSAsPerCase[0].Start_DateTime__c.Year() == DateTime.Now().Year()) {
System.debug('SCHEDULED: MONTH HAS CHANGED from: ' + oldSAsPerCase[0].Start_DateTime__c.Month() + ' to: ' + DateTime.Now().Month());
// Update last SA with duration until end of the month
oldSAsPerCase[0].Duration__c = saHelper.CalculateDurationUntilEndOfMonth(hoursToUse, oldSAsPerCase[0].Start_DateTime__c, currentDate);
// Start of current month
// TODO: THIS CAUSES TEST CASE TO FAIL, SINCE LOCAL TIME IS USED THERE
DateTime startOfCurrentMonth = DateTime.newinstance(currentDate.Year(), currentDate.Month(), 1, 0, 0, 0);
System.debug('SCHEDULED: START OF CURRENT MONTH: ' + startOfCurrentMonth);
saHelper.InsertServiceAvailabilityObjectToCase(c, startOfCurrentMonth, recordTypeIncident, true);
update oldSAsPerCase[0];
// Update corresponding Setup SA objects
if (!alreadyHandled.Contains(relatedSetup)) {
saHelper.UpdateSetupServiceAvailability(c, relatedSetup, true);
alreadyHandled.Add(relatedSetup);
}
// Year changed, so did month
} else if (DateTime.Now().Year() > oldSAsPerCase[0].Start_DateTime__c.Year()) {
System.debug('SCHEDULED: YEAR HAS CHANGED from: ' + oldSAsPerCase[0].Start_DateTime__c.Year() + ' to: ' + DateTime.Now().Year());
System.debug('SCHEDULED: MONTH HAS CHANGED from: ' + oldSAsPerCase[0].Start_DateTime__c.Month() + ' to: ' + DateTime.Now().Month());
oldSAsPerCase[0].Duration__c = saHelper.CalculateDurationUntilEndOfYear(hoursToUse, oldSAsPerCase[0].Start_DateTime__c, currentDate);
// Start of current month
DateTime startOfCurrentMonth = DateTime.newinstance(currentDate.Year(), currentDate.Month(), 1, 0, 0, 0);
System.debug('SCHEDULED: START OF CURRENT MONTH: ' + startOfCurrentMonth);
saHelper.InsertServiceAvailabilityObjectToCase(c, startOfCurrentMonth, recordTypeIncident, true);
update oldSAsPerCase[0];
// Update corresponding Setup SA objects
if (!alreadyHandled.Contains(relatedSetup)) {
saHelper.UpdateSetupServiceAvailability(c, relatedSetup, true);
alreadyHandled.Add(relatedSetup);
}
} else {
System.debug('SCHEDULED: MONTH not CHANGED');
System.debug(' BUSINESS HOURS NO FOUND');
}
}
}
}
}
What exactly are you trying to achieve with this code?
This would help us get an overview of the complete code.
Regards,
Satish Kumar
All Answers
Map<Id, List<service_availability__c>> service_availability = new Map<Id, List<service_availability__c>>();
List<service_availability__c> value = m.get(Id);
instead of the two lines there
Regards,
Satish Kumar
I tried that but then I'll get Compile Error: expecting a left parentheses, found 'service_availability.containsKey' at line 52 column 23
if service_availability.containsKey(d.case__c){
I see that thereare no parenthesesfor the if statement.
It should be:
if (service_availability.containsKey(d.case__c)){
Now it's giving this error : Compile Error: Variable does not exist: Id at line 50 column 54
List<service_availability__c> value = m.get(Id); it should be able to use Cases Id.
Regards,
Satish Kumar
Yeah I noticed, it should have been c to relate to List above where are fetched the sa-records, but even thought I change it it says the same compile error that the Id does not exist.
So that means you have tried this code.
List<service_availability__c> value = c.get(Id);
Here there is no variable called ID in scope. Also because 'c' is a list, the c.get() method takes an integer as an argument. It returns the Service_Availability object stored at the index you specify as the argument.
Regards,
Satish Kumar
I put the code this List<service_availability__c> value = c.get(); and then I got this error : Compile Error: Invalid bind expression type of Schema.SObjectField for column of type Id at line 36 column 51
List<Case> suppportCases =[select Id, Fault_Classification__c, Setup_Name__c, CaseNumber, Related_Setup__c,Status_Explanation__c
from Case
where RecordTypeId =: recordType.Id
and IsClosed =: false
order by CreatedDate desc];
RecordType recordTypeIncident = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
System.Debug('SCHEDULED: SUPPORT CASE REC TYPE ID: ' + recordType.Id);
What exactly are you trying to achieve with this code?
This would help us get an overview of the complete code.
Regards,
Satish Kumar
Sorry, I accidentally pushed accept as solution when I should have pushed reply. :D So the main idea with this code is to gather all supportcases, setups and service availability records which concerns both setup and case. (Service availability record is the connective object between setup and cases). After we have gathered the information about these, the code should understand when a month is changed it updates last SA record on setup and the sa records contains businesshour minutes from the whole month. After this it adds new Sa object to Case when the month has changed , then it updates the setups Sa-object (which is attached to Case) . If the setup doesn't have a SA-object it adds it to it. Does this make it easier to understand?
Here is the whole code
global class ServiceAvailabililtyUpdaterForCase implements Schedulable,
Database.Stateful/*,
Database.Batchable<sObject>*/ {
// Implement Schedulable interface function for entry function to schedule this batch
global void execute(SchedulableContext sc){
ServiceAvailabililtyUpdaterForCase updater = new ServiceAvailabililtyUpdaterForCase();
Date currentDate = Date.newinstance(DateTime.Now().Year(), DateTime.Now().Month(), DateTime.Now().Day());
updater.HandleServiceAvailabilityChange(currentDate);
}
/*
* Execute method of Batchable interface
*/
global void execute( Database.BatchableContext BC, List<sObject> records ) {
System.debug('ServiceAvailabililtyUpdaterForCase.execute batch');
}
public void HandleServiceAvailabilityChange(Date currentDate) {
/*
1) Fetch not closed Support Cases
2) Fetch valid Service Availability objects per Case
3) Sort SAs by a) Case b) Name
4) Update last SA with duration until end of the month
5) Create new SA from the beginning of the new month (with same Fault Classification)
*/
List<Case> suppportCases =[select Id, Fault_Classification__c, Setup_Name__c, CaseNumber, Related_Setup__c,Status_Explanation__c
from Case
where RecordTypeId =: recordType.Id
and IsClosed =: false
order by CreatedDate desc];
RecordType recordTypeIncident = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
System.Debug('SCHEDULED: SUPPORT CASE REC TYPE ID: ' + recordType.Id);
//Create new list which contains supportcases records
List<Id> idList = new List<Id>();
for (Case c : suppportCases) {
idList.add(c.Id);
System.Debug('SCHEDULED: CASE ' + c.CaseNumber + ' HANDLING STARTED');
}
//Creating list which contains service availability + setup records
List <Service_Availability__c> c = [SELECT setup__r.id,setup__r.name, setup__r.service_availability__c,setup__r.contract__c,setup__r.cost_center__c,ID, Name, Status__c, Duration__c, Start_DateTime__c,case__c FROM Service_Availability__c WHERE Case__c IN : idList];
//Create map which has list of sa-records
Map<Id,List<service_availability__c>> service_availability = new Map<Id,List<service_availability__c>>();
List<service_availability__c> value = c.get();
for(service_availability__c d: c) {
if (service_availability.containsKey(d.case__c)){
service_availability.get(d.case__c).add(d);
} else {
List<service_availability__c> s = new List<service_availability__c>();
s.add(d);
service_availability.put(d.case__c, s);
}
}
for (Id currentcase:service_availability.keySet()) {
for (service_availability__c sa:service_availability.get(currentcase)) {
Id hoursToUse = saHelper.GetHoursToUse(c);
if(hoursToUse != null && c != null) {
System.debug('SCHEDULED: DATE IN LAST SA: ' + c[0].Start_DateTime__c);
System.debug('SCHEDULED: CURRENT DATE: ' + DateTime.Now());
// Check if month has changed, year not
if (c[0].Start_DateTime__c.Month() < DateTime.Now().Month() && c[0].Start_DateTime__c.Year() == DateTime.Now().Year()) {
System.debug('SCHEDULED: MONTH HAS CHANGED from: ' + c[0].Start_DateTime__c.Month() + ' to: ' + DateTime.Now().Month());
// Update last SA with duration until end of the month
c[0].Duration__c = saHelper.CalculateDurationUntilEndOfMonth(hoursToUse, c[0].Start_DateTime__c, currentDate);
// Start of current month
// TODO: THIS CAUSES TEST CASE TO FAIL, SINCE LOCAL TIME IS USED THERE
DateTime startOfCurrentMonth = DateTime.newinstance(currentDate.Year(), currentDate.Month(), 1, 0, 0, 0);
System.debug('SCHEDULED: START OF CURRENT MONTH: ' + startOfCurrentMonth);
saHelper.InsertServiceAvailabilityObjectToCase(c, startOfCurrentMonth, recordTypeIncident, true);
update c[0];
// Update corresponding Setup SA objects
if (!alreadyHandled.Contains(c)) {
saHelper.UpdateSetupServiceAvailability(c, true);
alreadyHandled.Add(c);
}
// Year changed, so did month
} else if (DateTime.Now().Year() > c[0].Start_DateTime__c.Year()) {
System.debug('SCHEDULED: YEAR HAS CHANGED from: ' + c[0].Start_DateTime__c.Year() + ' to: ' + DateTime.Now().Year());
System.debug('SCHEDULED: MONTH HAS CHANGED from: ' + c[0].Start_DateTime__c.Month() + ' to: ' + DateTime.Now().Month());
c[0].Duration__c = saHelper.CalculateDurationUntilEndOfYear(hoursToUse, c[0].Start_DateTime__c, currentDate);
// Start of current month
DateTime startOfCurrentMonth = DateTime.newinstance(currentDate.Year(), currentDate.Month(), 1, 0, 0, 0);
System.debug('SCHEDULED: START OF CURRENT MONTH: ' + startOfCurrentMonth);
saHelper.InsertServiceAvailabilityObjectToCase(c, startOfCurrentMonth, recordTypeIncident, true);
update c[0];
// Update corresponding Setup SA objects
if (!alreadyHandled.Contains(c)) {
saHelper.UpdateSetupServiceAvailability(c, true);
alreadyHandled.Add(c);
}
} else {
System.debug('SCHEDULED: MONTH not CHANGED');
System.debug(' BUSINESS HOURS NO FOUND');
}
}
}
}
}
}