+ Start a Discussion
Bramha1Bramha1 

Swap Fields DataType in VF Page

Hi,

 

   I have a problem where i need to swap the field type in VF Page. The senario is as below:

 

 1) I have a contact Creation Page where i have a Lookup for Account. If i have a matching account present i will create a contact wiht that Acocunt.  

2) If i dont have the Account I need to create the Account first and then create the contact on the same page. 

 

For first case i will be having the Account Filed as Id(since it is a lookup). I am not able to create an account with this as the second case requires the field to be a String to create the account.

 

Note: I am calling an Ajax Call( using an apex:actionSupport on the account field on onchange event) to get some other information and display.

 

 

Is there any way to tackel with this?

 

Cheers

 

Bramha

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Ok now I understand your requirement,

Here is I developed a smaller version of your req, just use it for your issue as well

 

VFP : 

 

<apex:page controller="contactController" id="pg">
      <script>
          function showLookup(ctrlID,objKeyPrefix)
                {
                  openLookup("/_ui/common/data/LookupPage?lkfm=editPage&lknm="+ ctrlID +"&lktp="+objKeyPrefix);           
                }
      </script>
      
      <apex:form id="frm">
          <apex:pageMessages ></apex:pageMessages>
          <apex:pageBlock id="pb">
              <apex:pageblockButtons >
                  <apex:commandButton value="Save" action="{!saveContact}"/>
              </apex:pageblockButtons>
              <apex:pageBlockSection id="pbs">
                  <apex:inputField value="{!con.lastName}"/>
                  <apex:pageBlockSectionItem id="pbsiAccountID" HelpText="{!$ObjectType.Contact.fields.ACCOUNTID.inlineHelpText}">
                    {!$ObjectType.Contact.fields.ACCOUNTID.label}
                    <apex:OutPutPanel id="opAccountID">
                        <apex:inputText id="vField_SupplierName" value="{!SupplierName}" style="width:150px"/>
                        <apex:inputHidden id="vField_SupplierName_lkid" value="{!AccountID}"/>
                        <apex:inputHidden id="vField_SupplierName_lkold" value="{!SupplierNameOld}"/>
                        
                        <apex:image url="/s.gif" alt="Lookup (New Window)" styleClass="lookupIcon" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" onclick="javascript&colon;showLookup('pg:frm:pb:pbs:pbsiAccountID:vField_SupplierName','001')" title="Lookup (New Window)"/>
                    </apex:OutPutPanel>
                </apex:pageBlockSectionItem>
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
</apex:page>

 Controller : 

 

public class contactController {

    public Account acc {get;set;}
    public Contact con {get;set;}
    
    public string supplierName{get;set;}
    public string supplierNameOld{get;set;}
    public string AccountID{get;set;}
    public contactController()
        {
            acc = new Account();
            con = new Contact();
            
        }
        
    public PageReference saveContact()
        {
            try
                {
                   
                    
                    
                    if(AccountID == null || AccountID.trim().length() == 0)
                        {
                            acc.Name = supplierName;
                            insert acc;
                            con.accountid = acc.id;
                        }
                    else
                        {
                            con.accountid = AccountID;
                        }    
                    
                    insert con;
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();    
                        
        }


}

