Toggle Side Panel
Forcetalks
  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
    • Salesforce Industry Expertise
    • Salesforce Experts by Location
  • Webinars
  • Contact Us
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
      • Salesforce Industry Expertise
      • Salesforce Experts by Location
    • Webinars
    • Contact Us
    Close search

    Running Metadata API in Apex as a specific user

    neha
    Neha May 4, 2016
    Running Metadata API in Apex as a specific user

    Running Metadata API in Apex as a specific user

    Calling the metadata API in Apex requires admin permission or at least the “Modify All Data” permission. To run the code as a non-admin user, you need to first login as the system administrator, get a session ID, pass it to the request header, and then make the call.

    To achieve this, we can call SOAP API of Partner or Enterprise WSDL for login as the system administrator, get a session ID and then pass it to the request header, and then make a call.

    Steps for obtaining partner and metadata wsdl and then create a webservice class from it.
    – Generate from Setup in Salesforce (enter API in the Quick Find box, then select API)
    – Download the appropriate WSDL document and save it to a local directory.
    – Generate a Class from WSDL, by clicking “Generate WSDL” Button.

    Example of accessing metadata for read listview with specific user:-

    public static MetadataService.MetadataPort createService() {
        Boolean orgType;
        orgType = [SELECT Id, IsSandbox FROM Organization where Id = : UserInfo.getOrganizationId() LIMIT 1].IsSandbox;
        if(orgType){
            credentials = Credentials__c.getInstance('Sandbox');
        }
        else{
            credentials = Credentials__c.getInstance('Production');
        }
        final String USERNAME = credentials.User_Name__c;
        final String PASSWORD = credentials.Password__c;
        final String SECURITY_TOKEN = credentials.Security_Token__c;
        // Calling Partner class for login with specific user
        PartnerSoapSforceCom.Soap loginConnection = new PartnerSoapSforceCom.Soap();
        PartnerSoapSforceCom.LoginResult config = loginConnection.login(USERNAME,PASSWORD+SECURITY_TOKEN);
        userSessionId = config.sessionId;
        // Calling Metadata Class
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = userSessionId;
        return service;
    }
    // Calling createService method for reading list view.
    MetadataService.MetadataPort metadataService = createService();
    MetadataService.ListView readListView = (MetadataService.ListView)metadataService.readMetadata('ListView', new String[] { 'product2.'+listViewAPIName.DeveloperName}).getRecords()[0];
    listviewCoumns.addAll(readListView.columns);

     

    Categories: Others
    Tags: Metadata API, Running Metadata in Apex, Salesforce APIs, SOAP, WSDL

    Get listed your company

    Have an innovative Salesforce solution that delivers faster, smarter results?
    Join the Marketplace

    [adinserter block=”16″]

    best salesforce consultants
    best salesforce consultants
    best salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    salesforce consultants

    [adinserter block=”10″]

    salesforce consultants
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    Forcetalks

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.