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
Admin 123Admin 123 

Visual force page?

Now I created new page having sections of Account and Task and created two buttons and now the conditions are " if you click on Save with task button it should create record in account and display in related list (Open Activities) and if you click on Save Without task button it should create Account record but not Task (Open Activities) record."

How will I achieve this functionality?
 
Best Answer chosen by Admin 123
Arvind KumarArvind Kumar
Hi Admin,

I have created VF page & controller class for you & displayed two button on custom page savewithtasksavewithouttask. When you will fill account & task information and click on savewithtask  button. Account will be created with task.
If you will fill only account information and click on savewithouttask button thenn account will be created without task.
Please use vf page & class. 

If you want to more fields for account & task information then add your fields in VF page.

Controller class:
 
public class accountController {

   
   Account account;
   Task Task;
   


 
   public Account getAccount() {
      if(account == null) account = new Account();
      return account;
   }

   public Task getTask() {
      if(Task == null) task = new Task();
      return task;
   }

  

   public PageReference savewithtask() {
   
      insert account;

      
      task.whatId = account.id;
      insert task;

  

      PageReference accPage = new ApexPages.StandardController(Account).view();
      accPage.setRedirect(true);

      return accPage;
   }
   
   public PageReference savewithouttask() {

      insert account;

      PageReference accPage = new ApexPages.StandardController(Account).view();
      accPage.setRedirect(true);

      return accPage;
   }

}

VF Page:
<apex:page controller="accountController" tabStyle="Account">
 
  <apex:sectionHeader title="New Account"/>
    <apex:form >
      <apex:pageBlock title="Account Information" mode="edit">
        <apex:pageBlockButtons >
          <apex:commandButton action="{!savewithtask}" value="savewithtask"/>
          <apex:commandButton action="{!savewithouttask}" value="savewithouttask" />
                              
        </apex:pageBlockButtons>
      <apex:pageBlockSection title="Account Information">

        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. -->
        <apex:inputField id="accountName" value="{!account.name}" required="false"/>
        <apex:inputField id="accountSite" value="{!account.site}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Task Information">
        <apex:inputField id="Subject" value="{!Task.Subject}" required="false"/>
        <apex:inputField id="Status" value="{!Task.Status}" required="false"/>
        
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

If you have any query please let me know.

If this post solves your problem kindly mark it as solution.

Thanks,
Arvind Kumar

All Answers

Arvind KumarArvind Kumar
Hi Admin,

I have created VF page & controller class for you & displayed two button on custom page savewithtasksavewithouttask. When you will fill account & task information and click on savewithtask  button. Account will be created with task.
If you will fill only account information and click on savewithouttask button thenn account will be created without task.
Please use vf page & class. 

If you want to more fields for account & task information then add your fields in VF page.

Controller class:
 
public class accountController {

   
   Account account;
   Task Task;
   


 
   public Account getAccount() {
      if(account == null) account = new Account();
      return account;
   }

   public Task getTask() {
      if(Task == null) task = new Task();
      return task;
   }

  

   public PageReference savewithtask() {
   
      insert account;

      
      task.whatId = account.id;
      insert task;

  

      PageReference accPage = new ApexPages.StandardController(Account).view();
      accPage.setRedirect(true);

      return accPage;
   }
   
   public PageReference savewithouttask() {

      insert account;

      PageReference accPage = new ApexPages.StandardController(Account).view();
      accPage.setRedirect(true);

      return accPage;
   }

}

VF Page:
<apex:page controller="accountController" tabStyle="Account">
 
  <apex:sectionHeader title="New Account"/>
    <apex:form >
      <apex:pageBlock title="Account Information" mode="edit">
        <apex:pageBlockButtons >
          <apex:commandButton action="{!savewithtask}" value="savewithtask"/>
          <apex:commandButton action="{!savewithouttask}" value="savewithouttask" />
                              
        </apex:pageBlockButtons>
      <apex:pageBlockSection title="Account Information">

        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. -->
        <apex:inputField id="accountName" value="{!account.name}" required="false"/>
        <apex:inputField id="accountSite" value="{!account.site}"/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Task Information">
        <apex:inputField id="Subject" value="{!Task.Subject}" required="false"/>
        <apex:inputField id="Status" value="{!Task.Status}" required="false"/>
        
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

If you have any query please let me know.

If this post solves your problem kindly mark it as solution.

Thanks,
Arvind Kumar
This was selected as the best answer
Admin 123Admin 123
Thank you so much! It worked..!