function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Terry411Terry411 

Invalid initial type LIST<Student_Lookup__c> for MAP<String,id>

What I'm attempting to do here is replicate data from the Contact, Account and Contract objects into a custom object called Student_Lookup__cThe application is for a franchise that has multiple schools and they have security on the accounts that prevent one franchise from seeing another frachises students however, a student can attend class at another franchises location when travelingThis requires the franchises to be able to see limited information about the student to know if they have an active contract.

 

I thought what I'd do is run a nightly job that updates the Student_Lookup__c with the relevant information from the Contact, Account, and Contract objectsBasically duplicating the dataI'm not convinced I'm doing this the easiest way so I'm open to ideasHowever this is what I have so far but I'm receiving the above error on my Map statement.

 

 

public with sharing class clsRefreshStudentLookup {
	public void Refresh() {
		// Create Variable to update all the updates
		List<Student_Lookup__c> SL = New List<Student_Lookup__c>();
		
		// Set Today's Date
		Date dExpDate = date.today();
		
		// Gather Student_Lookup__c ID's to add to the data set below
		Map<String, ID> SList = new Map<String, ID>([Select ContractId__c, ID from Student_Lookup__c where ContractId__c <> '']);		
		
		// Query for Student with active Contracts 
		for (Contract[] Stud : [Select c.AccountId, c.Id, c.OwnerId, c.Program__r.Name, c.Program__r.School__c, 
			c.StartDate, c.EndDate, c.Student__c, c.Student__r.Student_ID__c, c.Student__r.FirstName, c.Student__r.LastName, 
			c.Student__r.Birthdate, c.Student__r.EC_Name__c, c.Student__r.EC_Other_Phone__c, c.Student__r.EC_Phone__c, 
			c.Student__r.EC_Relationship__c from Contract c where c.EndDate >= :dExpDate]) {
		
			for (Contract c : Stud ) {
				ID SLid = SList.get(c.id);
				
				// Update Student_Lookup__c
				Student_Lookup__c SLUpdate = New Student_Lookup__c (
					ID = SLid,
					ContractId__c=c.Id, 
					OwnerId=c.OwnerId,
					Program_Name__c=c.Program__r.Name, 
					Contract_StartDate__c=c.StartDate,
					Contract_EndDate__c=c.EndDate,
					Student_ContactID__c=c.Student__c,
					Student_ID__c=c.Student__r.Student_ID__c, 
					First_Name__c=c.Student__r.FirstName, 
					Last_Name__c=c.Student__r.LastName,  
					Birthdate__c=c.Student__r.Birthdate, 
					EC_Name__c=c.Student__r.EC_Name__c,
					EC_Other_Phone__c=c.Student__r.EC_Other_Phone__c,
					EC_Phone__c=c.Student__r.EC_Phone__c, 
					EC_Relationship__c=c.Student__r.EC_Relationship__c,
					School_AccountID__c=c.AccountID);
				SL.add(SLUpdate);
			}
		}
		upsert SL;
	}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can't use the Map<T,U> constructor that takes a List<U>, unless T is the data type of ID, and U is an SObject. That means that you can do this:

 

 

Map<Id,Student_Lookup__c> SList = new Map<Id,Student_Lookup__c>([select ContractId__c,Id from student_Lookup__c where contractId__c <> null]);

In this context, you can find a ContractId__c by using the key of a Student_Lookup__c.Id.

 

 

To make the map in the manner you are attempting to do, you would have to use the usual for-each loop:

 

 

Map<Id, ID> SList = new Map<Id, Id>();
for(Student_Lookup__c sl:[select id,ContractId__c from Student_Lookup__c where ContractId__c <> null])
   slist.put(sl.contractId__c,sl.id);

I assume that this is your use case based on your code example. Note that Id, Id is the most appropriate here, since ContractId__c is obviously an ID value.

 

All Answers

sfdcfoxsfdcfox

You can't use the Map<T,U> constructor that takes a List<U>, unless T is the data type of ID, and U is an SObject. That means that you can do this:

 

 

Map<Id,Student_Lookup__c> SList = new Map<Id,Student_Lookup__c>([select ContractId__c,Id from student_Lookup__c where contractId__c <> null]);

In this context, you can find a ContractId__c by using the key of a Student_Lookup__c.Id.

 

 

To make the map in the manner you are attempting to do, you would have to use the usual for-each loop:

 

 

Map<Id, ID> SList = new Map<Id, Id>();
for(Student_Lookup__c sl:[select id,ContractId__c from Student_Lookup__c where ContractId__c <> null])
   slist.put(sl.contractId__c,sl.id);

I assume that this is your use case based on your code example. Note that Id, Id is the most appropriate here, since ContractId__c is obviously an ID value.

 

This was selected as the best answer
Terry411Terry411

That makes sense and it works perfectly.  Thank you!