0

I have to create a picklist to replace a lookup to prevent my users from seeing all of the lookup records. It throws an error when it is blank when we go to edit it so I had to write it like this:

oppOptions = new List<SelectOption>();

if(wrapper.oppId != null) {
    oppOptions.add(new SelectOption(wrapper.oppId, wrapper.oppName));
    oppOptions.add(new SelectOption(‘ID here’,'MegOpp'));
    oppOptions.add(new SelectOption(‘ID here’,’TedOpp'));
} else {
    oppOptions.add(new SelectOption('','--None--'));
    oppOptions.add(new SelectOption(‘ID here’,'MegOpp'));
    oppOptions.add(new SelectOption(‘ID here’,’TedOpp'));
}

How can I make it simpler because I have more values I need to add or why is it throwing an error if my field is blank?

error: Argument 1 cannot be null

  • What error is it throwing? – gNerb May 30 '18 at 15:37
  • Argument 1 cannot be null – adming32442 May 30 '18 at 15:52
2

Creating Select Options

First you query your opps that you want the user to be able to see:

List<Opportunity> opps = new List<Opportunity>([
    SELECT Id
    FROM Opportunity
    WHERE /* What ever you need */
]);

Then, you use this list in a loop to create options:

// Add your first/default option
if (wrapper.oppId != null) {
    oppOptions.add(wrapper.oppId, wrapper.oppName);
} else {
    oppOptions.add('', '--None--');
}

for (Opportunity opp: opps ) {
    oppOptions.add(new SelectOption(opp.Id, opp.Name));
}

Add the options to your page:

<apex:selectList value="{!selectedOpp}">
    <apex:selectOptions value="{!oppOptions}" />
</apex:selectList>

Declare a place to store the selection in your page controller:

public String selectedOpp {get; set;}

Update your record

if (selectedOpp == '') {
    selectedOpp = null
}

record.field__c = (Id) selectedOpp;
update record;

Error

The "Argument cannot be null" error is provided by most built in Salesforce functionality when null is passed into a provided method. In this case, the method you're passing it into is the SelectOption constructor. It's telling you that Salesforce doesn't know what to do with a null value. You can handle this on this on the backend with a null check as you have in your code.

I would recommend, that if the field is not required, to always provide a none option and to only add the wrapper option if the ID is not null. Change my above code to the following:

// Add your first/default option
oppOptions.add('', '--None--');

if (wrapper.oppId != null) {
    oppOptions.add(wrapper.oppId, wrapper.oppName);
}

for (Opportunity opp: opps ) {
    oppOptions.add(new SelectOption(opp.Id, opp.Name));
}

If the field IS required, then simply remove the "--None--" option I have here and you should be good to go.

Update based on comments

The "default" option is simply the first option on the list as I'm sure you know. In your case, I believe you're using the wrapper to represent your default option. All you should have to do is rearrange the code I provided as follows:

// Add your first/default option
if (wrapper.oppId != null) {
    oppOptions.add(new SelectOption(wrapper.oppId, wrapper.oppName));
}

oppOptions.add('', '--None--');

for (Opportunity opp: opps ) {
    oppOptions.add(new SelectOption(opp.Id, opp.Name));
}

This will put your default at the top of the list, if it is available, followed by the "none" option. One of the things I've done in the past is added a disabled option to the list to create a separation for user friendliness:

if (wrapper.oppId != null) {
    oppOptions.add(new SelectOption(wrapper.oppId, wrapper.oppName));
    oppOptions.add(new SelectOption('','----------------'));
    oppOptions[oppOptions.size()-1].setDisabled(true);
}

This will make it look better when a default option is available and since it's disabled, user's wont be able to select it so it shouldn't cause errors or data issues. Even if it did somehow get selected, the code on the back end is already set up to handle empty string values (see the rest of my examples).

  • just added the error – adming32442 May 30 '18 at 15:53
  • Updated my answer – gNerb May 30 '18 at 15:57
  • Hmm but I also need it to default if there is an opp already. Like if an opp is already selected and they go back in to edit, then that opp should be at the top of the list. – adming32442 May 30 '18 at 16:33
  • Updated my answer – gNerb May 30 '18 at 16:41

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.