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
Alessandro Guarnieri 4Alessandro Guarnieri 4 

Save button on a visualforce page doesn't work if workflow rule is active

Hi, 

we made a visualforce page to create new accounts. We wanted the page NOT to have a field where to insert the "account name" because we want it to be automatically populated after we press the save button, using a workflow rule.

Now, if we create and activate a workflow rule to autopopulate the Account Name then we press save, the new account is not created and the page simply refresh. While if we use a trigger everything works fine. We'd like to use a workflow though, becuase it's easier to be mantained and transfered.

Here the vf page:

<apex:page standardController="Account" showHeader="true" sidebar="true">
    <apex:form id="myForm">
        <script>
            function copyAddress() {
                var shippingStreet = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id58:actShippingStreet');
                var shippingCity = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id64:actShippingCity');
                var shippingState = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id70:actShippingState');
                var shippingCountry = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id82:actShippingCountry');
                var shippingPostalCode = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id76:actShippingPostalCode');

                var BillingStreet = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id55:actBillingStreet');
                var BillingCity = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id61:actBillingCity');
                var BillingState = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id67:actBillingState');
                var BillingCountry = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id79:actBillingCountry');
                var BillingPostalCode = document.getElementById('j_id0:myForm:pgBlock:pgBlockSectionAddressInfo:j_id73:actBillingPostalCode');

                shippingStreet.value = BillingStreet.value;
                shippingCity.value = BillingCity.value;
                shippingState.value = BillingState.value;
                shippingCountry.value = BillingCountry.value;
                shippingPostalCode.value = BillingPostalCode.value;

            }
        </script>
        <apex:sectionHeader title="Account Edit" subtitle="{!Account.Name}" />    
        <apex:pageBlock id="pgBlock" mode="edit" title="Account Edit">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
        <apex:pageBlockSection id="pgBlockSectionAcctInfo" title="Account Information" collapsible="false" columns="2" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Owner</apex:outputLabel>
                <apex:outputField id="actOwner" value="{!account.ownerid}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Rating</apex:outputLabel>
                <apex:inputField id="actRating" value="{!account.rating}" />
            </apex:pageBlockSectionItem>
            
            <!--<apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}" required="true" />
            </apex:pageBlockSectionItem>-->
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Phone</apex:outputLabel>
                <apex:inputField id="actPhone" value="{!account.Phone}" />
            </apex:pageBlockSectionItem>    
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Parent Account</apex:outputLabel>
                <apex:inputField id="actParentAccount" value="{!account.ParentId}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Fax</apex:outputLabel>
                <apex:inputField id="actFax" value="{!account.Fax}" />
            </apex:pageBlockSectionItem>     
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Number</apex:outputLabel>
                <apex:inputField id="actNumber" value="{!account.Accountnumber}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Website</apex:outputLabel>
                <apex:inputField id="actWebsite" value="{!account.Website}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Site</apex:outputLabel>
                <apex:inputField id="actSite" value="{!account.Site}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Ticker Symbol</apex:outputLabel>
                <apex:inputField id="actTicker" value="{!account.TickerSymbol}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Type</apex:outputLabel>
                <apex:inputField id="actType" value="{!account.Type}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Ownership</apex:outputLabel>
                <apex:inputField id="actOwnership" value="{!account.Ownership}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Industry</apex:outputLabel>
                <apex:inputField id="actIndustry" value="{!account.Industry}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Employees</apex:outputLabel>
                <apex:inputField id="actEmployees" value="{!account.NumberOfEmployees}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Annual Revenue</apex:outputLabel>
                <apex:inputField id="actAnnualRevenue" value="{!account.AnnualRevenue}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >SIC Code</apex:outputLabel>
                <apex:inputField id="actSIC" value="{!account.Sic}" />
            </apex:pageBlockSectionItem>            
        </apex:pageBlockSection>
            
        <apex:pageBlockSection id="pgBlockSectionAddressInfo" title="Address Information" collapsible="false" columns="2">
            <apex:facet name="header">
                    <span class="pbSubExtra">
                        <span class="bodySmall">
                            <a href="javascript:copyAddress();">Copy Billing Address to Shipping Address</a>
                        </span>
                    </span>
                    <h3>Address Information<span class="titleSeparatingColon">:</span></h3>
            </apex:facet>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Billing Street</apex:outputLabel>
                <apex:inputField id="actBillingStreet" value="{!account.BillingStreet}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Shipping Street</apex:outputLabel>
                <apex:inputField id="actShippingStreet" value="{!account.ShippingStreet}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Billing City</apex:outputLabel>
                <apex:inputField id="actBillingCity" value="{!account.BillingCity}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Shipping City</apex:outputLabel>
                <apex:inputField id="actShippingCity" value="{!account.ShippingCity}" />
            </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Billing State/Province</apex:outputLabel>
                <apex:inputField id="actBillingState" value="{!account.BillingState}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Shipping State/Province</apex:outputLabel>
                <apex:inputField id="actShippingState" value="{!account.ShippingState}" />
            </apex:pageBlockSectionItem>           
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Billing Zip/Postal Code</apex:outputLabel>
                <apex:inputField id="actBillingPostalCode" value="{!account.BillingPostalCode}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Shipping Zip/Postal Code</apex:outputLabel>
                <apex:inputField id="actShippingPostalCode" value="{!account.ShippingPostalCode}" />
            </apex:pageBlockSectionItem>    
            
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Billing Country</apex:outputLabel>
                <apex:inputField id="actBillingCountry" value="{!account.BillingCountry}" />
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Shipping Country</apex:outputLabel>
                <apex:inputField id="actShippingCountry" value="{!account.ShippingCountry}" />
            </apex:pageBlockSectionItem>
            
        </apex:pageBlockSection>
        <apex:pageBlockSection id="pgBlockSectionAdditionalInformation" title="Additional Information" collapsible="false" columns="2">
            <!--ANY CUSTOM FIELDS / ADDITIONAL INFORMATION CAN GO HERE-->
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>
Here the apex trigger:
trigger UpdateNameOact on Account (before insert, before update) {
    
    for (Account act : Trigger.New) {
    
        act.Name = act.Phone + ' ' + act.Website;
        
    }

}

