Last active
March 30, 2017 05:23
-
-
Save TonyRenHK/2315fcb589e53872a7752ff4d3e10956 to your computer and use it in GitHub Desktop.
JSForce Apex Code Backup.js
This file contains hidden or 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
var jsforce = require('jsforce'); | |
var conn = new jsforce.Connection({ | |
loginUrl: 'https://test.salesforce.com' | |
}); | |
conn.login('YOURACCOUNT', 'YOURASSWORD', function(err, res) { | |
if (err) { | |
return console.error(err); | |
} | |
//Backup Visualforce Page | |
queryRecordPage("SELECT Id, Name,Markup FROM ApexPage WHERE name like 'ASI%' "); | |
//Backup Apex Class | |
queryRecordClass("SELECT Id, Name,Body FROM ApexClass WHERE name like 'ASI "); | |
//Backup Visualforce Components Function | |
queryRecordComp("SELECT Id, Name,Markup FROM apexComponent WHERE name like 'ASI%' "); | |
}); | |
//query Visualforce Components | |
function queryRecordComp(inputSQL) { | |
var records = []; | |
conn.query(inputSQL, function(err, result) { | |
if (err) { | |
return console.error(err); | |
} | |
console.log("total : " + result.totalSize); | |
console.log("fetched Comp: " + result.records.length); | |
for (var i = 0; i < result.records.length; i++) { | |
console.log(result.records[i].Name); | |
writefile(result.records[i].Name + '.cmp', result.records[i].Markup); | |
} | |
}); | |
} | |
//query Visualforce Page | |
function queryRecordPage(inputSQL) { | |
conn.query(inputSQL, function(err, result) { | |
if (err) { | |
return console.error(err); | |
} | |
console.log("total : " + result.totalSize); | |
console.log("fetched Page: " + result.records.length); | |
for (var i = 0; i < result.records.length; i++) { | |
writefile(result.records[i].Name + '.page', result.records[i].Markup); | |
} | |
}); | |
} | |
//query class and its body | |
function queryRecordClass(inputSQL) { | |
conn.query(inputSQL, function(err, result) { | |
if (err) { | |
return console.error(err); | |
} | |
console.log("total : " + result.totalSize); | |
console.log("fetched Class : " + result.records.length); | |
for (var i = 0; i < result.records.length; i++) { | |
writefile(result.records[i].Name + '.cls', result.records[i].Body); | |
} | |
}); | |
} | |
function writefile(InputName, InputBody) { | |
var fs = require('fs'); | |
fs.writeFile(InputName, InputBody, function(err) { | |
if (err) { | |
return console.log(err); | |
} | |
console.log("The file" + InputName + " was saved!"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment