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
neeruneeru 

save button and Save&New button Not Working

Hi everyone,

i am writing the controller for save,cancel,save &New buttons ..when i entering the values after i click the save button it throws error.and after i click the save&new button i am not getting any error but the values are not saved..

 

can anyone help this scenario please??

This is my Controller..

 

public with sharing class newtest {
 public Organizers__c org { get; private set; }
 public Organizers__c org1;
private ApexPages.StandardController sController;  
    private String queryString;  
              public newtest (ApexPages.StandardController controller) {  
        sController = controller;  
        org = (Organizers__c)controller.getRecord();  
     List<string> pageUrl = ApexPages.currentPage().getUrl().split('\\?');  
        queryString = pageUrl[1];  
    }  
        public newtest(){
        Id id = ApexPages.currentPage().getParameters().get('id');
        org1 = (id == null) ? new Organizers__c() :
            [SELECT FirstName__c, LastName__c, Organizer_Email__c,ProductType__c,Status__c FROM Organizers__c WHERE Id = :id];
    }

    public PageReference save() {
                    
        //  After Save, navigate to the default view page:
    
        return (new ApexPages.StandardController(org)).view();
    }
    public PageReference cancel() {
        
            return null;
        }
 public PageReference saveAndNew() {  
          
                  try {  
            // Save the current sObject  
            sController.save();  
 
            // Get the Meta Data for Foo__c  
            Schema.DescribeSObjectResult describeResult = sController.getRecord().getSObjectType().getDescribe();  
            // Create PageReference for creating a new sObject and add any inbound query string parameters.  
            PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?' + queryString);  
            // Don't redirect with the viewstate of the current record.  
            pr.setRedirect(true);  
            return pr;  
        } catch(Exception e) {  
            // Don't redirect if something goes wrong. May be a validation or trigger issue on save.  
       ApexPages.addMessages(e);  
            return null;  
    }
          
}
}

Best Answer chosen by Admin (Salesforce Developers) 
salvatoreovlassalvatoreovlas

ok then for the save and new you need to write your own method, and for the save and cancel you should just use the standard ones....

so your controller:

public with sharing class newtest {
 public Organizers__c org { get; private set; }

private ApexPages.StandardController sController;  
    private String queryString;  
              public newtest (ApexPages.StandardController controller) {  
        sController = controller;  
        org = (Organizers__c)controller.getRecord();  
 


    }  

 public Pagereference doSaveAndNew()
  {
  
    upsert org;

    string s = '/' + ('' + org.get('Id')).subString(0, 3) + '/e?';
    ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info, s));
    return new Pagereference(s);
  }


         
}

 

and your page:

 

<apex:page standardController="Organizers__c" extensions="newtest">


<apex:sectionHeader subtitle="New Organizer" title="Organizer Edit" />

<apex:pageMessages />


<apex:form >
<apex:pageBlock title="Organizers Edit" mode="edit">
<apex:pageBlockSection title="Information" >
<apex:inputField value="{!Organizers__c.FirstName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.LastName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Organizer_Email__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.ProductType__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Status__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="cancel"/>

 <apex:commandButton action="{!doSaveAndNew}" value="Save & New"/>


</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

hope that helps if yes mark it as soltion.

All Answers

SFFSFF

Generally, it helps if you provide the error message when asking this sort of question. "Not working" is a little vague.

 

Thanks,

neeruneeru

Thanx For rpl Sff

 

 

when pass the values of my fields after click the save button i am getting error like dis

 

System.NullPointerException: Argument cannot be null

Error is in expression '{!save}' in component <apex:page> in page reuse


Class.newtest.save: line 23, column 1

 

salvatoreovlassalvatoreovlas

Your problem seems to be that  you have 2 constructors.

If you want to have a controller extension for your visualforce page and then use the standard save() method you should use just this one:  public newtest (ApexPages.StandardController controller) { ...... } and on your visual force page just use what is shown below.delete then the other constructor

(  public newtest(){
        Id id = ApexPages.currentPage().getParameters().get('id');
        org1 = (id == null) ? new Organizers__c() :
            [SELECT FirstName__c, LastName__c, Organizer_Email__c,ProductType__c,Status__c FROM Organizers__c WHERE Id = :id];
    }

and if you need it put all the logic into the other one.

Please refer to : http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm.

 

<apex:page standardController="Organizers__c" extensions="newtest">
neeruneeru

thanx for reply  salvatoreovlas

 

i remove my second constructor i am getting some errors can you please change my code....please

 

Error: newtest Compile Error: The method object <Constructor>() is referenced by Visualforce Page (reuse) in salesforce.com. Remove the usage and try again. at line 13 column 9:

salvatoreovlassalvatoreovlas

would you please send me both the VF page and the controller?

neeruneeru

controller is in der in our 1st conversation

 

vf page:

<apex:page Controller="Organ" >
<apex:sectionHeader subtitle="New Organizer" title="Organizer Edit" />
<apex:form >
<apex:pageBlock title="Organizers Edit" mode="edit">
<apex:pageBlockSection title="Information" >
<apex:inputField value="{!Organizers__c.FirstName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.LastName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Organizer_Email__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.ProductType__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Status__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!SaveAndNew}" value="Save&New"/>
<apex:commandButton action="{!cancel}" value="cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

salvatoreovlassalvatoreovlas

public with sharing class newtest {
 public Organizers__c org { get; private set; }
 public Organizers__c org1;
private ApexPages.StandardController sController;  
    private String queryString;  
              public newtest (ApexPages.StandardController controller) {  
        sController = controller;  
        org = (Organizers__c)controller.getRecord();  
 


    }  

         
}

 

vf page:

 

<apex:page standardController="Organizers__c" extensions="newtest">


<apex:sectionHeader subtitle="New Organizer" title="Organizer Edit" />
<apex:form >
<apex:pageBlock title="Organizers Edit" mode="edit">
<apex:pageBlockSection title="Information" >
<apex:inputField value="{!Organizers__c.FirstName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.LastName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Organizer_Email__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.ProductType__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Status__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

salvatoreovlassalvatoreovlas

and why did you put a private set? it shouldnt be private....

neeruneeru

yes salvatoreovlas

 

through the extesions functionality it works fine....but i need custom controller logic??

 

 

salvatoreovlassalvatoreovlas

ok then for the save and new you need to write your own method, and for the save and cancel you should just use the standard ones....

so your controller:

public with sharing class newtest {
 public Organizers__c org { get; private set; }

private ApexPages.StandardController sController;  
    private String queryString;  
              public newtest (ApexPages.StandardController controller) {  
        sController = controller;  
        org = (Organizers__c)controller.getRecord();  
 


    }  

 public Pagereference doSaveAndNew()
  {
  
    upsert org;

    string s = '/' + ('' + org.get('Id')).subString(0, 3) + '/e?';
    ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info, s));
    return new Pagereference(s);
  }


         
}

 

and your page:

 

<apex:page standardController="Organizers__c" extensions="newtest">


<apex:sectionHeader subtitle="New Organizer" title="Organizer Edit" />

<apex:pageMessages />


<apex:form >
<apex:pageBlock title="Organizers Edit" mode="edit">
<apex:pageBlockSection title="Information" >
<apex:inputField value="{!Organizers__c.FirstName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.LastName__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Organizer_Email__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.ProductType__c}" />
<br></br>
<apex:inputField value="{!Organizers__c.Status__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="cancel"/>

 <apex:commandButton action="{!doSaveAndNew}" value="Save & New"/>


</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

hope that helps if yes mark it as soltion.

This was selected as the best answer