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
Satish PrajapatSatish Prajapat 

How to write code for the FieldSet and Wrapper classes.?

public class QuizExcelFile 
{
    public List<QOA> listQOA {get;set;}
    public Integer count {get;set;}          // used for append the question number in question.
    public Integer correctAnswer {get;set;}  //number of correct answer.
    public Integer incorrectAnswer {get;set;}//number of incorrect answer.
    public Integer total {get;set;}          //total number of questions.
    public String firstname {get;set;}       // Firstname of candidate.
    public String lastname {get;set;}        // Lastname of candidate.
    public String contact {get;set;}         //Contact number of candidate.
    public String email {get;set;}           //Email address of candidate.
    public Boolean block1 {get;set;}         // Registration block rendered value.
    public Boolean block2 {get;set;}         // Quiz block rendered value.
    public Boolean block3 {get;set;}         //Result block rendered value.
    public Id quizId {get;set;}              //store id of quiz save from url.
    public Contact conObj { get; set; }      //contact Object for retrieving the value of field Set at the time of user registration.
    
    public QuizExcelFile()                   //Controller
    {
        block1 = true;              //deault value is true for registration block.
        block2 = false;             //deault value is false for Quiz block.
        block3 = false;             //deault value is false for result block.
        firstname = null;           //Candidate firstname is null by default.
        lastname = null;            //Candidate lastname is null by default.
        contact = null;             //Candidate cotatact is null by default.
        email = null;               //Candidate Email Address is null by default.
        correctAnswer = 0;          //Initialized zero to the correct answer.
        incorrectAnswer = 0;        //Initialized zero to the incorrect answer.
        total = 0;                  //Initialized zero to the total number of question.
        count = 0;                  //Initialized zero to the count value.
        listQOA = new List<QOA>();//Initialized the list of QOA wrapper class type.
        conObj = new Contact();   // Initialize the contact object.
    }   
    public List<Schema.FieldSetMember> getFields() 
    {
    	return SObjectType.Contact.FieldSets.Quiz_Field_Set.getFields();
    }
    
   
public void initializedLogic()
    {
        //Retriving the quid Id named "quizId" from Url used in the formula field.
        quizId = apexpages.currentpage().getparameters().get('quizId');
        //Checking the Quiz is active or not by API name of Field "Active__c".
        List<Quiz__c>  quizList = [select Active__c, name from Quiz__c where id=:quizId ];
        String str = quizList.get(0).Active__c +'';
        if(str.equals('false')) //If Checkbox is not active, the generate Error Message on VF page.
        {
            //Error Message
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Quiz is not Activated please contact your administrator..!!!'));
            block1 = false;//Set all block Hide.
            block2 = false;
            block3 = false;
        }
        else
        {
            //Getting the record from the Latest file placed in atttachment of corresponding Quiz.
            List<Attachment> attachmentList = [Select Id,body From Attachment where ParentId=:quizId order by LastModifiedDate desc limit 1];
            if(attachmentList.size() == 0) // If no Attachment is found then generate error.
            {
                //Error Message
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Quiz is not Activated please contact your administrator..!!!'));
                block1 = false;//Set all block Hide.
                block2 = false;
                block3 = false;
            }
            else
            {
                String contents = attachmentList.get(0).body.toString();// Convert body into single String.
                
                for(String lines : contents.split('\n')) //Split the whole string based on the nextLine or newLine.
                {
                    String question = null;//Temporary question is stored here.
                    List<SelectOption> option_List = new list<SelectOption>();//Temporary all Option are stored in this List.
                    String answer = null;//Temporary answer is stored into the this variable.
                    count++;//increase count number, for next question number.
                    String[] qOptionArray= lines.split(',');//Split the line based on the value, which are saved in Excel file.
                  
                    question = Integer.valueOf(count) +'. '+ qOptionArray[0];
                    for(Integer i = 1; i<(qOptionArray.size() - 1); i++) 
                    {
                        if(qOptionArray[i].length() > 0)
                        {
                            option_List.add(new SelectOption(qOptionArray[i],qOptionArray[i]));
                        }
                    }
                    
                    answer  = qOptionArray[qOptionArray.size()-1];
                    
                    listQOA.add(new QOA(question,option_List,answer));
                }
            } 
        }
    }
    
    
    public void startTest()
    {
        
        block1 = false;
        block2 = true;
        block3 = false;
       
        firstname = conObj.FirstName;
        lastname = conObj.LastName;
        contact = conObj.Phone;
        email = conObj.Email;
        List<contact> conList1 = new List<Contact>();//List is used to insert the record.
        
        List<contact> conList2 = [Select Id, name from contact where Email=:conObj.email];
        conList2.add(conObj);
        if(conList1.size() > 0 && conList2.size() == 0)
        {
            if(conList2.size() == 0)
            {
                //Insert record
                insert conList1;
            }
            else
            {
                //Update Record
                update conList2;
            }
        }
        
    }
   
    public void check()
    {
        //validate the answer with user response.
        for(QOA tempList1:listQOA)
        {
            if(tempList1.Answer.equals(tempList1.response))
            {
                correctAnswer++;//Icnrease counting when correct answer.
            }
            else
            {
                incorrectAnswer++;//Increase counting when incorrect answer
            }
        }
        total = listQOA.size();//Total number of Question is equal to the list size.
        
        block1 = false;
        block2 = false;
        block3 = true;
        
        List<contact> conList3 = [Select Id, name from contact where Email=:conObj.email];
        // Creating temporary List Object of "Summary__c" Object.
        List<Summary__c> SummaryListObj1 = new List<Summary__c>();
        // Getting total number of Summary__c record whose contact is created or updated recently.
        List<Summary__c> SummaryListObj2 = [Select Id, name from Summary__c where Contact__c=:conList3.get(0).Id];
        Summary__c SummaryObj1 = new Summary__c();
        SummaryObj1.Name = SummaryListObj2.size()+1+'';
        SummaryObj1.Contact__c = conList3.get(0).Id;
        SummaryObj1.Corrected_Answer__c = correctAnswer;
        SummaryObj1.Incorrect_Answer__c = incorrectAnswer;
        SummaryObj1.Total_Number_of_questions__c = total;
        SummaryListObj1.add(SummaryObj1);
        if(SummaryListObj1.size() > 0)
        {
            insert SummaryObj1;
        }        
        List<Question__c> Q_list = new List<Question__c>();
        for(QOA tempList2:listQOA)
        {	
            Question__c queObj = new Question__c();
            Id id1 = SummaryObj1.id;
            queObj.Name = tempList2.Question.split('\\.')[0];
            queObj.Question__c = tempList2.Question.split('\\.')[1];
            queObj.Response__c = tempList2.response;
            queObj.Answer__c = tempList2.Answer;
            queObj.Summary__c = id1;
            String str = '';
            for(SelectOption tempList3: tempList2.Options)
            {
            	str = str + tempList3.getLabel() + '; ' ; 
            }
            queObj.Options__c = str;
            Q_list.add(queObj);
        }
        insert Q_list;
    }
    public with sharing class QOA
    {
        public String question {get;set;}
        public List<SelectOption> Options {get;set;}
        public String answer {get;set;}
        public String response {get;set;}
        public QOA(String que, List<SelectOption> list_Options, String ans)
        {
            Question = que;
            Options = list_Options;
            Answer = ans;
        }
    }
}