Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a requirement where I need to fetch list of objects present on another Org. I tried it using metadata service but could not fetch.Any Idea how this can be done using Partner WSDL or ToolsLink API. I am pasting my sample code using metadta service.

public void fetchMetadata()
    {
        a = TestMetadata.createService();
        a.describeMetadata(29);
        MetadataService.DescribeMetadataResult describeRes = new MetadataService.DescribeMetadataResult();
        describeRes = a.describeMetadata(29);
        List<MetadataService.DescribeMetadataObject> objectList = describeRes.MetadataObjects;
         if (objectList.size() > 0) {
            for (MetadataService.DescribeMetadataObject obj : objectList) {
                system.debug('****'+obj.XmlName);

            }
         }
    }
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The metadata API isn't for retrieving records, it is for retrieving information about the structure of those records. E.g. what fields are on a custom object and what data type each field is.

The partner API isn't really intended for consumption from Apex. It includes a number of schema features that aren't supported by Wsdl2Apex.

One of these is the any element on the complexType sObject. This is important, as it is this element that would contain the sObject fields (beyond the Id) when retrieved via a SOQL query.

E.g.

This will work if you have at least one Account in the target org and have the remote site settings setup to include https://login.salesforce.com and https://targetOrgPod.salesforce.com

partnerSoapSforceCom.Soap partner = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult lr=partner.login('test@example.com', 'myPasswordInPlainText!PlusTheSecurityToken');
partnerSoapSforceCom.SessionHeader_element header=new partnerSoapSforceCom.SessionHeader_element();
header.sessionId=lr.sessionId;
partner.SessionHeader=header;
partner.endpoint_x = lr.serverUrl;
partnerSoapSforceCom.QueryResult qr = partner.Query('Select Id from Account limit 1');
System.debug('Number of Query results:' + qr);
sobjectPartnerSoapSforceCom.sObject_x result1 = qr.records[0];
System.debug(result1);

However, if you add anything to the returned field set beyond Id you will get an error due to the missing any element. Here is what happens when I try the SOQL Select Id, Name from Account limit 1.

System.CalloutException: Web service callout failed: Unable to parse callout response. Apex type not found for element Name: Class.DFB.partnerSoapSforceCom.Soap.query: line 2048, column 1 AnonymousBlock: line 7, column 1 AnonymousBlock: line 7, column 1

You could potentially use the Enterprise WSDL, which will include explicit elements for each field. It won't be able to dynamically handle changes in schema though.


You might be better served by an alternative method of retrieving the data, such as:

share|improve this answer
    
thanks it worked :) –  Sonal Apr 4 at 9:22
    
@Sonal. I'm glad it worked. Which solution did you go with? Also, there is a green tick towards the top of the answer that can be used to indicate it as such. This makes the answer clearer to future users. –  Daniel Ballinger Apr 6 at 22:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.