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
eliotstock2eliotstock2 

Using the metadata API from within Apex code

Hi all.

 

I'm aware of the metadata API and understand how to use it from code external to Salesforce, in Java or C# for example. But what I really need is to use this API from within Apex code in a custom controller. I want to be able to set up some page layouts in the browser, then query the fields on them and whether they're required or not from within my Apex code.

 

I could generate a SOAP client for the metadata API in Apex code and use that, but I'd really like to aovid going outside just to get back in again. For a start, the tool under Setup that consumes a WSDL and generates an Apec class pukes on the metadata WSDL file (Error: Class name 'Metadata' already in use. Please edit WSDL to remove repeated names). So that already seems a bad approach.

 

Is there an internal way to access this API?

 

Thanks in advance,

 

Eliot Stock. 

SatgurSatgur

Hi,

 

You can try exploring SCHEMA Class in Apex for this purpose.

 

It does provide lot of useful describe information, but haven't validated myself whether it provides meta data level info (page layout or not).

 

 

I have used Schema class for retrieving RECORD TYPE of a given object from the system. 

 

 

- Satgur 

eliotstock2eliotstock2

Looks like this is the best that will give me:

 

Schema.DescribeSObjectResult[getKeyPrefix=a00;getLabel=CustomProject;getLabelPlural=CustomProjects;getLocalName=CustomProject__c;getName=CustomProject__c;getSObjectType=CustomProject__c;isAccessible=true;isCreateable=true;isCustom=true;isDeletable=true;isDeprecated=false;isDeprecatedAndHidden=false;isMergeable=false;isQueryable=true;isSearchable=true;isUndeletable=true;isUpdateable=true;]

 

Nothing on page layouts. 

David VPDavid VP

While you can do Schema describe calls (and object and field describes), you can not get the meta data on page layouts from within Apex directly (I want this too for an idea I'm toying with).

 

The SOAP client won't work either. If I'm not mistaken you can call the salesforce servers from the server itself.

eliotstock2eliotstock2
you mean you can NOT call the salesforce servers from the server itself?
David VPDavid VP

... I really should proofread before hitting 'Post' ..

 

Yes, that's what I meant.

Prashant WayalPrashant Wayal

If you are having the error mentioned by you then edit your Metadata.wsdl file and go to

<service name="MetadataService">

  <documentation>Manage your Salesforce.com metadata</documentation>

  <port binding="tns:MetadataBinding" name="Metadata">

this section, then simply change the name in port binding tag to different one. It will solve the problem.

NOTE ==> If it gives you another error regarding "delete" or "update" then change their names too.