+ Start a Discussion
Shanke_PaudelShanke_Paudel 

add contact with custom page and controller

I have an custom page that shows come contacts with inline edit option.

 

I also have another apex:pageBlock 

that contains add new contact fields. the contact can be added successfuly.

 

But the problems are below.

1)If user clicks on any navigation link.. error comes lastname cannot be empty i know it is required.. but what if user do not want to add another contact.

2)I want to show and hide this on user click pageBlock.. i have added Jquery but i am not able to find a suitable selector for ti.

 

Here is the controller.

public class approvestep2companycontact{

public PageReference saveNew() {
newContact.AccountId = opportunity.Account.Id;
insert newContact;

PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
pageRef.setRedirect(true);
return pageRef;
}

public PageReference back(){
PageReference oPageRef = Page.ApproveStep1CompanyInfo;
oPageRef .getParameters().put('id',opportunity.id );
oPageRef.setRedirect(false);
return oPageRef;
}

public PageReference save() {
update contacts;
return null;
}
public PageReference approve() {
update contacts;
PageReference oPageRef = Page.ApproveStep2CompanyContact;
oPageRef .getParameters().put('id',opportunity.id );
oPageRef.setRedirect(false);
return oPageRef;
}

public PageReference edit() {
return null;
}

Public List<Contact> contacts {get;set;}
private final Opportunity opportunity;
private final Contact newContact;

public approvestep2companycontact() {
opportunity = [SELECT Id,Account.Id, Name FROM Opportunity WHERE Id = :ApexPages.currentPage().getParameters().get('id') ];
contacts = [SELECT LastName, FirstName FROM contact WHERE AccountId = :opportunity.Account.Id ];
newContact = new Contact();
}

public Contact getNewContact(){
return newContact;
}

public List<Contact> getContacts () {
return contacts ;
}
}

 

Here is the vf page.

<apex:page controller="approvestep2companycontact" >
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" />
<apex:form >
<apex:pageBlock title="Company Contacts" mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" id="saveButton" value="Save"/>
<apex:commandButton action="{!approve}" id="approveButton" value="Approve"/>
<apex:commandButton action="{!back}" id="bcakButton" value="Back"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockTable value="{!contacts }" var="item">
<apex:column headerValue="Last Name">
<apex:outputField value="{!item.LastName}"/>
</apex:column>
<apex:column headerValue="First Name">
<apex:outputField value="{!item.FirstName}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Add New Contact" id="pbNewContact">
<apex:pageBlockButtons >
<apex:commandButton action="{!saveNew}" id="saveButton" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:inputField value="{!newContact.Firstname}"/>
<apex:inputField value="{!newContact.Lastname}"/>
<apex:inputField value="{!newContact.Title}"/>
<apex:inputField value="{!newContact.Email}"/>
<apex:inputField value="{!newContact.Fax}"/>
<apex:inputField value="{!newContact.Phone}"/>
<apex:inputField value="{!newContact.OtherPhone}"/>
</apex:pageBlockSection>
</apex:pageBlock>

</apex:form>
<script type="text/javascript">
j$ = jQuery.noConflict();
j$(document).ready(function() {
j$("#j_id0:j_id2:pbNewContact")).hide();
});

</script>
</apex:page>

Subramani_SFDCSubramani_SFDC

Hi,

 

 

Use immediate="true" for bypassing the Validation.

 

For showing/hiding pageblock following url will help u..

 

 

http://boards.developerforce.com/t5/Visualforce-Development/Show-Hide-pageblock-on-button-click/td-p/98847