 Use this example and use it for your requirement, I hope it helps you.

All Answers

Shashikant SharmaShashikant Sharma

I hope your lookUp for Account is not required on the page.

 

What you should do is that you should ahve a save action of contact

 

if(contact.accountId == null)

{

Account a = new Account(Name = contact.lastName;

insert a;

contact.accountId = a.id;

}

 

insert contact;

 

try to do like above code, just given you a way

Bramha1Bramha1

Hi,

 

   I need the lookup as well, because we need clients to select the account using the lookup as well. If they type an account name and TAB is pressed(means a onchange event occurs) , i am retrieving some address(from Address__c) on the onchange event. So, i need a lookup for the clients user friendly. Some thing like if the account exists it should be a lookup field, else it should behave as a normal input text field( Name field for account).

 

 

 

Thanks

 

Bramha

Shashikant SharmaShashikant Sharma

Ok now I understand your requirement,

Here is I developed a smaller version of your req, just use it for your issue as well

 

VFP : 

 

<apex:page controller="contactController" id="pg">
      <script>
          function showLookup(ctrlID,objKeyPrefix)
                {
                  openLookup("/_ui/common/data/LookupPage?lkfm=editPage&lknm="+ ctrlID +"&lktp="+objKeyPrefix);           
                }
      </script>
      
      <apex:form id="frm">
          <apex:pageMessages ></apex:pageMessages>
          <apex:pageBlock id="pb">
              <apex:pageblockButtons >
                  <apex:commandButton value="Save" action="{!saveContact}"/>
              </apex:pageblockButtons>
              <apex:pageBlockSection id="pbs">
                  <apex:inputField value="{!con.lastName}"/>
                  <apex:pageBlockSectionItem id="pbsiAccountID" HelpText="{!$ObjectType.Contact.fields.ACCOUNTID.inlineHelpText}">
                    {!$ObjectType.Contact.fields.ACCOUNTID.label}
                    <apex:OutPutPanel id="opAccountID">
                        <apex:inputText id="vField_SupplierName" value="{!SupplierName}" style="width:150px"/>
                        <apex:inputHidden id="vField_SupplierName_lkid" value="{!AccountID}"/>
                        <apex:inputHidden id="vField_SupplierName_lkold" value="{!SupplierNameOld}"/>
                        
                        <apex:image url="/s.gif" alt="Lookup (New Window)" styleClass="lookupIcon" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" onclick="javascript&colon;showLookup('pg:frm:pb:pbs:pbsiAccountID:vField_SupplierName','001')" title="Lookup (New Window)"/>
                    </apex:OutPutPanel>
                </apex:pageBlockSectionItem>
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
</apex:page>

 Controller : 

 

public class contactController {

    public Account acc {get;set;}
    public Contact con {get;set;}
    
    public string supplierName{get;set;}
    public string supplierNameOld{get;set;}
    public string AccountID{get;set;}
    public contactController()
        {
            acc = new Account();
            con = new Contact();
            
        }
        
    public PageReference saveContact()
        {
            try
                {
                   
                    
                    
                    if(AccountID == null || AccountID.trim().length() == 0)
                        {
                            acc.Name = supplierName;
                            insert acc;
                            con.accountid = acc.id;
                        }
                    else
                        {
                            con.accountid = AccountID;
                        }    
                    
                    insert con;
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();    
                        
        }


}

 Use this example and use it for your requirement, I hope it helps you.

This was selected as the best answer
Bramha1Bramha1

Hi,

 

  can you explain me the code in VF Page where we have:

 

   Thanks

 

Bramha

 



<apex:inputText id="vField_SupplierName" value="{!SupplierName}" style="width:150px"/>
                        <apex:inputHidden id="vField_SupplierName_lkid" value="{!AccountID}"/>
                        <apex:inputHidden id="vField_SupplierName_lkold" value="{!SupplierNameOld}"/> 
I am not sure why we are using these many hidden values. And in what cases which hidden field is used? How can we differntiate them that  in this case this value is to be used. Like i am get only one among AccountID or SupplerName or SuppliernameOld to have a value and others are null.  If you could explain me the case then that would be brilliant.
Shashikant SharmaShashikant Sharma

Any lookup will have two parts 

1) Text  : This is the text in the text box , this is the name that apperas when you select any value from the lookup window, we are binding it with controller property SupplierName

2)ID : The lookup will save a reference to the selected item, this id is not visible to the user but we just keep it in backgroundion 

 

 

The Old id is not of any use for you, it comes in use when you need to perform any action on change of .lookup value.

 

Reason for using vField prefix and lkid suffix is that on a standard lookup page salesforce has these field and openLookup page copies selected values to thesee fields. If we will not use these prefix then javascript error will occur.

 

 

 

I hope above explanation will help you.

Bramha1Bramha1

Hi,

 

  Thanks, but the code create duplicate acocunts. Example if we already have an Account TestAcocunt1 and then when create a new contact with Account as TestAccount1, then TestAccount1 is being created again.

 

 

Cheers

 

Bramha

Bramha1Bramha1

Hi,

 

  Could you let me know why we are using 001 in the javascript fucntion here:

 

javascript&colon;showLookup('pg:theform:thePageBlock:pageSection:pbsiAccountID:vField_SupplierName','001')"

 

 

Cheers

Bramha

 

LakshmanLakshman

001 specifies that the record is for Account i.e. 001 is prefix for Account.

LakshmanLakshman

For more information check here:

http://www.wohill.com/standard-salesforce-object-prefixes/

 

Regards,

Lakshman

Bramha1Bramha1

Thanks Guys, 

 

  That was helpful, i made little modifications to tackle with duplicate account creation.

 

 

Thanks

Bramha