we want to get the source IP field from login history object. We do not know how to get it. We can get the IP but the system randomly chooses a one. Does anybody have any idea? thanks
trigger ProductBriefHistory on Product_Brief__c (after update) {
List<Schema.FieldSetMember> trackedFields = SObjectType.Product_Brief__c.FieldSets.PBHistoryTracking.getFields();
if (trackedFields.isEmpty()) return;
LoginHistory src = [SELECT SourceIp from LoginHistory WHERE UserId =:UserInfo.getUserId() Limit 1];
List<Product_Brief_History__c> fieldChanges = new List<Product_Brief_History__c>();
List<string> apiNameList = new List<string>();
if(Trigger.isUpdate){
for (Product_Brief__c aNew : trigger.new) {
Product_Brief__c aOld = trigger.oldmap.get(aNew.Id);
for (Schema.FieldSetMember fsm : trackedFields) {
String fieldName = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();
if (aNew.get(fieldName) != aOld.get(fieldName)) {
String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255);
Product_Brief_History__c aht = new Product_Brief_History__c();
aht.Product_Brief__c = aNew.Id;
aht.name__c = fieldLabel;
aht.apiName__c = fieldName;
aht.Updated_By__c = UserInfo.getUserId();
aht.OldValue__c = oldValue;
aht.NewValue__c = newValue;
aht.Source_IP__c = src.SourceIp;
apiNameList.add(aht.apiName__c);
fieldChanges.add(aht);
}
}
}
}
if (!fieldChanges.isEmpty()) {
insert fieldChanges;
}
}