2

After creating the Sobject below, I'm trying to create 3 sets, 1 for YearNumber, Scheduled_Days and MonthNumber to use in a query. I need these 3 sets to be of Integer/String type so I must dereference the SObject field first. I would appreciate help in writing this as it seems as if it should be easy but I've not been doing it right and getting syntax errors.

List schconnList = [select id, Address_Type__c, Address_Type_r.Provider_c, Address_Type_r.Address_c,Schedule_Development__r.Scheduled_Days__c, Schedule_Development_r.YearNumber_c, Schedule_Development_r.MonthNumber_c, Schedule_Development__r.Start_Time__c, Schedule_Development__r.End_Time__c from Schedule_Connection__c where Id in:schconnections];

Set MonthNumber
Set Scheduled_Days
Set YearNumber

Note: I'm creating 3 Sets to later do a query against another Custom Setting using these values. Currently it's querying the entire recordset of this object = BAD so I'm trying to limit this query by using these Sets.

Can I use List to add all items to set in SOQL without loop similar to:
Set yearList = New Set ( [select Schedule_Development_r.YearNumber_c from Schedule_Connection__c where Id in:schconnList].Schedule_Development_r.YearNumber_c.intvalue());

2

assuming that the Schedule_Development_r.MonthNumber_c and Schedule_Development_r.YearNumber_c fields are of type number, the following should do the trick

Set<String> scheduled_Days = new Set<String>();
Set<Integer> monthNumber = new Set<Integer>();
Set<Integer> yearNumber = new Set<Integer>();

List<Schedule_Connection__c> schconnList = [select id, Address_Type__c, Address_Type_r.Provider_c, Address_Type_r.Address_c,Schedule_Development__r.Scheduled_Days__c, Schedule_Development_r.YearNumber_c, Schedule_Development_r.MonthNumber_c, Schedule_Development__r.Start_Time__c, Schedule_Development__r.End_Time__c from Schedule_Connection__c where Id in:schconnections];

for (Schedule_Connection__c record:schconnList){

  scheduled_Days.add(record.Schedule_Development__r.Scheduled_Days__c);

  if (record.Schedule_Development__r.MonthNumber_c != null){
    monthNumber.add(record.Schedule_Development__r.MonthNumber_c.intValue());
  }

  if (record.Schedule_Development__r.YearNumber_c != null){
    yearNumber.add(record.Schedule_Development__r.YearNumber_c.intValue());
  }

}
  • Good and bad is that this was my initial solution so now problem may lie somewhere else. I thought the for loop was redundant and the problem. Edited initial post to include the type of solution I thought may help, though given your answer my problem is obviously somewhere else anyway and not the loop. Is using the list to add directly to Set as I've now added a more efficient method, though getting syntax errors with what I've posted anyway. – user2451 Jun 26 '13 at 15:20
  • you can instantiate a set directly from a soql query. in addition your needs to be of a specific type, have a look here salesforce.com/us/developer/docs/apexcode/… – Seb Wagner Jun 26 '13 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.