Skip to content

Instantly share code, notes, and snippets.

@alexed1
Created May 16, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexed1/7f64604d317278b10a7d03493aeb31a8 to your computer and use it in GitHub Desktop.
Save alexed1/7f64604d317278b10a7d03493aeb31a8 to your computer and use it in GitHub Desktop.
Deploying ApexClass metadata from Apex
public static MetadataService.DeployOptions getDeployOptions(){
MetadataService.DeployOptions lvDeployOption = new MetadataService.DeployOptions();
lvDeployOption.allowMissingFiles = false;
lvDeployOption.autoUpdatePackage = true;
lvDeployOption.checkOnly = false;
lvDeployOption.ignoreWarnings = true;
lvDeployOption.performRetrieve = false;
lvDeployOption.purgeOnDelete = false;
lvDeployOption.rollbackOnError = true;
lvDeployOption.runTests = new String[]{};
lvDeployOption.singlePackage = true;
lvDeployOption.testLevel = 'NoTestRun';
return lvDeployOption;
}
public static String generateZipfile() {
Zippex sampleZip = new Zippex();
Blob fileData = Blob.valueOf(getPackageXml());
sampleZip.addFile('package.xml', fileData, null);
fileData = Blob.valueOf(getHelloWorldMetadata());
sampleZip.addFile('classes/HelloWorld.cls-meta.xml', fileData, null);
fileData = Blob.valueOf(getHelloWorld());
sampleZip.addFile('classes/HelloWorld.cls', fileData, null);
return EncodingUtil.base64Encode(sampleZip.getZipArchive());
}
public static void deployApexClass() {
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
String zippedClass = generateZipfile();
MetadataService.AsyncResult deployResult= service.deploy(zippedClass, getDeployOptions());
String jobId = deployResult.id;
MetadataService.DeployResult result = service.checkDeployStatus(jobId,true);
System.debug('deploy status is: ' + result);
Long startTime = DateTime.now().getTime();
Long finishTime = DateTime.now().getTime();
while ((finishTime - startTime) < 3000) {
finishTime = DateTime.now().getTime();
}
result = service.checkDeployStatus( jobId,true);
System.debug('deploy status is: ' + result);
}
public static String getPackageXml()
{
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<Package xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<types>' +
'<members>HelloWorld</members>' +
'<name>ApexClass</name>' +
'</types>' +
'<version>48.0</version>' +
'</Package>';
}
public static String getHelloWorldMetadata()
{
return '<?xml version="1.0" encoding="UTF-8"?>' +
'<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<apiVersion>48.0</apiVersion>' +
'<status>Active</status>' +
'</ApexClass>';
}
public static String getHelloWorld()
{
return 'public class HelloWorld' +
'{' +
'public static void helloWorld()' +
'{' +
'System.debug(\' Hello World\');' +
'}' +
'}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment