2

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.

3

1 Answer 1

2

MetadataService class.

To leave old IP ranges untouched, you need to first read Profile metadata, retrieve old IP ranges (if existed) and add new ones to the list. Then new Profile should be instantiated, so no other data is overridden.

List<String> startRange = new List<String>{
        '0.0.0.0', '2.2.2.2'
};
List<String> endRange = new List<String>{
        '1.1.1.1', '3.3.3.3'
};

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
service.CallOptions = new MetadataService.CallOptions_element();
service.timeout_x = 120000;

List<MetadataService.Profile> profiles = (List<MetadataService.Profile>) service.readMetadata('Profile', new List<String>{'Chatter External User'}).getRecords(); //read Profile to get existing IP ranges

MetadataService.Profile profile = new MetadataService.Profile();
profile.fullName = 'Chatter External User';
if (profiles[0].loginIpRanges != null) {
    profile.loginIpRanges = profiles[0].loginIpRanges; //set existing IP ranges
} else {
    profile.loginIpRanges = new List<MetadataService.ProfileLoginIpRange>(); //if no IP ranges existed, add empty list
}

for (Integer i = 0; i < startRange.size(); i++) {
    MetadataService.ProfileLoginIpRange ipRange = new MetadataService.ProfileLoginIpRange();
    ipRange.startAddress = startRange[i];
    ipRange.endAddress = endRange[i];
    profile.loginIpRanges.add(ipRange); //add new IP ranges
}

List<MetadataService.SaveResult> saveResults = service.updateMetadata(new List<MetadataService.Profile>{profile});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.