+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Illegal assignment error

I am getting an errro with my code:
Illegal assignment from List<sector__c> to id
What I am trying to do:
When a Candidate is made Primary, the Sector Record's Site Lookup field should get populated with the Site record that is linked with the Candidate record.
 
Map<Id, Sector__c> m_Sectors;
Map<Id, List<Sector__c>> m_sitesToSectors = new Map<Id, List<Sector__c>>();
List<Sector__c> sectorsToUpdate = new List<Sector__c>();

 public void bulkAfter(){{
 if (Trigger.IsUpdate && Trigger.isAfter) {
            for (SObject so : Trigger.New) {
                Candidate__c newCandidate = (Candidate__c) so;
               Candidate__c oldCandidate = (Candidate__c) Trigger.oldMap.get(newCandidate.Id);

if ((newCandidate.Primary_Candidate__c != oldCandidate.Primary_Candidate__c) && 
                     newCandidate.Primary_Candidate__c == true && newCandidate.Site__c != null) {                    candidateIdsPrimary.add(newCandidate.Id);                }

 if (!candidateIds.isEmpty()) {
                m_Sectors = new Map<Id, Sector__c>([SELECT Id,Candidate__c,Site__c FROM Sector__c WHERE Site__c = null AND Candidate__c IN: candidateIdsPrimary]);
            }

if (m_Sectors != null && !m_Sectors.isEmpty()){
                for (Sector__c sect : m_Sectors.values()) {
                    if (!m_sitesToSectors.containsKey(sect.Candidate__c)) {
                        m_sitesToSectors.put(sect.Candidate__c, new List<Sector__c>{sect});
                    }
                    else {
                        m_sitesToSectors.get(sect.Candidate__c).add(sect);
                    }
                }
            }

}

public void afterUpdate( SObject oldSo, SObject newSo ){

Candidate__c newCandidate = (Candidate__c) newSo;
Candidate__c oldCandidate = (Candidate__c) oldSo;

if (m_sitesToSectors.get(newCandidate.Id) != null && !m_sitesToSectors.get(newCandidate.Id).isEmpty()) {
            for (Sector__c sector: m_sitesToSectors.get(newCandidate.Id)) {
                sector.Site__c = m_sitesToSectors.get(newCandidate.Site__c); ---error here
                sectorsToUpdate.add(sector);
            }
        }

}

 
Best Answer chosen by Phuc Nguyen 18
ANUTEJANUTEJ (Salesforce Developers) 
list<Sector__c> sRecord=m_sitesToSectors.get(newCandidate.Site__c);
Sector__c rec = sRecord[0];
sector.Site__c =   rec.id;
sectorsToUpdate.add(sector);

Can you try the above changes once and beware that I am assuming that in the list of records the necessary record is first so I am taking only that particular record and assigning the id if not then you need to check each record and get the id of the record that satisfies your condition.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Phuc,

So the error in this line sector.Site__c = m_sitesToSectors.get(newCandidate.Site__c); is that the m_sitesToSectors has a key as id and the value as a list of records List<Sector__c>, also I think Site__c is a lookup field for the object sector, to assign a value you will have to get the id of the record and then assign the id to the sector.Site__c.

You can do Something like the below:
Sector__c sRecord=m_sitesToSectors.get(newCandidate.Site__c)[0];
sector.Site__c =   sRecord.id;
sectorsToUpdate.add(sector);

Please make changes as per your requirement.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Phuc Nguyen 18Phuc Nguyen 18
Hey Anutej,
You are corretc.  teh Site field is a lookup.  Code saves and dseployed but getting a 'Message[Attempt to de-reference a null object' error whne I go to update/save a record. But not seeing error message in my debug log.  
Phuc Nguyen 18Phuc Nguyen 18
This line is null Sector__c sRecord=m_sitesToSectors.get(newCandidate.Site__c)[0];
Eventhough I know there is a value there(in a test org).  Why am I mot getting it?
ANUTEJANUTEJ (Salesforce Developers) 
list<Sector__c> sRecord=m_sitesToSectors.get(newCandidate.Site__c);
Sector__c rec = sRecord[0];
sector.Site__c =   rec.id;
sectorsToUpdate.add(sector);

Can you try the above changes once and beware that I am assuming that in the list of records the necessary record is first so I am taking only that particular record and assigning the id if not then you need to check each record and get the id of the record that satisfies your condition.
This was selected as the best answer