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
Alap MistryAlap Mistry 

Remediate CRUD and FLS violations in Apex

My code is given below. It displays error to me:
Error: Compile Error: Method does not exist or incorrect signature: [Schema.DescribeFieldResult].isCreatable() at line 44 column 25
 
public with sharing class CRUD_FLS_Create_Challenge{

    public Id newUser {get;set;}
    
    public List<Personnel__c> getUnReg() {
        unregisteredUsers = new List<Personnel__c>();
       List<Jouster__c> currentParticipants = [ select Participant_Name__r.Name from Jouster__c ];
       List<Personnel__c> currentPersonnel = [ select Id, Name, Favorite_Color__c, Castle__r.Name from Personnel__c limit 15];
       Boolean reg;
       
       System.debug(currentParticipants);
       System.debug(currentPersonnel);
       
       for( Personnel__c p : currentPersonnel)
       {
           reg = false;
           for(Jouster__c j : currentParticipants)
           {
               if(j.Participant_Name__r.Name == p.Name) {
                   reg = true;
                   break;
               }
           }
           
           if(reg == false)
               unregisteredUsers.add(p);
       }
       
       System.debug(unregisteredUsers);
       
       return unregisteredUsers;
    }

    public Jouster__c newParticipant {get;set;}
    public List<Personnel__c> unregisteredUsers;
    
    
    public void register(){
        System.debug(newUser);
        Personnel__c p = [select Name, Favorite_Color__c, Castle__r.Name from Personnel__c where Id =: newUser]; 
        if (!Schema.sObjectType.Jouster__c.fields.Participant_Name__c.isCreatable()) {
            if (!Schema.sObjectType.Jouster__c.fields.Color__c.isCreatable()) {
                if(!Schema.sObjectType.Jouster__c.fields.Favorite_Color__c.isCreatable()) {  
                    if(!Schema.sObjectType.Jouster__c.fields.Castle__c.isCreatable()) {   
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Insufficient Access'));                 
                    }
                }
            }  
        }
        if([select id from Jouster__c where Participant_Name__r.Name =: p.Name] != null)
            insert new Jouster__c(Participant_Name__c=newUser, Color__c=p.Favorite_Color__c, Castle__c=p.Castle__c);
        else
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Jouster already entered in Tournament'));
    }    
    public string[] getPermSets(){
        String[] permSetArray = new List<string>();
        PermSetArray.add('User with Read ONLY Access to the Jousters object'); // description of the needed permission set
        return permSetArray;
    }

}

 
Best Answer chosen by Alap Mistry
Charisse de BelenCharisse de Belen
Hi Alap,

You are using isCreatable(), but I believe the correct spelling of the method is isCreateable().

I hope this helps!

All Answers

Alex EzhilarasanAlex Ezhilarasan
Probably it is a read only field. You can use isAccessbile() to check if the current user can see a particular field.
Alap MistryAlap Mistry
After checking Challenge, I got this error:

Challenge Not yet complete... here's what's wrong:
The code does not appear to be preventing unauthorized users from registering new jousting participants for the tournament. Make sure you're checking create access for each of the inserted fields.
Alex EzhilarasanAlex Ezhilarasan
The class error is fixed, right? Post the challenge question. Also check the field level permission for Jouster__c.Castle__c
Alap MistryAlap Mistry
Remediate CRUD and FLS violations in Apex
Your kingdom is holding a tournament, and it's up to you to make sure that only authorized jousters can register to participate. Choose the CRUD/FLS & Sharing app in your Kingdom Management Developer Edition org, navigate to the CRUD & FLS Fix Challenge tab, and click the Apex Controller link. Then add the appropriate checks to the Apex code.
For this module, you can’t use a standard Developer Edition org or Trailhead Playground. You must sign up for a super-special Kingdom Management DE org. Trust us...it’s worth it.
    Click the Apex Controller link in the CRUD & FLS Fix Challenge tab of the CRUD/FLS & Sharing app
    Find the register function
    Add checks to the code to prevent unauthorized users from registering new jousting participants for the tournament
Charisse de BelenCharisse de Belen
Hi Alap,

You are using isCreatable(), but I believe the correct spelling of the method is isCreateable().

I hope this helps!
This was selected as the best answer
Alap MistryAlap Mistry
Hi Charisse,
       Thank you for your answer. I passed this challenge.

Regards,
Alap Mistry
PammyPammy
Hi Alap,

