Skip to main content
added 517 characters in body
Source Link
sfdcfox
  • 501.8k
  • 21
  • 473
  • 828

You can use apex:actionSupport to listen for changes and respond accordingly. For example:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr">
    <apex:actionsupport event="onchange" action="{!validateandupdate}" reRender="form" />
</apex:inputfield>

If you want to do client-side validation, you can also use apex:inputField's onchange attribute to support listening for changes to the lookup field.


Edit: The action attribute is used to determine the function to call. So, in this example, the function is named validateAndUpdate, which would look like this:

public void validateAndUpdate() {
    if(...) { // Some validations
        ApexPages.addMessage(...);
    } else {
        update someRecords;
    }
}

Appropriate error handling should also be part of such a function to avoid partial transactions by way of Database.setSavePoint and Database.rollback.

You can use apex:actionSupport to listen for changes and respond accordingly. For example:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr">
    <apex:actionsupport event="onchange" action="{!validateandupdate}" reRender="form" />
</apex:inputfield>

If you want to do client-side validation, you can also use apex:inputField's onchange attribute to support listening for changes to the lookup field.

You can use apex:actionSupport to listen for changes and respond accordingly. For example:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr">
    <apex:actionsupport event="onchange" action="{!validateandupdate}" reRender="form" />
</apex:inputfield>

If you want to do client-side validation, you can also use apex:inputField's onchange attribute to support listening for changes to the lookup field.


Edit: The action attribute is used to determine the function to call. So, in this example, the function is named validateAndUpdate, which would look like this:

public void validateAndUpdate() {
    if(...) { // Some validations
        ApexPages.addMessage(...);
    } else {
        update someRecords;
    }
}

Appropriate error handling should also be part of such a function to avoid partial transactions by way of Database.setSavePoint and Database.rollback.

Source Link
sfdcfox
  • 501.8k
  • 21
  • 473
  • 828

You can use apex:actionSupport to listen for changes and respond accordingly. For example:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr">
    <apex:actionsupport event="onchange" action="{!validateandupdate}" reRender="form" />
</apex:inputfield>

If you want to do client-side validation, you can also use apex:inputField's onchange attribute to support listening for changes to the lookup field.