In my package I created a Detail Page Button that executes some Javascript. The intention is be able to add this to the Contact layout using the Metadata API.
I looked in eclipse and this is the metadata generated for my button:
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<webLinks>
<fullName>myCustomContactButton</fullName>
<availability>online</availability>
<displayType>button</displayType>
<linkType>javascript</linkType>
<masterLabel>Use My Button</masterLabel>
<openType>onClickJavaScript</openType>
<protected>false</protected>
<url>var url = '/apex/CustomVfAction?Id={!Contact.Id}'; //string for the URL of the
parent.location.href = url; //refresh the page</url>
</webLinks>
</CustomObject>
And here is how I am attempting to add to the layout (following the instructions from https://andyinthecloud.com/2014/04/24/apex-metadata-api-and-spring14-keys-to-the-kingdom/)
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
// Read the Layout
MetadataService.Layout layout =
(MetadataService.Layout) service.readMetadata('Layout',
new String[] { 'Contact Layout' }).getRecords()[0];
// Add the Custom Button to the Layout
if(layout.customButtons==null)
layout.customButtons = new List<String>();
layout.customButtons.add('myCustomContactButton');
However this is throwing an exception in Apex - it's not very helpful though. It simply says "Webservice returned a SOAP fault: UNKNOWN EXCEPTION".
Any ideas on what I'm doing wrong or how to troubleshoot this? I'm not very familiar with the MetaData API.