Skip to content

Instantly share code, notes, and snippets.

@edmondop
Created September 7, 2020 14:19
Show Gist options
  • Save edmondop/eb66b4dd06bae9a11a25be1632678bb7 to your computer and use it in GitHub Desktop.
Save edmondop/eb66b4dd06bae9a11a25be1632678bb7 to your computer and use it in GitHub Desktop.
Remove metadata from unlocked packages
metadataElements = [{
name: 'CGS Score',
metadataType : 'Custom Field',
parentObject : 'Account' // can be undefined
},
{
name: 'Rating Value',
metadataType : 'Custom Field',
parentObject : 'Account' // can be undefined
}];
function buildPackageRemovalUrl(jsCode){
decodedJs = decodeURI(jsCode);
location_index = decodedJs.indexOf('window.location');
window_location_prefix_length = "window.location%3D".length;
end_url_index = decodedJs.lastIndexOf('\'');
location_url = decodedJs.substring(location_index + window_location_prefix_length, end_url_index+1);
decoded_location_url = decodeURIComponent(location_url);
return decoded_location_url.substring(1,decoded_location_url.length -1);
}
function isRightMetadataElement(field, parentObjectTdText, metadataTypeTdText){
let isRight = true;
if(parentObjectTdText !='' && !field.parentObject || field.parentObject !== parentObjectTdText )
{
isRight = false;
}
if(field.metadataType != metadataTypeTdText)
isRight = false;
return isRight;
}
function removeMetadataElementFromPackage(field, node){
node = document.evaluate('//a[text()="' + field.name + '"]',document);
componentDetailHref = node.iterateNext();
while (componentDetailHref != null){
row = componentDetailHref.parentNode.parentNode
parentObject = document.evaluate('td[2]',row).iterateNext().textContent;
metadataType = document.evaluate('td[3]',row).iterateNext().textContent;
if(isRightMetadataElement(field, parentObject, metadataType)){
removeActionHref = row.firstElementChild.firstElementChild;
jsCode = removeActionHref.attributes['href'].value;
decoded_location_url = buildPackageRemovalUrl(jsCode);
console.log('Opening new window at ' + decoded_location_url + ' for field ' + JSON.stringify(field));
window.open(decoded_location_url);
}else
{
console.log(row + ' does not contain value for field ' + JSON.stringify(field) + ' moving forward');
}
componentDetailHref = node.iterateNext();
}
}
metadataElements.forEach( metadataElement => {
removeMetadataElementFromPackage(metadataElement);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment