• Alessandro Guarnieri 4
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hi,

is there a way to write a method in a controller extension which saves the new account i'm creating with the visualforce page below? The standard controller save action doesn't work so i need a method on the controller extension which saves the account

Here the visualforce page:
<apex:page standardController="Account" extensions="testcontroller123" recordSetVar="accounts">
   <apex:form >
       <apex:pageBlock >

       <apex:pageBlockSection >
       
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}"/>
            </apex:pageBlockSectionItem>     

           <apex:commandButton value="Save" action="{!saveRecord}"/>


       </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>

Here the extension:
public class testcontroller123 {

    public Account acct;
    
    public testcontroller123(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    
public ApexPages.PageReference saveRecord() {
  // I can do custom logic here before I save the record.
  ApexPages.StandardController controller = new ApexPages.StandardController(acct);
  try {
    controller.save();
  }
  catch(Exception e) {
    return null;
  }
  return controller.view();
}

    

}

 
Hi,

I'm trying to add the save action to a button on a visualforce page to save a new account. I'm using a standard list controller because I want the page to be available as a list button. If I click on save it doesn't work, am I missing something?

Thanks

Here is the code:
<apex:page standardController="Account" recordSetVar="accounts">
   <apex:form>
       <apex:pageBlock>

       <apex:pageBlockSection>
       
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}"/>
            </apex:pageBlockSectionItem>     
           
       		<apex:commandButton value="Save" action="{!save}"/>
           
       </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>

 

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
Hi,

is there a way to write a method in a controller extension which saves the new account i'm creating with the visualforce page below? The standard controller save action doesn't work so i need a method on the controller extension which saves the account

Here the visualforce page:
<apex:page standardController="Account" extensions="testcontroller123" recordSetVar="accounts">
   <apex:form >
       <apex:pageBlock >

       <apex:pageBlockSection >
       
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}"/>
            </apex:pageBlockSectionItem>     

           <apex:commandButton value="Save" action="{!saveRecord}"/>


       </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>

Here the extension:
public class testcontroller123 {

    public Account acct;
    
    public testcontroller123(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    
public ApexPages.PageReference saveRecord() {
  // I can do custom logic here before I save the record.
  ApexPages.StandardController controller = new ApexPages.StandardController(acct);
  try {
    controller.save();
  }
  catch(Exception e) {
    return null;
  }
  return controller.view();
}

    

}

 
Hi,

I'm trying to add the save action to a button on a visualforce page to save a new account. I'm using a standard list controller because I want the page to be available as a list button. If I click on save it doesn't work, am I missing something?

Thanks

Here is the code:
<apex:page standardController="Account" recordSetVar="accounts">
   <apex:form>
       <apex:pageBlock>

       <apex:pageBlockSection>
       
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Account Name</apex:outputLabel>
                <apex:inputField id="actName" value="{!account.name}"/>
            </apex:pageBlockSectionItem>     
           
       		<apex:commandButton value="Save" action="{!save}"/>
           
       </apex:pageBlockSection>
           
       </apex:pageBlock>           
   </apex:form>
</apex:page>

 

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