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
Rakesh MRakesh M 

Pagereference issue

public class RecordCreations 
{
    public string name{set;get;}
    public account acc{set;get;}
    public contact con{set;get;}
    public pagereference createAccount()
    {
        acc=new account();
        acc.name=name;
        insert acc;
        pagereference pr = new pagereference('/'+acc.id);
        return pr;
    }
    public pagereference createContact()
    {
        con=new contact();
        con.lastname=name;
        insert con;
        pagereference p = new pagereference('/'+con.id);
        return p;
    }
}
<apex:page controller="RecordCreations">
    <apex:sectionHeader title="Record Creations" subtitle="Accounts & Contacts creations"/>
    <apex:form >
        <apex:pageBlock >
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Account" action="{!createAccount}"/>
            <apex:commandButton value="Contact" action="{!createContact}"/>
                </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputText Label="Enter Name : " value="{!name}"/>
            </apex:pageBlockSection>
            </apex:pageBlock>
    </apex:form>
</apex:page>

User-added image
After clicking account, the code works fine and below page comes.
User-added image
But when I click contact button, The error comes, request you to debug the error and let me know.
Thanks in advance.User-added image
Best Answer chosen by Rakesh M
Harish RamachandruniHarish Ramachandruni
Hi,


Any Trigger for contact and in error it's showing email filed . Pass Email field in code like below .
 
public class RecordCreations 
{
    public string name{set;get;}
    public account acc{set;get;}
    public contact con{set;get;}
    public pagereference createAccount()
    {
        acc=new account();
        acc.name=name;
        insert acc;
        pagereference pr = new pagereference('/'+acc.id);
        return pr;
    }
    public pagereference createContact()
    {
        con=new contact();
        con.lastname=name;
        con.Email = 'harish.rao.salesforce.com';
        insert con;
        pagereference p = new pagereference('/'+con.id);
        return p;
    }
}

Check Below code it will work any issue ask me .


Regards ,
Harish.R

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Rakesh
you got this error becasue any validation rule active on contact object
check validation rule on contact object.
setup --> contact---> validation rule
Thanks
 
Rakesh MRakesh M
Hi Piyush,

No validation rules are activated in my contact object.

Please see below image.

User-added image

Thanks in advance.
 
sfdcMonkey.comsfdcMonkey.com
check any other requried field on contact object  ?
 
Harish RamachandruniHarish Ramachandruni
Hi,


Any Trigger for contact and in error it's showing email filed . Pass Email field in code like below .
 
public class RecordCreations 
{
    public string name{set;get;}
    public account acc{set;get;}
    public contact con{set;get;}
    public pagereference createAccount()
    {
        acc=new account();
        acc.name=name;
        insert acc;
        pagereference pr = new pagereference('/'+acc.id);
        return pr;
    }
    public pagereference createContact()
    {
        con=new contact();
        con.lastname=name;
        con.Email = 'harish.rao.salesforce.com';
        insert con;
        pagereference p = new pagereference('/'+con.id);
        return p;
    }
}

Check Below code it will work any issue ask me .


Regards ,
Harish.R
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Rakesh M,

Please check below point
1) Have you created any Email Id field on contact object as required field ?  IF yes then you need to pass value Email Id value on contact.
2) Please check you created any Trigger on Contact object for email id validation ?

NOTE:- you need to pass Email ID also while creating contact