How to add "last_ip" filed in a doctype

Apologies in advance it this topic has already been asked / discussed elsewhere (as neither searching here nor in google gave me any clue about it) , so here I go …

I want to add a field “Last IP” as we see it in “User” Doctype in , say “Material Request” in ERPNext.

I added that filed - exactly as it is in “User” - but did not get my IP registered as it does in User DT.

What is being missed here ?

Thank you.

Hi @quqnuss:

You will need some logic for this, with custom app writing some python code, AFAIK there is no way from server script.

But you can use a trick on client side:

Add the ip address field (i assume it is custom_ip_address)
Create a client script for Material Request, enable it.

frappe.ui.form.on('Material Request', {
	refresh(frm) {
        getIPAddress().then(ip => {
            if (ip) {
                frm.set_value("custom_ip_address", ip);
                frm.refresh_field("custom_ip_address");
            } else {
                console.error('Failed to fetch IP address');
            }
        });
	}
})

function getIPAddress() {
    return fetch("https://checkip.amazonaws.com/")
        .then(res => res.text())
        .then(data => data.trim())
        .catch(error => {
            console.error('Error fetching IP address:', error);
            return null; 
        });
}

I am using Amazon service, but there are a lot more …
Hope this helps.

1 Like

Thanks a lot for the reply.

We use ERPN in our local LAN where we need to log local IP addresses like 192.168., which of course cannot be gotten via Amazone you’ve mentioned.
I am wondering how it is used in User doctype why it can’t be used elsewhere !

But your code was definitely taught me something new :slight_smile: