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
Rajat Jain 1Rajat Jain 1 

Please help me to improve my code

Hello Everyone,

I have created one VF page which will ask you to select the "Object" for which you wan to create record and after selecting particular object it will show us fields panel , where we have to enter the fields value and then we can save therecord.
User-added imageUser-added image

Issue :  When I am switching between the object dropdown, It is forcing me to enter some value for the mandatory fields that should no be.

If possible, please also provide me some tips to write the same fucntionality in a simpler/ short way.

Thanks,
Rajat
Rajat Jain 1Rajat Jain 1
Hello,

Sorry I forgot to add the code, please find the below code.

VF Page : 
<apex:page sidebar="false" controller="VFCreateRecordClass" showHeader="false">
    <apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection title="Select Object For Record Creation">
            <apex:selectList size="1" value="{!record}">
                <apex:selectOptions value="{!Items}"/>
                <apex:actionSupport event="onchange" rerender="output" />
            </apex:selectList>   
        </apex:pageBlockSection>
        </apex:pageBlock>       
        <apex:pageBlock title="Enter Record Details" id="output">
            <apex:pageBlockSection rendered="{!record == 'Account'}">
                <apex:inputField value="{!acct.Name}" />
                <apex:inputField value="{!acct.Industry}" />
                <apex:inputField value="{!acct.Type}" />
                <apex:inputField value="{!acct.Phone}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection rendered="{!record == 'Contact'}">
                <apex:inputField value="{!cont.FirstName}" />
                <apex:inputField value="{!cont.LastName}" />
                <apex:inputField value="{!cont.Email}" />
                <apex:inputField value="{!cont.Phone}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection rendered="{!record == 'Opportunity'}">
                <apex:inputField value="{!oppty.Name}" />
                <apex:inputField value="{!oppty.CloseDate}" />
                <apex:inputField value="{!oppty.StageName}" />
                <apex:inputField value="{!oppty.Amount}" />
            </apex:pageBlockSection    >   
            <apex:pageBlockButtons>
                <apex:commandButton action="{!Save}" value="Save Record"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    
  
    </apex:form>
</apex:page>

Apex Class :
public class VFCreateRecordClass 
{
    public Account acct{get;set;}
    public Contact cont{get;set;}
    public Opportunity oppty{get;set;}
    public String record {get;set;}
    
    public VFCreateRecordClass()
    {
        acct = new Account();
        cont = new Contact();
        oppty = new Opportunity();
    }
    
  public List<SelectOption> getItems()
  {
      List<SelectOption> options = new List<SelectOption>();
      options.add(new SelectOption('Select An option','Select An options'));
      options.add(new SelectOption('Account','Account'));
      options.add(new SelectOption('Contact','Contact'));
      options.add(new SelectOption('Opportunity','Opportunity'));
      options.add(new SelectOption('Case','Case'));
      return options;
  }
    
   public PageReference Save()
   {
       System.debug('Account : '+acct);
       System.debug('Contact : '+cont);
       System.debug('Oppty : '+oppty);
       if(acct != NULL && (acct.Name != Null))
           insert acct;
      if(cont != NULL && (cont.LastName != NULL))
           insert cont;
      if(oppty != NULL && oppty.Name != Null)
           insert oppty;

       PageReference pr = ApexPages.currentPage();
        pr.setRedirect(true);
        return pr;
   }
}
Amit Chaudhary 8Amit Chaudhary 8
You can try below code to get required field of object

Schema.DescribeSObjectResult r = User.sObjectType.getDescribe(); // Just Change the object Name
            Map<String,Schema.SObjectField> M = r.fields.getMap();
            for(String fieldName : M.keySet())
            { 
                Schema.SObjectField field = M.get(fieldName);
                Schema.DescribeFieldResult F = field.getDescribe();
               //A nillable field can have empty content. A isNillable Boolean non-nillable field must have a value for the object to be                       //created or saved. 
              // if F.isNillable() is false then field is mandatory
              Boolean isFieldreq  = F.isNillable() ;
             System.debug ('F = ' + fieldName + ' isnul= ' +  isFieldreq);
           }