function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhananjaya BulugahamulleDhananjaya 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; }

}
Best Answer chosen by Dhananjaya Bulugahamulle
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
List<LoginHistory > lst= 
[SELECT UserId, LoginTime , SourceIp ,NetworkId from LoginHistory where UserId =:userinfo.getuserid() order by LoginTime desc limit 1];
System.debug('---->'+lst);

NOTE:- Just order by your record with LoginTime and limit 1 in query , Then it will give you latest record.

User-added image

Please let us know if this will help you.

Thanks,
Amit Chaudhary

All Answers

surasura
you can access the LoginHistroy info from api. refer this link for example queires and available fields on loginhistroy object
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_loginhistory.htm
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
List<LoginHistory > lst= 
[SELECT UserId, LoginTime , SourceIp ,NetworkId from LoginHistory where UserId =:userinfo.getuserid() order by LoginTime desc limit 1];
System.debug('---->'+lst);

NOTE:- Just order by your record with LoginTime and limit 1 in query , Then it will give you latest record.

User-added image

Please let us know if this will help you.

Thanks,
Amit Chaudhary
This was selected as the best answer