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