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
Arvind1Arvind1 

Problem in displaying field values as blank after saving

I am displaying the Account detail page with related Contacts as a datatable. I've placed a new button there.
Onclicking that it goes to a page where new contact can be created. The Account name in that contact pg will be read only.
Since i'm passing the account id from the Acount pg to the new contact page, it'l giv an error if the create new contact pg is directly accessed.
The contact page has 3 buttons 'save', 'cancel' and 'save and new'. Onclicking save and new it should save the record and
return the same page with all fields blank.The prob is it is saving the record but when returning the page its displaying the previously entered
details.

This is the controller i'm using.
public class Contact5 {
Contact con;
    public Contact getContact() {
   
    if(con==null)   
    con=new Contact();
    return con;
}
public PageReference save() {
con.AccountId=cinfo.id;
insert con;
return page.SPPManageAccount;
}
public PageReference cancel() {
return page.SPPManageAccount;
}
ID id=System.currentPageReference().getParameters().get('id2');
  account cinfo;
    public account getcinfo() {
  cinfo=[select id,name from account where id=:id];
 
        return cinfo;
    }
public PageReference new1() {
con.AccountId=cinfo.id;
insert con;
return page.SPPNewContact;
}
}
<apex:commandbutton value="Save and New" action="{!new1}"/>

Any solution on this would be of a  great help.
Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler
Code:
newContact.setRedirect(true);
newContact.getParameters().put('id', id);
return newContact;

 where newContact is your PageReference.

Make sure you save off the id in your constructor so you can use it in your action method.

All Answers

Ron HessRon Hess
i think if you setRedirect(true)  on the page before returning it, the viewstate will be cleared.

try this

PageReference newcontact = page.SPPNewContact;
newcontact.setRedirect(true);

return newcontact;
Arvind1Arvind1
Hi Ron, thanks for the reply. If i use setredirect attribute it is saving the record but its showing the following error.

'System.QueryException: List has no rows for assignment to SObject '
Class.Contact5.getcinfo:cinfo=[select id,name from account where id=:id];
External entry point
Arvind1Arvind1
I'm facing one more prob in the Account detail pg where contacts related list is displayed as a table.
i've created a column for delete which is displayed as a link. onclicking 'del' it should delete the respective contact.
Onclicking the contact is not getting deleted and its showing the following error.
System.QueryException: List has no rows for assignment to SObject
Class.Controller4.Delete2: d=[select FirstName,LastName,Phone,Email,Title,MobilePhone,Fax,MailingStreet,MailingCity,
MailingCountry,MailingState,MailingPostalCode from Contact where id=:id];
External entry point

This is the complete code i'm using

public class Controller4 {
Account cinfo;
Contact c;
Contact d;
public PageReference new1() {
//ID id1=System.currentPageReference().getParameters().get('selectedCategoryId');
//c=[select name, SPA_Name__c, Distributor_Name__c,Reseller_Account__c,Status__c,SPA_Type__c,Effective_Date__c from SPA_Detail__c where id=:id1];
ID id1=cinfo.id;
PageReference p= page.SPPNewContact;
p.getParameters().put('id2',id1);
system.debug('User:'+id1);
p.setRedirect(true);
return p;
}
public Account getcinfo() {
cinfo = [select id,name,OwnerId,Region_Code__c,Phone,Fax,Website,BillingCity,BillingCountry,BillingState,
BillingPostalCode,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,(select Name,Phone,Email from Contacts) from Account where OwnerId = :Userinfo.getUserId()];
return cinfo;
}
public PageReference Edit() {
ID id3=System.currentPageReference().getParameters().get('selectedId');
c=[select FirstName,LastName,Phone,Email from Contact where id=:id3];
/*Copy_spa__c spacopy=new Copy_spa__c();
spacopy.name=c.name;
spacopy.spaname__c=c.Distributor_Name__c;
insert spacopy;*/
PageReference p= page.SPPEditContact;
p.getParameters().put('id4',c.id);

p.setRedirect(true);
return p;
//return null;
}
public PageReference Delete2(){
ID id=System.currentPageReference().getParameters().get('selectedId');
d=[select FirstName,LastName,Phone,Email,Title,MobilePhone,Fax,MailingStreet,MailingCity,
MailingCountry,MailingState,MailingPostalCode from Contact where id=:id];

delete d;
PageReference p=page.SPPManageAccount;
p.setRedirect(true);
return p;
}
}

<apex:commandLink id="DeleteContact" value="Del" action="{!Delete2}">
 <apex:param name="selectedcategoryId" value="{!tact.id}"></apex:param>
 </apex:commandLink>
jwetzlerjwetzler
Please use the SRC button when posting your code to this board.

That exception means your query didn't return anything, which makes sense because you didn't pass an id to it.  If you are trying to redirect to a page that's doing a query based off of an id passed in, then you have to pass the id in as a parameter.
Arvind1Arvind1
Is it possible to pass the id from a page to itself?
Once i click 'Save and New' button, it should save the record and should
display the same page with blank field values. If i use setredirect attribute it is showing that error.
If i directly return the page by giving page.pagename its returning the page, but the previously entered values are being displayed.
I want that that to be cleared.
jwetzlerjwetzler
Code:
newContact.setRedirect(true);
newContact.getParameters().put('id', id);
return newContact;

 where newContact is your PageReference.

Make sure you save off the id in your constructor so you can use it in your action method.
This was selected as the best answer
Arvind1Arvind1
Thaks Jill. Its working fine now..
GL_NSGL_NS
Hi ,
 
Is there any way by which we can first validate the ID passed in the QueryString and then passing it on to the Query.
My page contains ID in queryString.For testing purpose I replaced the ID with other string 32432432 and I am getting error:
System.StringException: Invalid id: 32432432.

Pl suggest ..can we validate this ID and instead of getting this error if we could throw Exception.

Thanks.