I have requirement to update ProfileLoginIpRange on the profile. Lets say there are 5 IP ranges already added on the profile and I want to add 1 more to it.
Code Used,
MetadataService.MetadataPort metaDataPort = new MetadataService.MetadataPort();
metaDataPort.SessionHeader = new MetadataService.SessionHeader_element();
metaDataPort.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.Profile existingProfile = new MetadataService.Profile();
existingProfile.fullName = 'Chatter External User';
MetadataService.ProfileLoginIpRange profileLoginIpRange1 = new MetadataService.ProfileLoginIpRange();
profileLoginIpRange1.startAddress = '0.0.0.0';
profileLoginIpRange1.endAddress = '1.1.1.1';
existingProfile.loginIpRanges = new MetadataService.ProfileLoginIpRange[] {profileLoginIpRange1} ;
List<MetadataService.SaveResult> results = metaDataPort.updateMetadata(new MetadataService.Metadata[] { existingProfile});
My expectation was that this code will add provided IP range into existing list. But after successful execution of this code, Result can be seen that all old IP ranges are vanished and only one will exist on Profile.
Is there any way so that I can achieve this functionality. I have one option to fetch existing IP ranges on Profile first and then add new one to that list. But I am looking for some cleaner solution similar to how we do it on Profile UI where only new IP would be added to list.