0

enter image description hereWhen i try to access other org metadata item list after authentication getting the following error ..

Web service callout failed: WebService returned a SOAP Fault: INVALID_CROSS_REFERENCE_KEY: invalid cross reference id faultcode=sf:INVALID_CROSS_REFERENCE_KEY faultactor=

Only able to get metadata type not the item after selecting the metadata type

public with sharing class CallBackURLController{
public class wrapperOfTknLink{
    @AuraEnabled
    public String acsToken;
    @AuraEnabled
    public String link;

    public wrapperOfTknLink(String acsToken,String link){
        this.acsToken = acsToken;
        this.link = link;
    }
}

@AuraEnabled  
public static List<wrapperOfTknLink> AccessToken(String AuthCode){
    List<wrapperOfTknLink> wrpr = new List<wrapperOfTknLink>();
    string bodyCode;
    string bodyInfo;
    string cd;
    string hd;
    list<contact> lstCon;
    String vrsn='46.0';
    //string Accesscode=ApexPages.currentPage().getParameters().get('code');
    //Accesscode = Accesscode.replace('=','%3D').trim();
    string clientId='id provided';
    String clientSecret='clientscrt key';
    String redireUri='https://jhaalok-developer-edition.ap4.force.com/MetaOrg/RetrieveXMLCmpPage';
    string loginUri='https://login.salesforce.com';
    string ContentType = 'application/json';

    //Get Access Token
    HttpRequest req = new HttpRequest();  
    req.setMethod('POST');
    string reqbody='grant_type=authorization_code' +
        '&client_id=' + clientId +
        '&client_secret=' + clientSecret +
        '&code=' + AuthCode +
        '&redirect_uri=' + redireUri;
    req.setEndpoint(loginUri+'/services/oauth2/token?'+reqbody);
    Http http = new Http();  
    HTTPResponse res = http.send(req);
    bodyCode=res.getBody();
    Map<String, object> mapReqBody=(Map<String, object>)JSON.deserializeUntyped(res.getBody());
    system.debug('------mapReqBody-------'+mapReqBody);
    String linkToFetch = string.valueOf(mapReqBody.get('id'));
    string header=string.valueOf(mapReqBody.get('token_type'))+' '+string.valueOf(mapReqBody.get('access_token'));
    string acsToken = string.valueOf(mapReqBody.get('access_token'));
    system.debug('-------header---------'+header);
    hd=header;        

    wrpr.add(new wrapperOfTknLink(acsToken,linkToFetch));

    // Get User Info
    HttpRequest reqInfo = new HttpRequest();  
    reqInfo.setMethod('POST');
    reqInfo.setEndpoint(loginUri+'/services/oauth2/userinfo');
    reqInfo.setHeader('Content-Type','application/json');
    reqInfo.setHeader('Authorization', header);
    Http httpInfo = new Http();  
    HTTPResponse resInfo = httpInfo.send(reqInfo);
    bodyInfo = resInfo.getBody();
    system.debug('----bodyInfo-----'+bodyInfo);
    Map<String, object> mapReqInfoBody=(Map<String, object>)JSON.deserializeUntyped(resInfo.getBody());       
    system.debug('-----mapReqInfoBody-------'+mapReqInfoBody);   
    system.debug('#########wrapper data accs token and link'+wrpr);
    return wrpr;
}

private static final Integer METADATA_API_VERSION = 
    Integer.valueOf(new MetadataService.MetadataPort().endpoint_x.substringAfterLast('/'));

public class OptionVal{
    @AuraEnabled
    public String label;
    @AuraEnabled
    public String value;       
    public OptionVal(String label,String value){
        this.label = label;
        this.value = value;
    }
}
public static List<OptionVal> MetaDataTypes;
public static String MetaDataType;  
public static List<OptionVal> MetaDataItems;
public static String MetaDataFolder;
public static String MetaDataItem;
public static String MetadataFileName;
public static String MetadataFileData;
public static MetadataService.AsyncResult AsyncResult;
public static String MetaDataRetrieveZip;
public static List<MetadataFile> MetadataFiles;

@AuraEnabled
public static String listMetadataTypes(String AcessTkn,String endPointLink){
    String endPntLnk = endPointLink;
    // List available Metadata Types via the 'describeMetadata' API call
    MetadataService.MetadataPort service = createService(AcessTkn);                 
    MetadataService.DescribeMetadataResult describeResult = service.describeMetadata(METADATA_API_VERSION);
    List<String> metadataTypeNames = new List<String>();
    for(MetadataService.DescribeMetadataObject metadataObject : describeResult.metadataObjects)
    {
        metadataTypeNames.add(metadataObject.xmlName);
        if(metadataObject.childXmlNames!=null)
            for(String childXmlName : metadataObject.childXmlNames)
            if(childXmlName!=null)
            metadataTypeNames.add(childXmlName);
    }   

    // Sort Metadata Types
    metadataTypeNames.sort();
    MetaDataTypes = new List<OptionVal>();              
    for(String metadataTypeName : metadataTypeNames)
        MetaDataTypes.add(new OptionVal(metadataTypeName, metadataTypeName));
    system.debug('MetaDataTypes'+MetaDataTypes);     
    String metaDataWrap = Json.serialize(MetaDataTypes); 
    //listMetadataItems(AcessTkn,endPointLink);
    return metaDataWrap; 
}

@AuraEnabled
public static String listMetadataItems(String slctdVal,String AcessTkn,String endPointLink){
    system.debug('inside listMetadataItems()');
    MetaDataFolder = '';
    system.debug('endPointLink-------'+endPointLink);
    system.debug('--------slectedValue-------'+slctdVal);
    system.debug('------AccessToken-------'+AcessTkn);
    MetaDataType = slctdVal;
    system.debug('------MetaDataType------'+MetaDataType);
    // List Metadata items for the selected Metadata Type
    MetaDataItems = new List<OptionVal>();      
    MetadataService.MetadataPort service = createService(AcessTkn);
    system.debug('service----'+service);             
    List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();        
    MetadataService.ListMetadataQuery queryLayout = new MetadataService.ListMetadataQuery();
    system.debug('++++++++++++++++++++++queryLayout-------------'+queryLayout);
    system.debug('++++++++++++++++++++++MetaDataFolder-------------'+MetaDataFolder);

    system.debug('++++++++++++++++++++++slctdVal-------------'+slctdVal);
    system.debug('++++++++MetaDataType--------'+MetaDataType);
    if(MetaDataFolder!=null && MetaDataFolder.length()>0)
        queryLayout.folder = MetaDataFolder;
    queryLayout.type_x = MetaDataType;
    queries.add(queryLayout);  
    system.debug('queries---'+queries);
    system.debug('METADATA_API_VERSION---'+METADATA_API_VERSION);
    MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 42);
    system.debug('fileProperties'+fileProperties);
    // Sort
    List<String> fullNames = new List<String>();
    if(fileProperties!=null){
        for(MetadataService.FileProperties fileProperty : fileProperties)
            fullNames.add(fileProperty.fullName);
        fullNames.sort();
        for(String fullName : fullNames)
            MetaDataItems.add(new OptionVal(fullName,EncodingUtil.urlDecode(fullName, 'UTF-8')));
    }
    String metaDataItemWrap = Json.serialize(MetaDataItems); 
    system.debug('metaDataItemWrap------'+metaDataItemWrap);
    return metaDataItemWrap; 
}


