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
Joe HayesJoe Hayes 

Compare element of list against set

Hi,

I am writing a controller to check the Name field in a list of child records against a field called Approved_scope__c on a custom object. If the Name fields in the List are all contained in the Approved_scope__c field then it needs to insert the list, else it needs to display which of the name fields are not in the Approved_scope__c.

I have put the Approved_scope__c field into a set and then I am trying to use set.contains(List.Name)

It is giving an error at the moment on line 17 'Initial term of field expression must be a concrete SObject: List'
I'm not sure where I am going wrong.

Also how would I then get a new list of records that weren't contained in the set?

Any help would be appreciated thank you.

Controller:
public class AddingAssessmentsController {
    Id candId;
    Id centreId;
    
    public List<Assessment__c> AssessmentList {get;set;}
    public Integer rowNum{get;set;}
    
    public AddingAssessmentsController(){
        candId = ApexPages.currentPage().getParameters().get('candId');
        AssessmentList = new List<Assessment__c>();
        AssessmentList.add(new Assessment__c(Candidate__c=candId, pass__c=true, photo_scanned__c=true));
    }
        
	public PageReference insertAssessments(){
		centreId = ApexPages.currentPage().getParameters().get('centreId');
		set <Certification_Accredited_Centres__c> centreSet = new set <Certification_Accredited_Centres__c>([Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId]);
          if(centreSet.contains(';' + AssessmentList.Name + ';')){
        	insert AssessmentList;
			PageReference reRender = new PageReference('/'+candId);
			reRender.setRedirect(true);
			return reRender;
        } else {
            return null; 
        }
    }

    public PageReference cancelAssessments(){
        PageReference reRender = new PageReference('/'+candId);
        reRender.setRedirect(true);
        return reRender;
    }
    
    public void insertAsstRow(){
        if(AssessmentList.Size()>0){
        Assessment__c tmpAssessment = AssessmentList.get(0);
        AssessmentList.add(new Assessment__c(Candidate__c=candId,
                                             Verifier__c=tmpAssessment.Verifier__c,
                                             Assessor__c=tmpAssessment.Assessor__c,
                                             LastCentreDate__c=tmpAssessment.LastCentreDate__c,
                                             DateofTechReview__c=tmpAssessment.DateofTechReview__c,
                                             Date_Received_By_Cert_Office__c=tmpAssessment.Date_Received_By_Cert_Office__c,
                                             Photo_Scanned__c=tmpAssessment.Photo_Scanned__c,
                                             Photo_Scan_Date__c=tmpAssessment.Photo_Scan_Date__c,
                                             Fastrack__c=tmpAssessment.Fastrack__c,
                                             MOT_Cert__c=tmpAssessment.MOT_Cert__c,
                                             Pass__c=tmpAssessment.Pass__c                                             
                                            ));
    }
        else AssessmentList.add(new Assessment__c(Candidate__c=candId, pass__c=true, photo_scanned__c=true));
    }
    
    public void delAsstRow(){
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        AssessmentList.remove(rowNum);
    }
}

