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
Team CirrusTeam Cirrus 

Illegal assignment from list to list is the error am getting in the code below

public class chkDoctorRegn {
    public static void chkRegn(Map<Id, Doctor__c> oldV, List <Doctor__c> newV){
        Map<String, Id> docMap=new Map<String, Id>();
        List<Doctor__c> docList=[select Registration_no__c, Registration_State__c from Doctor__c];
        for(Doctor__c d : docList){
            docMap.put(d.Registration_no__c, d.Registration_State__c);
        }
        for(Doctor__c d1 : newV){
                if(d1.Registration_State__c!=oldV.get(d1.Id).Registration_State__c){
                    List<String> l=new List<String>();
                    l=[Select Registration_no__c from Doctor__c Where Registration_State__c=:d1.Registration_State__c];
                    if(l.contains(String.valueOf(d1.Registration_no__c))==true){
                        d1.addError('Incorrect Value: Updated State already has doctor with same registration no.');
                    }
                }
            }
        }
    }
    
    public static void chkInsRegn(List <Doctor__c> newV){
        Map<String, Id> docMap=new Map<String, Id>();
        List<Doctor__c> docList=[select Registration_no__c, Registration_State__c from Doctor__c];
        for(Doctor__c d : docList){
            docMap.put(d.Registration_no__c, d.Registration_State__c);
        }
        for(Doctor__c d1 : newV){
            if((docMap.get(d1.Registration_no__c)!=null) && (docMap.get(d1.Registration_no__c).equals(d1.Registration_State__c))){
                d1.addError('Incorrect Value: State already has doctor with same registration no.');
            }
        }
    }
}

The code gives error:
illegal assignment from list to list   -> line 11
missing '' at 'public'  -> line 20
Plz Help me out:)
Best Answer chosen by Team Cirrus
David Zhu 🔥David Zhu 🔥
You may need to change line 11 from 
 l=[Select Registration_no__c from Doctor__c Where Registration_State__c=:d1.Registration_State__c];

to
for (Doctor__c  doctor :[Select Registration_no__c from Doctor__c Where Registration_State__c=:d1.Registration_State__c])
{
     I.add(doctor.Registration_no__c );
}

you also need to remove a brace sign after line
d1.addError('Incorrect Value: Updated State already has doctor with same registration no.');
 

All Answers

David Zhu 🔥David Zhu 🔥
You may need to change line 11 from 
 l=[Select Registration_no__c from Doctor__c Where Registration_State__c=:d1.Registration_State__c];

to
for (Doctor__c  doctor :[Select Registration_no__c from Doctor__c Where Registration_State__c=:d1.Registration_State__c])
{
     I.add(doctor.Registration_no__c );
}

you also need to remove a brace sign after line
d1.addError('Incorrect Value: Updated State already has doctor with same registration no.');
 
This was selected as the best answer
Team CirrusTeam Cirrus
Thanks for your Quick Reply, it worked well!