function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter BölkePeter Bölke 

Get Picklist Value for RecordType

Hello,

i need to get the picklist value for Field Reason of the Case-Objekt. Those vary depending on the record type. I found this on the net, but i am getting an Error 401

function:
@AuraEnabled
    public static List<reasonObject> getReasonRest(Id eId){
        System.debug(eId);
        Map<String,String> sessionAttributes = Auth.SessionManagement.getCurrentSession();
        System.debug('Session '+sessionAttributes.get('SessionId'));
        Id rType = [SELECT id, RecordTypeId FROM Case WHERE ID = :eId].RecordTypeId;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String host = System.Url.getSalesforceBaseURL().toExternalForm();
        String url = host + '/services/data/v43.0/ui-api/object-info/Case/picklist-values/'+rType+'/Reason';
        //String url = host + '/ui-api/object-info/Case/picklist-values/'+rType+'/Reason';
        System.debug(url);
        request.setEndpoint(url);
        request.setMethod('GET');  
        request.setHeader('Authorization', 'OAuth '+sessionAttributes.get('SessionId'));
        HttpResponse response;        
        response = http.send(request);
        System.debug('response ' + response);
        Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        system.debug(meta);
		List<reasonObject> optionsList = new List<reasonObject>();
        //optionsList.add(new SelectOption('--None--', ''));
        if(meta.containsKey('values')){                                
                for(Object o: (List<Object> )meta.get('values')){
                    Map<String, object > temp = (Map<String, object>)o;        
                    System.debug((String)temp.get('label')+ ' ' + (String)temp.get('value'));
                    reasonObject ro = new reasonObject();
                    ro.label=(String)temp.get('label');
                    ro.value=(String)temp.get('value');
                    //optionsList.add(new SelectOption((String)temp.get('label'), (String)temp.get('value')));
                    optionsList.add(ro);
                }
            }
        return optionsList;
    }
Error
System.HttpResponse[Status=Unauthorized, StatusCode=401]
Any suggestions on this?
Is there a better solution for Lightning components?

regards
Peter
Best Answer chosen by Peter Bölke
Khan AnasKhan Anas (Salesforce Developers) 
Hi Peter,

I trust you are doing very well. It is a pleasure to be in touch with you again.

The Session Id for Lightning is restricted from most API calls. According to this document (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_api_calls.htm#apex_api_calls) :

"By security policy, sessions created by Lightning components aren’t enabled for API access. This prevents even your Apex code from making API calls to Salesforce. Using a named credential for specific API calls allows you to carefully and selectively bypass this security restriction.
The restrictions on API-enabled sessions aren’t accidental. Carefully review any code that uses a named credential to ensure you’re not creating a vulnerability."

You need to use a Connected App and Named Credential to enable access to the APIs. Please refer to the below tutorial which might help you further.

https://eltoroit.herokuapp.com/Blog.app?page=ETLC_APIsFromApex

https://sfdcpanther.wordpress.com/2018/02/05/how-to-call-salesforce-api-from-lightning-component/


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Peter,

I trust you are doing very well. It is a pleasure to be in touch with you again.

The Session Id for Lightning is restricted from most API calls. According to this document (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_api_calls.htm#apex_api_calls) :

"By security policy, sessions created by Lightning components aren’t enabled for API access. This prevents even your Apex code from making API calls to Salesforce. Using a named credential for specific API calls allows you to carefully and selectively bypass this security restriction.
The restrictions on API-enabled sessions aren’t accidental. Carefully review any code that uses a named credential to ensure you’re not creating a vulnerability."

You need to use a Connected App and Named Credential to enable access to the APIs. Please refer to the below tutorial which might help you further.

https://eltoroit.herokuapp.com/Blog.app?page=ETLC_APIsFromApex

https://sfdcpanther.wordpress.com/2018/02/05/how-to-call-salesforce-api-from-lightning-component/


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Peter BölkePeter Bölke
Hi Khan,

thanks for your answer. As for Lightning there some wierd things going on. I sued ui:inputselect. As i changed to lightning:inputField. All valid Reason which are valid for a specific Recordtype will be populated. Without any code.... 

What made me think about the difference:

https://salesforce.stackexchange.com/questions/218626/lightninginputfield-does-not-show-picklist-values-based-on-record-type