You need to sign in to do that
Don't have an account?
Dhananjaya Bulugahamulle
How to get the source IP field?
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 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 fieldChanges = new List();
List apiNameList = new List();
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; }
}
trigger ProductBriefHistory on Product_Brief__c (after update) { List 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 fieldChanges = new List();
List apiNameList = new List();
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; }
}
NOTE:- Just order by your record with LoginTime and limit 1 in query , Then it will give you latest record.
Please let us know if this will help you.
Thanks,
Amit Chaudhary
All Answers
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_loginhistory.htm
NOTE:- Just order by your record with LoginTime and limit 1 in query , Then it will give you latest record.
Please let us know if this will help you.
Thanks,
Amit Chaudhary