vf page if needed:
<apex:page controller="AddingAssessmentsController">
    <style>
        .homeTab .pageTitleIcon, .individualPalette .homeBlock .pageTitleIcon
        {
            background-position: initial;
            background-image: url('/img/icon/wrench32.png');
        }
    </style>
    
    <apex:form >
        <apex:variable var="rowNum" value="{!0}"/>
        <apex:pageBlock >            
            <apex:sectionHeader subtitle="Add Assessments"/>
            <apex:variable var="rowNum" value="{!0}"/>
            <apex:pageBlockTable value="{!AssessmentList}" var="asst">
                <apex:facet name="footer">
                    <apex:commandLink value="Add" action="{!insertAsstRow}"/>
                </apex:facet>
                <apex:column headerValue="Name">
                    <apex:inputField value="{!asst.Name}" required="true"/>
                </apex:column>
                <apex:column headerValue="Last Centre Date">
                    <apex:inputField value="{!asst.LastCentreDate__c}" id="LastCentreDate"/>
                </apex:column>  
                <apex:column headerValue="Date of Tech Review">
                    <apex:inputField value="{!asst.DateofTechReview__c}" id="DateOfTechReview"/>
                </apex:column>
                <apex:column headerValue="Date Received By Cert Office">
                    <apex:inputField value="{!asst.Date_Received_By_Cert_Office__c}" id="Date_Received_By_Cert_Office"/>
                </apex:column>
                <apex:column headerValue="Assessor">
                    <apex:inputField value="{!asst.Assessor__c}" id="Assessor"/>
                </apex:column>
                <apex:column headerValue="Verifier">
                    <apex:inputField value="{!asst.Verifier__c}" id="Verifier"/>
                </apex:column>
                <apex:column headerValue="Photo Scanned">
                    <apex:inputcheckbox value="{!asst.Photo_Scanned__c}" id="photoscanned" selected="true"/>
                </apex:column>
                <apex:column headerValue="Photo Scan date">
                    <apex:inputfield value="{!asst.Photo_Scan_Date__c}" id="photo_scan_date"/>
                </apex:column>
                <apex:column headerValue="Fastrack">
                    <apex:inputcheckbox value="{!asst.Fastrack__c}" id="Fastrack"/>
                </apex:column>
                <apex:column headerValue="MOT Cert">
                    <apex:inputcheckbox value="{!asst.MOT_Cert__c}" id="Mot_cert"/>
                </apex:column>
                <apex:column headerValue="Pass?">
                    <apex:inputcheckbox value="{!asst.Pass__c}" id="pass" selected="true"/>
                </apex:column>
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delAsstRow}" immediate="true">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>          
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!insertAssessments}"/>
                <apex:commandButton value="Back" action="{!cancelAssessments}" immediate="true"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thank you
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
public PageReference insertAssessments(){
        List<Assessment__c> inslist=new List<Assessment__c>();
		centreId = ApexPages.currentPage().getParameters().get('centreId');
		set <Certification_Accredited_Centres__c> centreSet = new set <Certification_Accredited_Centres__c>([Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId]);
    for(Assessment__c a:AssessmentList){
          if(centreSet.contains(';' + a.Name + ';')){
        	//insert AssessmentList;
            inslist.add(a);
			PageReference reRender = new PageReference('/'+candId);
			reRender.setRedirect(true);
			return reRender;
        } else {
            return null; 
        }
      }
      insert inslist;
    }

 
Joe HayesJoe Hayes
Thanks Bala,

I am getting an error Incompatible element type String for collection of Certification_Accredited_Centres__c
it is on line 19 'if(centreSet.contains(';' + a.Name + ';'))'

 
Joe HayesJoe Hayes
I guess this is becasue my set neds to be a string?
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
Change the set like this,
List<Certification_Accredited_Centres__c> centers=[Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId];
set <String> centreSet = new set <String>();
for(Certification_Accredited_Centres__c c:centers){
   centreset.add(String.valueOf(c.Approved_Scope__c)); 
}

function will be like
public PageReference insertAssessments(){
        List<Assessment__c> inslist=new List<Assessment__c>();
		centreId = ApexPages.currentPage().getParameters().get('centreId');
		/*set <Certification_Accredited_Centres__c> centreSet = new set <Certification_Accredited_Centres__c>([Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId]); */
  
    List<Certification_Accredited_Centres__c> centers=[Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId];
set <String> centreSet = new set <String>();
for(Certification_Accredited_Centres__c c:centers){
centreset.add(String.valueOf(c.Approved_Scope__c));
}
     
    for(Assessment__c a:AssessmentList){
          if(centreSet.contains(';' + a.Name + ';')){
        	//insert AssessmentList;
            inslist.add(a);
			PageReference reRender = new PageReference('/'+candId);
			reRender.setRedirect(true);
			return reRender;
        } else {
            return null; 
        }
      }
      insert inslist;
    }


 
Joe HayesJoe Hayes
It always seems to return null, i'm not sure why.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Name is not matching to the set. Change your if condition like this,
if(centreSet.contains(String.valueOf(a.Name))){
            //insert AssessmentList;
            inslist.add(a);
            PageReference reRender = new PageReference('/'+candId);
            reRender.setRedirect(true);
            return reRender;
 }


 
brahmaji tammanabrahmaji tammana
Can you try the function like below:
 
public PageReference insertAssessments(){
        List<Assessment__c> inslist=new List<Assessment__c>();
		centreId = ApexPages.currentPage().getParameters().get('centreId');
		/*set <Certification_Accredited_Centres__c> centreSet = new set <Certification_Accredited_Centres__c>([Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId]); */
  
    List<Certification_Accredited_Centres__c> centers=[Select Approved_Scope__c from Certification_Accredited_Centres__c where Id = :centreId];
set <String> centreSet = new set <String>();
Map<String, String> centreMap = new Map<String, String>();
for(Certification_Accredited_Centres__c c:centers){
centreMap.put(String.valueOf(c.Approved_Scope__c), '');
}
     
    for(Assessment__c a:AssessmentList){
          if(centreMap.containsKey(a.Name)){
        	//insert AssessmentList;
            inslist.add(a);
			PageReference reRender = new PageReference('/'+candId);
			reRender.setRedirect(true);
			return reRender;
        } else {
            return null; 
        }
      }
      insert inslist;
    }

Using maps.