0

I've been struggling for a while this one and am hoping someone can help. I need to create a Log a Call list button that I can display on Account Activity History Related List. Goal of this button is to create a new record and pre-populate the Status (Completed), Priority (Normal), What Id (Account.Id), Due Date (today), Type (Call), and Subject (Call) fields. Here is the apex and vf page (incomplete but does not generate expected results either way). Any help would be greatly appreciated:

public with sharing class LogACallListButton1 {

public Task task {get; set;}

public LogACallListButton1(ApexPages.StandardController controller) {
        this.task = (Task)controller.getRecord();
        this.task.whatId = ApexPages.currentPage().getParameters().get('what_id');    
        this.task.subject = 'Call';    
        this.task.type = 'Call';
        this.task.status = 'Completed';
        this.task.priority = 'Normal';
        this.task.activitydate = Date.today();
}

}

<apex:page standardController="Task" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Subject</apex:outputLabel>
<apex:inputField id="Subject" value="{!task.Subject}"/>
 </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Comments</apex:outputLabel>
<apex:inputField id="Description" value="{!task.Description}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
0

I would advice to put the button on the Activity tab for the Account instead of the Activity History related list. This way you can easily create a Quick Action for the Log a Call task with pre-filled values with no code.

However, if you still want to go the Activity History way you could modify your code as follows:

public with sharing class LogACallListButton1 {

    public Task task {get; set;}

    public LogACallListButton1(ApexPages.StandardSetController controller) {
            this.task = new Task();
            this.task.whatId = ApexPages.currentPage().getParameters().get('id');
            this.task.subject = 'Call';    
            this.task.type = 'Call';
            this.task.status = 'Completed';
            this.task.priority = 'Normal';
            this.task.activitydate = Date.today();
    }

    public PageReference logCall() {
        insert task;
        return new PageReference('/' + task.whatId);
    }
}

And the page (adding all missing input fields):

<apex:page standardController="Task" recordSetVar="Tasks" extensions="LogACallListButton1">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Subject</apex:outputLabel>
<apex:inputField id="Subject" value="{!task.Subject}"/>
 </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Comments</apex:outputLabel>
<apex:inputField id="Description" value="{!task.Description}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!LogCall}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Note the "recordSetVar" in Visualforce and the StandardSetController in Apex. This is what you do when you need a VF to be available as a List button.

0

If the button can be placed on the detail page then a detail page button with below url format can be done

'/lightning/o/Task/new?defaultFieldValues=Status=’ Completed’,Priority=’Normal’,WhatId={!Account.Id},Activitydate={!today()},Type=(Call),Subject=’call’

If the button should be placed on related list view then create a new component with url accessible to which redirection should be done can be created

https://developer.salesforce.com/docs/component-library/bundle/lightning:isUrlAddressable/documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.