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
SuAkSuAk 

Need to update a value based on the picklist value before saving the record

Hi, 

 I have a requirement where on the EDIT Page(the edit page when we click on New on the object), When User select a value on the picklist to "Bill To" the field in the Location Name should be "Bill To Address". This needs to be pre populated before saving the record.

User-added image

As per the picture above, the location name should be updated in this screen to "Bill to Address" as soon as the user select the Type "Bill To" and user should be allowed to overwrite the Location Name "Bill to Address" to something and save the record.

Let me know if there are any solution to this.

Thanks,
Sujatha.M
krishnak2krishnak2
Hi Sujatha,

Fields cannot be dynamically pre-pulated via standard functionality. Using csutom code, this can be achieved in 2 ways.
  1. Override the New Button and create a custom visualforce edit page. You can have javascript in that page to dynallically populated the field based on other field values.Example (http://www.oyecode.com/2013/04/how-to-prepopulate-text-box-field-on.html)
   2.Not 100 % percent suer abt this but you could  create a homepage component  with a snippet of Jquery code to populate values selected in other fields.

 
Suraj TripathiSuraj Tripathi
Hi sujatha,

As per your requirement i have written a trigger hope it solves your problem.

trigger
trigger loct_test on Location__c (before insert) {
if(trigger.isbefore && trigger.isinsert)
{
    dev_test_7_nov.m1(trigger.new);
}
}
Handler class
public class dev_test_7_nov {
public static void m1(list<location__c> loct)
{
    for(location__c loc_loop : loct)
    {
        if(loc_loop.Type__c=='Bill To')
        {
            loc_loop.Name='Bill to Address';
        }
    }
   }
}

Regards
Suraj
SuAkSuAk
@Krishnak2 - Thank you for your response. The code that you mentioned is when the page loads on click ing New. My requirement is when the page loads, user has the option to select Type field. When the user selects the Type = 'Bill To', the Location name should be populated with 'Bill To Address' and if the user selects type = 'Ship To' the Location name should be populated with 'Ship To Address' and if the user wants to overwrite, then user should be able to overwrite. All this should happen after loading hte page and before saving the record.

@Suraj - Thank you for your response. Trigger works only when I click on the Save button. I need this before clicking on save.
krishnak2krishnak2
Hi Sujatha,

Thats a sample  code for overriding New/Edit buttons. You have to build logic using Javascript in to the page to dynamically prepoulate the field. Somethin like below.



<apex:page standardController="Your_Object__c" extensions="Customcontroller">
<script>

function location()
{
var Type = document.getElementById('{!$Component.form1.pblock.locationobject.Type__c}').value;
var location =document.getElementById('{!$Component.form1.pblock.locationobject.Type__c}');
if (Type = 'Bill To')
{
location.value= "Bill To Address";
}
window.onload=function()
{

    location();
}
</script>
<apex:form id="form1"> 
<apex:pageBlockButtons >
    <apex:commandButton value="Save" action="{!save}"/>
    <apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
      

<apex:pageblocksection id="locationobject" columns="2" title="LOC"> 
 <!--add other fields-->
<apex:inputField id="Type__c" onchange="location();"  value="{!Your_Object__c.Type__c}"/>
 <apex:inputField id="Location_Name__c"  value="{!Your_Object__c.Location_Name__c}"/> 
</apex:pageblocksection>    

</apex:pageBlock>
    
</apex:form>    

</apex:page>

Please excuse any typos.