3

Good day,

I posted this over on the Salesforce Dev community forums but have gotten no responses. I'm hoping someone here might be able to assist.

We have a call center application integrated with Salesforce. This application requires that a "disposition" be chosen when completing a phone call. Once the disposition is selected, a task record is added in the task object for which the Call Result value is set to the value of the disposition.

Because users are able to disposition a call without ever actually opening the lead edit page, I would like to be able to redirect the user to a custom visual force page when a particular disposition is selected so that additional information can be gathered regarding that call.

Flow/Process Builder did not appear to be an option so I thought that I needed to go the route of creating an extension to the Lead controller which would then redirect the user to a custom VF page which would load a couple of values and allow the user to round out the info on the record.

I added some debug messages and found that things appear to be firing correctly when a task is created and the URL appears to be generating correctly. However, when a task is added, the redirect does not seem to fire and I'm left at the lead detail page, the VF page never loads.

Please note that my end goal here is to only have this fire when a specific value exists in the CallDisposition field of the task record and when the WhatID value of that task record is null. That will only ever take place when the scenario I am attempting to solve for occurs.

I created an after insert trigger on the Task object as shown here which calls an Apex class. Please note that i intend to do some additional validation of the WhatID and CallDisposition but have not added that yet.

    trigger RefundTaskSubmitted on Task (after insert)
{
    List<Task> TaskValues = new List<Task>();
    for (Task tsk: trigger.new)

    {
    String v_TaskID = Trigger.newMap.Get(tsk.ID).ID;
    Task TaskData = [SELECT Id, WhoID, WhatID, CallDisposition FROM Task WHERE Id IN :Trigger.new];
        if(TaskData.WhatID ==null)

        {
            String v_LeadID = TaskData.WhoID;
            vf_LeadEdit.LoadRefundPage(v_LeadID);
        }

        else {

             }
    }

}

The vf_LeadEdit class is shown here which calls the visual force page :

    public class vf_LeadEdit
{
    public final Lead ld;

    public vf_LeadEdit(ApexPages.StandardController stdController)
    {
        this.ld = (Lead)stdController.getRecord();
    }

    public static PageReference LoadRefundPage(string LeadID)

        {

        PageReference myVFPage = new PageReference('/apex/vf_LeadRefund');
        myVFPage.getParameters().put('ID', LeadID);
        myVFPage.setRedirect(true);
        return myVFPage;

        }

}

The VisualForce page code is shown as follows:

    <apex:page standardController="Lead" extensions="vf_LeadEdit">
  <apex:form >
    <apex:tabPanel >
        <apex:tab label="Enter Call/Refund Information" labelWidth="200">
           <apex:PageBlock >
                    <apex:pageblocksection columns="1">
                        <b><font color="red">Instructions:</font></b> Instructions.
                    </apex:pageblocksection>
                    <apex:pageblocksection columns="1">
                         Additional Instructions
                    </apex:pageblocksection>
                    <apex:pageblocksection columns="1">
                        <b>First Name:</b> {!Lead.FirstName} <br/>
                        <b>Last Name:</b> {!Lead.LastName} <br/>
                        <b>Lead Source:</b> {!Lead.LeadSource} <br/>
                        <br/>

                        <apex:inputField value="{!Lead.Status}" label="Selected Status"/>
                        <apex:inputField value="{!Lead.Refund_Reason__c}" label="Reason Category"/> <br/>
                        <apex:inputField value="{!Lead.Borrower_Goals__c}" label="Reason Details" style="width: 280px; height: 80px"/> <br/>
                        <apex:inputField value="{!Lead.OwnerId}"/> <br/>

               </apex:pageblocksection>
                        <div align="left" draggable="false">
                        <apex:commandButton action="{!save}" value="Save"/>
                        </div>
            </apex:PageBlock>
        </apex:tab>
      </apex:tabPanel>
    </apex:form>
</apex:page>

Update:

Initially I was encountering the following error when attempting to implement the suggestion:

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Lead.Needs_RD__c

I found that this was because the field that I was updating to reflect that the lead needed to be updated was not included as field on the VF page being used for the override.

Here is what I ended up doing:

1) Added a field (API Name: Needs_RD__c") and added it to all page layouts for the Lead Object.

2) Added an override to the View action on the Lead object (see image)

enter image description here

3) Added a VF page named leadOverride with code as:

<apex:page standardController="Lead" action="{!if(Lead.Needs_RD__c='True', URLFOR($Page.vf_LeadRefund, null, [id=Lead.Id]), 
URLFOR($Action.Lead.View,Lead.Id,null,true))}">
<apex:outputText value="{!Lead.Needs_RD__c}" rendered="false"/>
</apex:page>

Adding the Needs_RD__c field as a non rendered field on the VF override page cleared up the SOQL errors I was getting.

4) I removed the controller extension from the vf_LeadRefund page posted above.

I am using process building to toggle the Needs_RD__c field values based on whether the lead needs to be updated or not.

All in all, much further along, thanks for the guidance!

New contributor
Brandon Stacy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3

You can't redirect from a trigger. You would want to put a VF page or Lightning Component directly on the Lead record page in order to redirect to an edit page (or even your VF page). There's a few correct approaches to this, but your solution is not a valid one.

If I were you, I'd have a trigger update a field on the Lead to indicate that it needs to be updated. Then, when the Lead page displays, redirect to your custom page or the edit page when the record loads. A simple override should suffice:

<apex:page standardController="Lead" 
  action="{!if(Lead.Status='Needs Disposition',
URLFOR($Page.updateLeadDisposition, Lead.Id), 
URLFOR($Action.Lead.View,Lead.Id,null,true))}" />

Put this page in the Lead View override action (Setup > Object Manager > Leads > Buttons, Actions, and Links), and it should do as you'd like.

  • Thank you very much! It appears I overthought this considerably. I will give this a shot and will report back. – Brandon Stacy Aug 14 at 0:30
  • I was able to get this solution working (partially). I am updating my original post to include the code from the leadOverride VF page. I am still having a slight problem addressing my original problem but believe that will merit it's own topic. Thank you for the assistance! – Brandon Stacy Aug 14 at 4:13

Your Answer

Brandon Stacy is a new contributor. Be nice, and check out our Code of Conduct.

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.