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
Sv Krishna ReddySv Krishna Reddy 

Constructor not defined: [PageReference].<Constructor>(String)

public class Duplicate {
    public Account acc {set;get;}
    public Duplicate(){
        acc=new Account();
    }
    public PageReference create(){
        PageReference p;
        try{
          integer count=[select count() from Account where name=:acc.name];
              if(count>0){
                Apexpages.Message msg=new Apexpages.Message(Apexpages.Severity.ERROR,'Duplicate Record found');
                Apexpages.addMessage(msg);
               }
              else{
                insert acc;
                p=new PageReference('/'+acc.Id);
               }
             }
         catch(Exception e){
            Apexpages.Message msg=new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(msg);
        }
        return p;
    }
}
Best Answer chosen by Sv Krishna Reddy
Maharajan CMaharajan C
Hi Krishna,

Instead of  PageReference  try to use the system.PageReferenc
 
public class Duplicate {
    public Account acc {set;get;}
    public Duplicate(){
        acc=new Account();
    }
    public PageReference create(){
        System.PageReference p;
        try{
            integer count=[select count() from Account where name=:acc.name];
            if(count>0){
                Apexpages.Message msg=new Apexpages.Message(Apexpages.Severity.ERROR,'Duplicate Record found');
                Apexpages.addMessage(msg);
            }
            else{
                insert acc;
                p=new System.PageReference('/'+acc.Id);
            }
        }
        catch(Exception e){
            Apexpages.Message msg=new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(msg);
        }
        return p;
    }
}

The error reason you can find it from below links;

https://salesforce.stackexchange.com/questions/221919/constructor-not-defined-pagereference-constructorstring

https://salesforce.stackexchange.com/questions/96148/getting-error-as-compile-error-constructor-not-defined-pagereference-constr

Thanks,
Maharajan.C

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Krishna,

Please change your class name and method name it might be possible some standard things are there in the apex like Duplicate

public class DataDuplicate {
    public Account acc{set;get;}
    public DataDuplicate(){
        acc=new Account();
    }
    public PageReference dataCreate(){
        PageReference p;
        try{
          integer count=[select count() from Account where name=:acc.name];
              if(count>0){
                Apexpages.Message msg=new Apexpages.Message(Apexpages.Severity.ERROR,'Duplicate Record found');
                Apexpages.addMessage(msg);
               }
              else{
                insert acc;
                p=new PageReference('/'+acc.Id);
               }
             }
         catch(Exception e){
            Apexpages.Message msg=new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(msg);
        }
        return p;
    }
}

Please mark it as the Best Answer if your queries are solved.

Thank You

Sv Krishna ReddySv Krishna Reddy
Again its show error
Maharajan CMaharajan C
Hi Krishna,

Instead of  PageReference  try to use the system.PageReferenc
 
public class Duplicate {
    public Account acc {set;get;}
    public Duplicate(){
        acc=new Account();
    }
    public PageReference create(){
        System.PageReference p;
        try{
            integer count=[select count() from Account where name=:acc.name];
            if(count>0){
                Apexpages.Message msg=new Apexpages.Message(Apexpages.Severity.ERROR,'Duplicate Record found');
                Apexpages.addMessage(msg);
            }
            else{
                insert acc;
                p=new System.PageReference('/'+acc.Id);
            }
        }
        catch(Exception e){
            Apexpages.Message msg=new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(msg);
        }
        return p;
    }
}

The error reason you can find it from below links;

https://salesforce.stackexchange.com/questions/221919/constructor-not-defined-pagereference-constructorstring

https://salesforce.stackexchange.com/questions/96148/getting-error-as-compile-error-constructor-not-defined-pagereference-constr

Thanks,
Maharajan.C
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

I am not getting any errors.

User-added image

Can you please try like this

 

System.PageReference p = new System.PageReference('/'+acc.Id);

or You can also change the API version.

public class DataDuplicate {
    public Account acc{set;get;}
    public DataDuplicate(){
        acc=new Account();
    }
    public PageReference dataCreate(){
       System.PageReference p ;
        try{
          integer count=[select count() from Account where name=:acc.name];
              if(count>0){
                Apexpages.Message msg=new Apexpages.Message(Apexpages.Severity.ERROR,'Duplicate Record found');
                Apexpages.addMessage(msg);
               }
              else{
                insert acc;
                  p= new System.PageReference('/'+acc.Id);
               
                  
               }
             }
         catch(Exception e){
            Apexpages.Message msg=new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage());
            ApexPages.addMessage(msg);
        }
        return p;
    }
}

Thank You

Sv Krishna ReddySv Krishna Reddy
i tried like System.pagereference p;
its fine,there is no errors but in vf page if i am enter duplicate value it does't show any popup msg
 
Maharajan CMaharajan C
Please post your Visualforce code also...
Sv Krishna ReddySv Krishna Reddy
<apex:page controller="Duplicate">
    <apex:sectionHeader title="Account"/>
    <apex:form >
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!create}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.name}"/>
                <apex:inputField value="{!acc.rating}"/>
                <apex:inputField value="{!acc.phone}"/>
                <apex:inputField value="{!acc.industry}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Maharajan CMaharajan C
Krishna am seeing the duplicate message is coming in the UI... Please see below...

User-added image