1

Using the Force Migration Tool, we want to find out how to capture the asyncRequestId from our Salesforce deployments as an ANT property for use later in our ANT target.

For Example, I may deploy using my ANT target and the response will show:

Request ID for the current retrieve task: 09SR0000001BtJJMA0

Now I want to automatically cancel that deployment if the deploy time takes 2 hours using the sf:cancelDeploy task.

While I could copy/paste the request ID to cancel the deployment manually, how would I capture the returned asyncRequestId as an ANT Property?

Example Target Mock-Up:

<!-- Try to Deploying to Dev Sandbox, if it takes more than 2 hours cancel deployment-->
<target name="deployDEV">

  <sf:deploy username="${dev.username}" password="${dev.password}" sessionId="${dev.sessionId}" serverurl="${dev.serverurl}" maxPoll="${dev.maxPoll}" deployRoot="src" testlevel="NoTestRun" rollbackOnError="true"/>

  <property name="dev.asyncRequestId" value=" **[DEPLOYMENT'S REQUEST ID]** "/>

  <condition **[2 HOURS PASSED AND DEPLOYMENT IS NOT DONE]**>
    <sf:cancelDeploy username="${dev.username}" password="${dev.password}" sessionId="${dev.sessionId}" serverurl="${dev.serverurl}" maxPoll="${dev.maxPoll}" requestId="${dev.asyncRequestId}"/>
  </condition>

</target>

Note that I specifically want to cancel the deployment rather than just let the build time out so that I can begin my next queued tests on CircleCI. Their hard 2 hour time limit causes cascading timeouts if one build time's out without being cancelled server-side.

In the Metadata API, it is really easy to get the ID like in the below example, but I'm not sure how to adapt it for ANT:

AsyncResult asyncResult = metadataConnection.deploy(zipBytes, deployOptions);
String asyncResultId = asyncResult.getId();
  • I may be wrong but since you are cancelling an ongoing deployment because of 2 hours limit. There will be no new job id created which one can retrieve. – maniac coder Sep 24 '16 at 6:28
1

I don't believe it is currently possible to query for the DeployRequest Id after the API call is made. You need to capture it from the deploy result directly.

As you found, if you use the Metadata API directly it is easy enough to get from the AsyncResult. However, the corresponding Force.com migration tool only exposes it via the log output.

One option might be to capture the output from the ant task directly. I think record is the task you want. Wrap the sf:deploy with a record start/stop.

Then run regex on the file to extract the request id. Use something like extracting value from a file using ant and the regex [a-zA-Z0-9]{18}.


Putting it all together:

<target name="deployDEV">

  <record name="deploylog.txt" action="start"/>
  <sf:deploy username="${dev.username}" password="${dev.password}" sessionId="${dev.sessionId}" serverurl="${dev.serverurl}" maxPoll="${dev.maxPoll}" deployRoot="src" testlevel="NoTestRun" rollbackOnError="true"/>
  <record name="deploylog.txt" action="stop"/>

  <!-- Extract the request id from the deploy log -->
  <loadfile 
    srcFile="deploylog.txt" 
    property="dev.asyncRequestId"> 
     <filterchain> 
       <!-- We are only interested in the last line of the deploy log -->
       <tailfilter lines="1"/>
       <!-- And only something that looks like a Salesforce ID. -->
       <tokenfilter>
         <!-- Split by white space and exclude the delimiter characters -->
         <stringtokenizer suppressdelims="true"/>
         <containsregex pattern="[a-zA-Z0-9]{18}"/>
       </tokenfilter>
     </filterchain> 
  </loadfile> 

  <echo message="DEBUG LOG: dev.asyncRequestId = ${dev.asyncRequestId}" />

  <condition **[2 HOURS PASSED AND DEPLOYMENT IS NOT DONE]**>
    <sf:cancelDeploy username="${dev.username}" password="${dev.password}" sessionId="${dev.sessionId}" serverurl="${dev.serverurl}" maxPoll="${dev.maxPoll}" requestId="${dev.asyncRequestId}"/>
  </condition>

</target>

Your Answer

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

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