Created
May 16, 2020 19:16
-
-
Save alexed1/7f64604d317278b10a7d03493aeb31a8 to your computer and use it in GitHub Desktop.
Deploying ApexClass metadata from Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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