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
CarlBCarlB 

Calling metadata REST API from Apex

Hello.

 

I have created an Apex class that I want to access some Salesforce metadata from.  I thought the easiest way would be to call the REST metadata service, thus:

 

PageReference ref = new PageReference ('/services/data/v20.0/Account/describe/');
ref.getHeaders().put('Authorization', 'Bearer ' + UserInfo.getSessionId());
System.debug('CONTENTS: ' + ref.getContent().toString());		

 However, when I try to run this, I get "401 status code return from request to http://na6.salesforce.com/services/data/v20.0/Account/describe/?inline=1"

 

Any ideas?

 

Thanks,

 

Carl

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_sobject_describe.htm

 

Use the above for the metadata .

 

If you want to use apex REST API for this then its too much conversions 

 

http://developer.force.com/cookbook/recipe/posting-a-chatter-mention-via-apex

 

This example shows how to make calls internally inside salesforce and get data .Too much conversions and for your case using describe calls will be best suited 

CarlBCarlB

Hello.

 

Thanks for the reply.

 

The reason that I need to use the metadata API rather than the Apex describe is that the Apex describe does not include some of the information that is available through the metadata API, for example a description of the views defined on the object.

 

I tried using the example in the cookbook and I get an unauthorised endpoint error.  Does this mean that I have to add my own Salesforce server (e.g. NA1.force.com) as a remote site?

 

I was hoping to avoid that, which is why I was trying to use the PageReference class.

 

Regards,

 

Carl

CarlBCarlB

It looks like I was barking up the wrong tree.  The REST API does not support the full metadata, so I can't get hold of the View definition that way.

 

Just another Salesforce dead end.

 

 

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

You can look into metadata API for your needs .Salesforce exposes meta data API.These may have views 

CarlBCarlB

I have seen some discussion about calling the metadata API from within Salesforce, but does that mean that I would have to add a remote site setting for my Saleforce instance, e.g. na1.salesforce.com?

 

Thanks,

 

Carl

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

http://www.salesforce.com/us/developer/docs/api_meta/index.htm

 

Link to metadata API .I think getting views is quite complex .

 

Ishaan UppalIshaan Uppal

Hi Mohit,

 

I have similar doubt. What should I specify in the Server URL of Metadata API. This should definitely not be hard-coded na1 or na2 or some other instance because it will vary for each organization. Then each organization would also need to create Remote Site Setting for this URL. So is there any general URL which we can specify in the Metadata API Endpoint and bundle remote site setting for that URL in our app and it can work seamlessly on all organizations