function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dev ListingOrgDev ListingOrg 

Enable Quotes object using Apex Code

Enable Quote sObject from Apex Code
Hi,

I am using Quote sObject in Rest API call but in new instance, I have to enable Quote sObject manually by clicking checkbox in Setup -> Quote -> Settings -> Enable Quote.

How can I enable it from Apex Code ??

Please let me know how can I enable it from Apex Code.

Can we put some code in "Post Install Script"? How can we use Metadata in our apex code for enable Quotes when in install package in my customer's account.
 
Piyush Kumar 58Piyush Kumar 58

Hello,
Use this code to enable Quotes in your org. I was write this code for enable Quotes in my developer org:-
 

public class EnableQuotes {  

    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);
    }

}

If you face any issue then let me know.

Thanks
Piyush.
Piyush Kumar 58Piyush Kumar 58
NO We can not enable quotes through post install script . Because we enable Quotes throgh metadata api and in this process Session Id is required for this. We can not access Session Id through post install script or any outher future method.