Can you please write the completed solution for this challenge? I am not able to complete it. :(

Thanks,
Pamy
testimomolttestimomolt
public void register(){

    if (!Schema.sObjectType.Jouster__c.fields.Name.isCreateable()){
        if (!Schema.sObjectType.Jouster__c.fields.Participant_Name__c.isCreateable()) {
            if(!Schema.sObjectType.Jouster__c.fields.Castle__c.isCreateable()) {   
        
                System.debug(newUser);
                Personnel__c p = [select Name, Favorite_Color__c, Castle__r.Name from Personnel__c where Id =: newUser];
                
                if([select id from Jouster__c where Participant_Name__r.Name =: p.Name] != null)
                    insert new Jouster__c(Participant_Name__c=newUser, Color__c=p.Favorite_Color__c, Castle__c=p.Castle__c);
                else
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Jouster already entered in Tournament'));
            }
        }
    }
    else{
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: User is unauthorized to register a jouster'));
    }            
}

 
Alap MistryAlap Mistry
Hi Pammy,
My code is below.
public with sharing class CRUD_FLS_Create_Challenge{

    public Id newUser {get;set;}
    
    public List<Personnel__c> getUnReg() {
        unregisteredUsers = new List<Personnel__c>();
       List<Jouster__c> currentParticipants = [ select Participant_Name__r.Name from Jouster__c ];
       List<Personnel__c> currentPersonnel = [ select Id, Name, Favorite_Color__c, Castle__r.Name from Personnel__c limit 15];
       Boolean reg;
       
       System.debug(currentParticipants);
       System.debug(currentPersonnel);
       
       for( Personnel__c p : currentPersonnel)
       {
           reg = false;
           for(Jouster__c j : currentParticipants)
           {
               if(j.Participant_Name__r.Name == p.Name) {
                   reg = true;
                   break;
               }
           }
           
           if(reg == false)
               unregisteredUsers.add(p);
       }
       
       System.debug(unregisteredUsers);
       
       return unregisteredUsers;
    }

    public Jouster__c newParticipant {get;set;}
    public List<Personnel__c> unregisteredUsers;
    
    
    public void register(){
            System.debug(newUser);
            
                Personnel__c p = [select Name, Favorite_Color__c, Castle__r.Name from Personnel__c where Id =: newUser]; 
                if (!Schema.sObjectType.Jouster__c.fields.Participant_Name__c.isCreateable()) {
                    if (!Schema.sObjectType.Jouster__c.fields.Color__c.isCreateable()) {
                        if (!Schema.sObjectType.Jouster__c.fields.Castle__c.isCreateable()) {     
                            if([select id from Jouster__c where Participant_Name__r.Name =: p.Name] != null)
                                insert new Jouster__c(Participant_Name__c=newUser, Color__c=p.Favorite_Color__c, Castle__c=p.Castle__c);
                            else
                                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Jouster already entered in Tournament'));
                        }
                    }
                }  
         
    }    
    public string[] getPermSets(){
        String[] permSetArray = new List<string>();
        PermSetArray.add('User with Read ONLY Access to the Jousters object'); // description of the needed permission set
        return permSetArray;
    }

}
Regards,
Alap Misry
Ajay ChakradharAjay Chakradhar
This code worked for me.
public with sharing class CRUD_FLS_Create_Challenge{

    public Id newUser {get;set;}
    
    public List<Personnel__c> getUnReg() {
        unregisteredUsers = new List<Personnel__c>();
       List<Jouster__c> currentParticipants = [ select Participant_Name__r.Name from Jouster__c ];
       List<Personnel__c> currentPersonnel = [ select Id, Name, Favorite_Color__c, Castle__r.Name from Personnel__c limit 15];
       Boolean reg;
       
       System.debug(currentParticipants);
       System.debug(currentPersonnel);
       
       for( Personnel__c p : currentPersonnel)
       {
           reg = false;
           for(Jouster__c j : currentParticipants)
           {
               if(j.Participant_Name__r.Name == p.Name) {
                   reg = true;
                   break;
               }
           }
           
           if(reg == false)
               unregisteredUsers.add(p);
       }
       
       System.debug(unregisteredUsers);
       
       return unregisteredUsers;
    }

    public Jouster__c newParticipant {get;set;}
    public List<Personnel__c> unregisteredUsers;
    
    
    public void register(){
        System.debug(newUser);
        Personnel__c p = [select Name, Favorite_Color__c, Castle__r.Name from Personnel__c where Id =: newUser]; 
        if (!Schema.sObjectType.Jouster__c.fields.Participant_Name__c.isCreateable()) {
            if (!Schema.sObjectType.Jouster__c.fields.Color__c.isCreateable()) {
                    if(!Schema.sObjectType.Jouster__c.fields.Castle__c.isCreateable()) {   
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Insufficient Access'));                 
                    }
            }  
        }
        if([select id from Jouster__c where Participant_Name__r.Name =: p.Name] != null)
            insert new Jouster__c(Participant_Name__c=newUser, Color__c=p.Favorite_Color__c, Castle__c=p.Castle__c);
        else
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Jouster already entered in Tournament'));
    }    
    public string[] getPermSets(){
        String[] permSetArray = new List<string>();
        PermSetArray.add('User with Read ONLY Access to the Jousters object'); // description of the needed permission set
        return permSetArray;
    }

}

 
Francis CrumpFrancis Crump
Thanks Ajay
RiteshKonduruRiteshKonduru
Thanks ajay
Julian Juez Alfaro 5Julian Juez Alfaro 5
Thanks, Ajay!
SureshKumarSureshKumar
Hi, my code for the register() method in the challenge is below
 
public void register(){
		System.debug(newUser);
		Personnel__c p = [select Name, Favorite_Color__c, Castle__r.Name from 
                                               Personnel__c where Id =: newUser];
                   
        if (Schema.sObjectType.Jouster__c.fields.Participant_Name__c.isCreateable() && 
             Schema.sObjectType.Jouster__c.fields.Color__c.isCreateable() && 
              Schema.sObjectType.Jouster__c.fields.Castle__c.isCreateable()) {
            
            if([select id from Jouster__c where Participant_Name__r.Name =: p.Name] != null)
                insert new Jouster__c(Participant_Name__c=newUser, Color__c=p.Favorite_Color__c, Castle__c=p.Castle__c);
            else
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Jouster already entered in Tournament'));
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Insufficient Privileges'));
        }
    }