NETWORK ACCESS

OBJECTIVE

We will create a list of Network Access IP's with the help of programmatic approach.  

OUTCOME


STEPS

Create a Apex Class "MetadataService.apxc"

 Developer Console -> File -> New -> Apex Class -> "MetadataService"

     Download Code from the Link and Paste in "MetadataService.apxc" File
              



Create a Apex Class "MetadataServiceExamplesException.apxc"

 Developer Console -> File -> New -> Apex Class -> "MetadataServiceExamplesException"

     Download Code from the Link and Paste in "MetadataServiceExamplesException.apxc" File
              
     Link : https://drive.google.com/open?id=1FzkeF7xkPkz4JyP3sFzyTj8NXNhY9Bcg



Create a Apex Class "FullNetworkAccess.apxc"

 Developer Console -> File -> New -> Apex Class -> "FullNetworkAccess"

     Download Code from the Link and Paste it in "FullNetworkAccess.apxc" File
              
     Link : https://drive.google.com/open?id=1P2VjZTZEEdMcZoC0n7kKx-U1PB_iJh4W



Create a Visualforce Page "FullNetworkAccessVFP.vfp"

 Developer Console -> File -> New -> Visualforce Page -> "FullNetworkAccessVFP"

     Download Code from the Link and Paste it in "FullNetworkAccessVFP.vfp" File
              
     Link : https://drive.google.com/open?id=1tQBPplIh4GjRQxmMbtLVs7Na9guSlRVw



Create a Visualforce Tab "Enable Network Access"

Setup -> Tabs -> Visualforce Tabs -> New -> 
Visualforce Page : FullNetworkAccessVFP
Tab Label : Enable Network Access
Tab Name : Enable Network Access
Tab Style : Bridge

Add Profiles and Apps that can access the Page






Final Steps

Checkout the Page


  Copy the endpoint Link




  Create a Remote Site




    SUCCESS !!!




    Checkout the result

Image result for woohoo icons



Salesforce Developer
Name : Manoj Kumar Pradhan
Email : mkumar3969p@gmail.com
Mobile : +91-9583081611

Comments

  1. Well this is awesome. Wondering if there is a way I can do it via the execute anon without having to call up saved classes. I'll come back and post the script if I manage it :)

    ReplyDelete
  2. Ahh bummer! I missed that first file - didn't realize it required a customer service:
    https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls

    Anyway, if anyone is interested in doing this *mostly* via execute anonymous, here is a rather large snippet you could run if you don't want to set up the vf page & controller just to blast a sandbox with the whole range of ip addresses (assuming you've save the MetadataService class in your org!) Just worked for me!

    (Also just want to say I entirely just stole this code reference here and edited it a bit)

    // Enter StartIp & EndIP as strings ... example: '0.0.0.0' & '255.255.255.255'
    String startIP = '0.0.0.0';
    String endIP = '255.255.255.255';
    List results = new List();

    results.add('SUCCESS !!!');

    List> list_IP_Ranges = new List>();


    List startIP_Bytes = startIP.split('\\.');
    List endIP_Bytes = endIP.split('\\.');

    Integer startIP_1stByte = Integer.valueOf(startIP_Bytes.get(0));
    Integer startIP_2ndByte = Integer.valueOf(startIP_Bytes.get(1));
    Integer startIP_3rdByte = Integer.valueOf(startIP_Bytes.get(2));
    Integer startIP_4thByte = Integer.valueOf(startIP_Bytes.get(3));

    Integer endIP_1stByte = Integer.valueOf(endIP_Bytes.get(0));
    Integer endIP_2ndByte = Integer.valueOf(endIP_Bytes.get(1));
    Integer endIP_3rdByte = Integer.valueOf(endIP_Bytes.get(2));
    Integer endIP_4thByte = Integer.valueOf(endIP_Bytes.get(3));

    Integer noOfIteration = endIP_1stByte-startIP_1stByte;

    List ipRange;
    String startIPaddress,endIPaddress;
    for(Integer i = 0;i{startIPaddress,endIPaddress};

    list_IP_Ranges.add(ipRange);
    }

    List> lstIps = list_IP_Ranges;
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();

    MetadataService.SecuritySettings securitySettings = new MetadataService.SecuritySettings();
    securitySettings.fullName = 'Test settings';
    securitySettings.networkAccess = new MetadataService.NetworkAccess();
    securitySettings.networkAccess.ipRanges = new MetadataService.IpRange[] {};

    for(List lstIpVaLs : lstIps)
    {
    MetadataService.IpRange ipRangeval = new MetadataService.IpRange();
    ipRangeVal.start = lstIpVaLs[0];
    ipRangeval.end_x = lstIpVaLs[1];
    securitySettings.networkAccess.ipRanges.add(ipRangeVal);
    }

    securitySettings.sessionSettings = new MetadataService.SessionSettings();
    securitySettings.sessionSettings.sessionTimeout = 'FourHours';
    List mdresults = service.upsertMetadata(
    new MetadataService.Metadata[] { securitySettings }
    );

    MetadataService.UpsertResult upsertResult = mdresults[0];
    if(upsertResult==null || upsertResult.success)
    return;
    // Construct error message and throw an exception
    if(upsertResult.errors!=null)
    {
    List messages = new List();
    messages.add(
    (upsertResult.errors.size()==1 ? 'Error ' : 'Errors ') +
    'occured processing component ' + upsertResult.fullName + '.');
    for(MetadataService.Error error : upsertResult.errors)
    messages.add(
    error.message + ' (' + error.statusCode + ').' +
    ( error.fields!=null && error.fields.size()>0 ?
    ' Fields ' + String.join(error.fields, ',') + '.' : '' )
    );
    if(messages.size()>0)
    System.debug('error-' + String.join(messages, ' '));
    }
    if(!upsertResult.success)
    System.debug('error- Request failed with no specified error.');

    ReplyDelete
  3. Lol sorry for the spam... so many goofy grammar errors in there and it wont let me edit! I'm dying lol. @Manoj, feel free to format this execute anon script and include it on your page here if you want. I'll definitely be using this again at some point!

    ReplyDelete

Post a Comment