Activity › Forums › Salesforce® Discussions › How can I update Metadata Components using Salesforce Apex?
-
How can I update Metadata Components using Salesforce Apex?
Posted by PRANAV on August 16, 2016 at 1:13 PMHi All,
How can I update Metadata Components using apex ?
I want to update the attributes of both standard and custom objects by using apex code.
Thanks
Avnish Yadav replied 7 years, 8 months ago 3 Members · 2 Replies -
2 Replies
-
Hello Pranav,
Here is the sample of code to update metadata through apex code.
public class ApiCallout { public void fetch() { HttpRequest request = new HttpRequest(); request = new HttpRequest(); request.setEndpoint(‘https://ap2.salesforce.com/services/Soap/m/31.0’); request.setMethod(‘POST’); request.setHeader(‘Content-Type’, ‘text/xml’); request.setHeader(‘SOAPAction’, ‘update’); String b = ‘<?xml version=”1.0″ encoding=”UTF-8″?>’; b += ‘<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>’; b += ‘<soapenv:Header>’; b += ‘<ns1:SessionHeader soapenv:mustUnderstand=”0″ xmlns:ns1=”http://soap.sforce.com/2006/04/metadata”>’; b += ‘<ns1:sessionId>’ + UserInfo.getSessionId() + ‘</ns1:sessionId>’; b += ‘</ns1:SessionHeader>’; b += ‘</soapenv:Header>’; b += ‘<soapenv:Body>’; b += ‘<update xmlns=”http://soap.sforce.com/2006/04/metadata”>’; b += ‘<UpdateMetadata>’; b += ‘<currentName>QuoteSettings</currentName>’; b += ‘<metadata xsi:type=”ns2:QuoteSettings” xmlns:ns2=”http://soap.sforce.com/2006/04/metadata”>’; b += ‘<fullName>QuoteSettings</fullName>’; b += ‘<enableQuote>true</enableQuote>’; b += ‘</metadata>’; b += ‘</UpdateMetadata>’; b += ‘</update>’; b += ‘</soapenv:Body>’; b += ‘</soapenv:Envelope>’; request.setBody(b); request.setCompressed(false); //request.setHeader(‘Authorization’, ‘OAuth ‘ + SESSION_ID); String body = (new Http()).send(request).getBody(); system.debug(‘Hello from body’+body); } } - [adinserter block='9']
Log In to reply.