Here the workflow rule:
User-added image
User-added image
Nagendra ChinchinadaNagendra Chinchinada
Hi,
For the Account Name is required field, and you can't save the Account record without giving Name. As you hidden Name field from the Page, while saving it is throwing ' Required fields missing : Name ' error and is unable to show the error on the page, and reloading the page.
So, show the Name field on the Page, let user enter it, anyhow Workflow will populate the correct name as u expected.

Please add <apex:pagemessages /> in line no 2, so that it will show the error messages(if any) without refreshing the page.
 
Alessandro Guarnieri 4Alessandro Guarnieri 4
Thanks for your reply Ch Nagendra ,

the thing is, if i use the trigger, there are no errors and i can create the new account with the autopopulated name. So we were wondering why we can't do it with a workflow rule.
Nagendra ChinchinadaNagendra Chinchinada
Hi Alessandro,

Error will be thrown while inserting record into database since Name field is null. Here is the order of execution for this scenario.
  1. Before Trigger 
  2. Record insert into database
  3. After trigger
  4. Workflow rule

Scenario 1: Populate Name in Before trigger.
Before Trigger fires before inserting record into database(as it's name indicates). So Name is beeing populated before inserting, and hence there is no error.

Scenario 2: Populate Name in workflow.
Workflow fires after insert.So while insering record into database still Name field is null (because Workflow is not fired yet), So error will be thrown in this case.

Refer this link for more info on order of execution,
http://www.asagarwal.com/2362/order-of-execution-rules-triggers-etc-in-salesforce-debug-log

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Please let me know if it clears ur doubt. Please select it as best answer if it clears your doubt, so it will be helpfull for the others who are searching for same topis.