@AuraEnabled
public static MetadataService.MetadataPort createService(String sessionId){ 
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = sessionId;
    service.endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/m/42.0';
    system.debug('-----service.endpoint_x----'+service.endpoint_x);
    system.debug('service.SessionHeader.sessionId'+ service.SessionHeader.sessionId);
    return service;     
}   



public class MetadataFile extends MetadataService.MetadataWithContent{
    @AuraEnabled
    public String getFullname(){
        return fullName;
    }
    @AuraEnabled
    public String getContent(){
        return content;
    }
}
}

The response I am getting with error in debug log is:

07:10:11.200 (1200320234)|CALLOUT_REQUEST|[13520]|SESSION_ID_REMOVEDApexClass42.0 07:10:11.200 (1200434748)|CALLOUT_REQUEST|[13520]|listMetadata_element:[apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), asOfVersion=42.0, asOfVersion_type_info=(asOfVersion, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false), field_order_type_info=(queries, asOfVersion), queries=(ListMetadataQuery:[apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), field_order_type_info=(folder, type_x), folder=null, folder_type_info=(folder, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), type_x=ApexClass, type_x_type_info=(type, http://soap.sforce.com/2006/04/metadata, null, 1, 1, false)]), queries_type_info=(queries, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false)]::SOAPAction="" Accept=text/xml User-Agent=SFDC-Callout/46.0 SFDC_STACK_DEPTH=1 Content-Type=text/xml; charset=UTF-8 07:10:11.200 (1714423757)|CALLOUT_RESPONSE|[13520]|sf:INVALID_CROSS_REFERENCE_KEYINVALID_CROSS_REFERENCE_KEY: invalid cross reference id 07:10:11.200 (1714706344)|EXCEPTION_THROWN|[13520]|System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_CROSS_REFERENCE_KEY: invalid cross reference id faultcode=sf:INVALID_CROSS_REFERENCE_KEY faultactor= 07:10:11.200 (1715004967)|HEAP_ALLOCATE|[13520]|Bytes:174 07:10:11.200 (1715058900)|METHOD_EXIT|[146]|01p6F00000QUCFO|MetadataService.MetadataPort.listMetadata(List, Double) 07:10:11.200 (1715199523)|FATAL_ERROR|System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_CROSS_REFERENCE_KEY: invalid cross reference id faultcode=sf:INVALID_CROSS_REFERENCE_KEY faultactor=

Class.MetadataService.MetadataPort.listMetadata: line 13520, column 1 Class.CallBackURLController.listMetadataItems: line 146, column 1 07:10:11.200 (1715223701)|FATAL_ERROR|System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: INVALID_CROSS_REFERENCE_KEY: invalid cross reference id faultcode=sf:INVALID_CROSS_REFERENCE_KEY faultactor=

  • Can you please share the code or request – Chandra Kiran Aug 13 at 7:10
  • i have added the apex class which i am using to retrieve the metadata item @ChandraKiran – alok jha Aug 13 at 7:22
  • You are authenticating the System admin user of other instance. Correct? – Chandra Kiran Aug 13 at 7:55
  • yes @ChandraKiran – alok jha Aug 13 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.