1

I'm loading the data in the dropdownlist as shown below and when the user select from dropdownlist Select I'm showing simple pageblockSection with the div but instead I'm getting this error

Invalid id: Select

Markup

<apex:pageBlockSection rendered="{!selectedRecordTypeId = 'Select'}" > 
      <div>Please select record type</div>
</apex:pageBlockSection>

<apex:pageBlockSection title="Select Record Type" columns="1"> 
 <apex:selectList id="countries" value="{!selectedRecordTypeId}" size="1" required="true">
 <apex:selectOptions value="{!RecordTypes}"/>
 </apex:selectList>    
</apex:pageBlockSection>

Apex

   public string selectedRecordTypeId {get;set;}

   public List<selectOption> getRecordTypes()
   {
    List<selectOption> rTypes = new List<selectOption>();  
    String objectAPIName = 'Case' ; //any object api
    Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
    List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
    Map<String,Id> mapofCaseRecordTypeNameandId = new Map<String,Id>();
    for(Schema.RecordTypeInfo info : recordTypeInfo)
    {
      mapofCaseRecordTypeNameandId.put(info.getName(),info.getRecordTypeId());    
      rTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
    }        
    rTypes.add(new SelectOption('Select','Select')); //<<<added
    return rTypes;
}
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
Nick
  • 7,012
  • 24
  • 131
  • 276

1 Answers1

2

You would get that error if selectedRecordTypeId had the type Id and the "Select" option was chosen because the string "Select" can't be assigned to an Id as it doesn't match the required syntax.

The solution would be to use an empty string to represent that option as that will automatically get converted to a null that can be assigned to an Id.

But with the code as posted I don't see what would cause the "Invalid id: Select" error.

Keith C
  • 135,775
  • 26
  • 201
  • 437
  • I'm presuming that "Select" was selected when the code later tried to assign the value to a RecordTypeId field; the error definitely isn't in the code posted, but it would be a side effect. – sfdcfox Mar 01 '16 at 22:23
  • I'm only getting the error if i select select so what is the solution you thinking pass empty string? – Nick Mar 01 '16 at 22:46
  • @AbuHamzah Yes use the empty string so even if the value is assigned to an Id the code should still work. – Keith C Mar 01 '16 at 22:50
  • empty string does not work, i was getting the same error so i end-up doing something like this rTypes.add(new SelectOption('000000000000000000','Select')); and its working – Nick Mar 01 '16 at 22:53
  • @AbuHamzah Weird. The "matches nothing" ID I usually use is "000000000000aaa" which you can assign in execute anonymous Id x = '000000000000aaa';. Doing Id x = '000000000000000'; produces an "Invalid Id" error for me. – Keith C Mar 01 '16 at 22:56
  • hmm weird...but its working on my end though will that bite me later? – Nick Mar 01 '16 at 22:57
  • @AbuHamzah Impossible for me to say; if you have a good test case and it works interactively then you are pretty safe. If it was my code I'd use 000000000000aaa. – Keith C Mar 01 '16 at 23:20
  • what is the significant with 000000000000aaa? – Nick Mar 02 '16 at 01:43
  • @AbuHamzah That is an ID value that is syntactically valid but to my understanding will never match with any ID in the database. I've seen it mentioned occasionally but don't know of any official documentation. – Keith C Mar 02 '16 at 07:44
  • @AbuHamzah On reflection I realise that I don't fully understand the appropriate use of the "empty id string" and have posted a question here to get a better understanding Any documentation or insight on the “empty id string”?. – Keith C Mar 02 '16 at 09:49
  • please update if you have any insight – Nick Mar 02 '16 at 13:30