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
Juan Antonio CantareroJuan Antonio Cantarero 

Change a text custom field value from a VisualForce page

Dear. 
This seems an easy question with easy solution, but I don't find a technical solution after look in google, developer salesforce, etc.

This is what I want: I need to perform a set of validations in a lot of custom field values of the Opportunity. After see different options (process builder, validation rules, formulas, etc) the best approach if to use a VisualForce page. In that VisualForce page I want to show a text custom field, with a custom error message that can change via visualforce/javascript code.

This is my issue: I am not able to change the text custom field value. I have no erros. Just no changes on the text value. Below is the code I used in the VisualForce page.

Do you have any suggestion?
 
<apex:page standardcontroller="Opportunity">

    <apex:form id="formId">
        <apex:pageblock id="pb">
        
            <apex:pageBlockSection id="Block1" title="Opportunity Information" columns="2">
        
                <apex:inputField id="Warning_message" value="{!Opportunity.Warning_message__c}" />
                
            </apex:pageBlockSection>

        
        </apex:pageblock>
    </apex:form>
               
           
    <script type="text/javascript">
    
        function myFunction() 
        {
            document.getElementById('{!Opportunity.Warning_message__c}').value = "This will not change the value";
            document.getElementById('{!Opportunity.Warning_message__c}').innerHTML= "This will not change the value";
            document.getElementById('Warning_message').value = "This will not change the value";
            document.getElementById('Warning_message').innerHTML= "This will not change the value";            
            document.getElementById('{!$Component.formId.pb.Block1.Warning_message}').value = "This will not change the value";            
        }    
                            
        var msgObj= document.getElementById('{!Opportunity.Warning_message__c}');
        msgObj.value = "This will not change the value";            
        msgObj.innerHTML= "This will not change the value";            

    </script>
    
</apex:page>

Many thanks.
 
Best Answer chosen by Juan Antonio Cantarero
Dushyant SonwarDushyant Sonwar
Change this line
<apex:inputField id="Warning_message" value="{!Opportunity.Warning_message__c}" />

to
<apex:inputField id="Warning_message" styleClass="warningTxtBoxCls" value="{!Opportunity.Warning_message__c}" />

Change Script
<script>
window.onload = function() {
  document.getElementsByClassName('warningTxtBoxCls')[0].value = 'This is definately changing the value';
};
</script>

This will work definately... enjoy :)
 

All Answers

Dushyant SonwarDushyant Sonwar
Change this line
<apex:inputField id="Warning_message" value="{!Opportunity.Warning_message__c}" />

to
<apex:inputField id="Warning_message" styleClass="warningTxtBoxCls" value="{!Opportunity.Warning_message__c}" />

Change Script
<script>
window.onload = function() {
  document.getElementsByClassName('warningTxtBoxCls')[0].value = 'This is definately changing the value';
};
</script>

This will work definately... enjoy :)
 
This was selected as the best answer
Juan Antonio CantareroJuan Antonio Cantarero
Great!. It worked. Many Thanks!
hUM rABRIhUM rABRI
Vertical Shores (https://civiljungle.com/shoring-in-construction/) are the lightest and most effortless way of ensuring your uncovering. They are intended to be introduced or taken out (ordinarily by one individual) from the highest point of your channel. Vertical Shores apply water driven strain to the channel dividers for help and to forestall collapses