[SalesForce] Invoking metadata API call using JSFORCE

Consider:

<apex:page applyHtmlTag="true">
    <apex:includeScript value="{!$Resource.Jsforce}"/>
    <input type="file" id="input"></input>
    <script type="text/javascript">
        function ctrlRead(){
            var selectedFile = document.getElementById('input').files[0];
            var files = selectedFile.files;
            var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}'});
            conn.metadata.retrieve(files,function(error,ref) {
                if (error) {
                   console.log("error");
                }
                else {
                    console.log(JSON.stringify(ref));
                }
            });
        }
    </script>
    <apex:form>
        <div>DEPLOYMENT TOOL</div>
        <apex:commandButton onclick="ctrlRead();" value="Reterive" id="theButton"/>
    </apex:form>
</apex:page>

I am trying to pass the file to the retrieving metadata API call using JSFORCE to extract the metadata. But I am getting the error

"INVALID_CROSS_REFERENCE_KEY: No packages or unpackaged specified"

How can I fix this problem?

Here is the next attempt and updated code.

I modified the code and tested it:

<apex:page applyHtmlTag="true">
    <apex:includeScript value="{!$Resource.Jsforce}"/>
    <input type="file" id="input"></input>
    <script type="text/javascript">
        function ctrlRead(){
            var selectedFile = document.getElementById('input').files[0];
            var files = selectedFile.files;
            var para = {apiVersion:37.0,packageNames:null,singlePackage:true,specificFiles:files,unpackaged:files};
            var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}'});
            conn.metadata.retrieve(para,function(error,ref) {
                if (error) {
                   console.log("error");
                }
                else {
                    console.log(JSON.stringify(ref));
                }
            });
        }
    </script>
    <apex:form >
        <div>DEPLOYMENT TOOL</div>
        <apex:commandButton onclick="ctrlRead();" value="Reterive" id="theButton"/>
    </apex:form>
</apex:page>

But now I am getting an error:

INVALID_CROSS_REFERENCE_KEY: More than 1 package retrieved with single package flag

This is the modified one i am getting success response

<apex:page applyHtmlTag="true">
<apex:includeScript value="{!$Resource.Jsforce}"/>
<input type="file" id="input"></input>
 <script type="text/javascript">
     function ctrlRead(){ 
        var selectedFile = document.getElementById('input').files[0];
        var files = selectedFile.files; 
        var para = {apiVersion:37.0,singlePackage:false,unpackaged:files};
         var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}'});
         conn.metadata.retrieve(para,function(error,ref) {
             if (error) {
                console.log("error");
             } 
             else {
                 console.log(JSON.stringify(ref));

             } 
      });
     }        
  </script>
 <apex:form >
 <div>DEPLOYMENT TOOL</div>
 <apex:commandButton onclick="ctrlRead();" value="Reterive" id="theButton"/>
 </apex:form>
 </apex:page>

Best Answer

From the metadata documentation for the retrieve call in SFDC, metadata requires any of the below.

  1. Package name or list of packages you want to retrieve the metadata info for

  2. A package.xml consisting of the list of file names to retrieve

I guess in your case you should create a retrieve Request object which will be pointed to the package.xml file.

The working code is as below.

<apex:page applyHtmlTag="true">
    <apex:includeScript value="{!$Resource.Jsforce}"/>
    <input type="file" id="input"></input>
    <script type="text/javascript">
        function ctrlRead(){
            var selectedFile = document.getElementById('input').files[0];
            var files = selectedFile.files;
            var para = {apiVersion:37.0,singlePackage:false,unpackaged:files};
            var conn = new jsforce.Connection({ accessToken: '{!$Api.Session_Id}'});
            conn.metadata.retrieve(para,function(error,ref) {
                if (error) {
                    console.log("error");
                }
                else {
                    console.log(JSON.stringify(ref));
                }
            });
        }
    </script>
    <apex:form >
        <div>DEPLOYMENT TOOL</div>
        <apex:commandButton onclick="ctrlRead();" value="Reterive" id="theButton"/>
    </apex:form>
</apex:page>
Related Topic