Thursday, August 9, 2018

Remote Site Settings : Way to Create/Update Using Apex (Metadata API)

We can create or update Remote Site Settings in Apex using Metadata API.

You can either download Metadata API WSDL from Salesforce and generate apex class by clicking on Generate from WSDL button.


Or you can download the MetadataService class from below link:

MetadataService.cls

Below is sample to code to create remote site settings.


Execute this static method from execute anonymous and check if records created or not.

 MetadataAPIUtility.createRemoteSiteSettings();

Below is snapshot of remote site settings created


Note:
  • I have used upsertMetadata method. If you want to only insert new remote site setting, then use insertMetadata method
  • fullname property is considered in order to decide whether remote site setting needs to be created or updated.
Hope this will help!!!


Create Custom Metadata Types Using Metadata API
Create Update Custom Label by Using Metadata API


16 comments:

  1. Hi, I'm getting this error:

    Error: Method does not exist or incorrect signature: void createService() from the type MetadataAPIUtility line 24 column 52

    ReplyDelete
  2. Hello ,
    thanks for this tutorial (very useful)
    I encounter an error when trying to execute the code in anonymous MetadataAPIUtility.createRemoteSiteSettings();

    Line: 13497, Column: 1
    System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

    I don't know why because i'm trying to udpate any record other than the remote site settings. Any idea ?

    ReplyDelete
    Replies
    1. NO problem: I found my error... => there was something i was trying to update before creating the remotesite Settings

      Delete
  3. Hi,
    How can i fetch remote site setting records using apex?

    ReplyDelete
    Replies
    1. Hi Raj,

      You can use tooling API to fetch remote site settings. Execute below code in developer console which uses tooling API to get remote site settings details:

      HttpRequest req = new HttpRequest();
      req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
      req.setHeader('Content-Type', 'application/json');
      String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
      system.debug('********domainUrl:'+domainUrl);
      req.setEndpoint(domainUrl+'/services/data/v44.0/tooling/query/?q=Select+EndpointUrl,SiteName+from+RemoteProxy');
      req.setMethod('GET');

      Http h = new Http();
      HttpResponse res = h.send(req);
      system.debug(res.getBody());

      Now parse the JSON response.

      Delete
  4. Hi,
    I'm Trying to create remote site Setting in apex using Metadata Service.But i'm Unable to create, It says it 'Unauthorized endpoint'.The remote site is my Base URL,any Idea to resolve this
    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi Ankit,
      I am getting same issue. Are you able to resolve this issue?

      Delete
  5. The time has arrived now IT staff can easily perform a unified desktop management for remote workers, remote offices, and local networked users  task list templates

    ReplyDelete
  6. Hi,
    Can we use this as a part of managed package, to update subscriber org

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Did you figure out how to query for the list of existing RemoteSiteSettings at all?

    ReplyDelete
  9. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. web-agency

    ReplyDelete
  10. Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. sviluppo sito ecommerce

    ReplyDelete
  11. This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. sites like fiverr australia

    ReplyDelete