1

I am tasked with creating a list button on the Account object for the related opportunities list. The record that needs to be created needs to have other default values that are found in one of the opportunities within the list. Here is some documentation for the process I am trying to expand on.

From what I gather, I can only access fields in the account record where this button is being called.

I tried another way by using a VF page to pass selected records to the corresponding apex controller and then created the URL in apex. This works, however this is very clunky because it will first open a blank screen and then the create record window will pop up. Along with leaving the user on that blank screen if they decided to not actually create a record.

Below is my URL(Primary contact does not set a default):

{!URLFOR( 
'/lightning/o/Opportunity/new?recordTypeId=' 
+ $Setup.namespace__Opportunity_Custom_setting__c.namespace__CO_Record_Type_ID__c 
+ '&defaultFieldValues=Name=' + URLENCODE(Account.Name & ' Opportunity') 
+ ',AccountId=' + Account.Id 
+ ',StageName=Qualification' 
+ ',CloseDate=' + TEXT(TODAY()+30)
+ ',' + $Setup.namespace__Opportunity_Custom_setting__c.namespace__CDF_Primary_ContactId__c 
+ '=' +     Opportunity.namespace__Primary_ContactId__c  

)}

Below is my VF attempt:

<apex:page standardController="Opportunity" recordSetVar="opportunities" extensions="ChangeOrderOppHelper" action="{!getRedirect}"></apex:page>

Below is the corresponding Controller:

public with sharing class ChangeOrderOppHelper {
private final ApexPages.StandardSetController stdSetCtrl;

// Constructor accepting a StandardSetController
public ChangeOrderOppHelper(ApexPages.StandardSetController stdSetController) {
    this.stdSetCtrl = stdSetController;
}

public PageReference getRedirect() {

    Profile sysAdminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
    Opportunity_Change_Order_Type__c adminSetting = Opportunity_Change_Order_Type__c.getInstance(sysAdminProfile.Id);


    List<SObject> selectedSObjects = stdSetCtrl.getSelected();
    if(selectedSObjects.size() > 0) {
        Opportunity selectedOpportunity = (Opportunity) selectedSObjects[0];
        selectedOpportunity = [SELECT Id, AccountId, namespace__Related_Order__c, namespace__Billing_AccountId__c, namespace__Primary_ContactId__c 
        FROM Opportunity WHERE Id = :selectedOpportunity.Id LIMIT 1];

            // Construct the URL
            String baseURL = '/lightning/o/Opportunity/new';
            
            String params = '?recordTypeId=' + adminSetting.namespace__CO_Record_Type_ID__c;
            params += '&defaultFieldValues=';
            params += 'AccountId=' + selectedOpportunity.AccountId;
            params += ',StageName=Issued Lead';
            params += ',Name=x';
            //params += ',' + adminSetting.namespace__CDF_Related_Order__c + '=' + selectedOpportunity.namespace__Related_Order__c;
            params += ',' + adminSetting.namespace__CDF_Billing_AccountId__c + '=' + selectedOpportunity.namespace__Billing_AccountId__c;
            params += ',' + adminSetting.namespace__CDF_Primary_ContactId__c + '=' + selectedOpportunity.namespace__Primary_ContactId__c;
            params += ',ForecastCategoryName=Pipeline';
            params += ',CloseDate=' + String.valueOf(Date.today());

            return new PageReference(baseURL + params);
    } else {
        return new PageReference(ApexPages.currentPage().getHeaders().get('Referer'));

    }
}

} enter image description here

0

You must log in to answer this question.