4

I am using salesforce Metadata Api to insert network access Ip ranges using apex. I have deployed classes given in github.
For inserting List of Ip ranges I have added two methods in MetadataServiceExample class.
createIpRange() and createListIpRange(List> lstIps).

public static void createIpRange(String startIp, String endIp)
    {

        MetadataService.MetadataPort service = createService();
        MetadataService.SecuritySettings securitySettings = new MetadataService.SecuritySettings();
        securitySettings.fullName = 'Test settings';
        securitySettings.networkAccess = new MetadataService.NetworkAccess(); 
        securitySettings.networkAccess.ipRanges = new MetadataService.IpRange[] {new MetadataService.IpRange()}; 
        securitySettings.networkAccess.ipRanges[0].end_x = endIp;
        securitySettings.networkAccess.ipRanges[0].start = startIp;
        securitySettings.sessionSettings = new MetadataService.SessionSettings(); 
        securitySettings.sessionSettings.sessionTimeout = 'FourHours'; 
        /*List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { securitySettings });
        handleSaveResults(results[0]); */
         /*List<MetadataService.UpsertResult> results =
            service.upsertMetadata(
                new MetadataService.Metadata[] { securitySettings }); 
        handleUpsertResults(results[0]); */ 
         List<MetadataService.SaveResult> results =
            service.createMetadata(
                new MetadataService.Metadata[] { securitySettings });
        handleSaveResults(results[0]);
    }

public static void createListIpRange(List<List<String>> lstIps)
    {
        for(List<String> lstIpVaLs : lstIps)  
        {
            createIpRange(lstIpVaLs[0],lstIpVals[1]);
        }
    }

From the execute anonymous window I am running the following code snippet -

List<List<String>>ListOfIps = new List<List<String>>();
List<String> lstIpVals = new List<String>();
List<String> lstIpVals2 = new List<String>();
lstIpVals.add('114.143.14.6');
lstIpVals.add('114.143.14.6');
ListOfIps.add(lstIpVals);
lstIpVals2 = new List<String>();
lstIpVals2.add('114.143.14.7');
lstIpVals2.add('114.143.14.7');
ListOfIps.add(lstIpVals2);
MetadataServiceExamples.createListIpRange(ListOfIps);


The problem that I am facing is when using updateMetadata() and upsertMetadata() it not only it inserts only last Ip range of the input list parameter but it also deletes existing Ip ranges if any exists.
So running this code results in only one network access record. Also when I use createMetadata I get FieldIntegrityException.
Any suggestions what I am missing here .
Thanks in advance

1 Answer 1

3

It was a silly mistake . I updated code as follows -

 public static void createIpRange(List<List<string>> lstIps)
    {
        MetadataService.MetadataPort service = createService();
        MetadataService.SecuritySettings securitySettings = new MetadataService.SecuritySettings();
        securitySettings.fullName = 'Test settings';
        securitySettings.networkAccess = new MetadataService.NetworkAccess(); 
        securitySettings.networkAccess.ipRanges = new MetadataService.IpRange[] {}; 

        for(List<String> 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<MetadataService.UpsertResult> results =
            service.upsertMetadata(
                new MetadataService.Metadata[] { securitySettings }); 
        handleUpsertResults(results[0]); 
    }

and in the execute anonymous I am calling

MetadataServiceExamples.createListIpRange(ListOfIps);
1
  • But still with the updated code also the existing IPRanges are getting deleted!!!
    – Rajesh
    Jan 29, 2018 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .