Zip Code API (pin code)

Hello,

I created a form called: Address

With the fields:

zip_code
street
neighborhood
city
state

So I created a client script:

frappe.ui.form.on(‘Address’, {
zip_code: function(frm) {
if(frm.doc.zip_code) {
frappe.call({
type: “GET”,
async: true,
url: “https://brasilapi.com.br/api/cep/v1/” + frm.doc.zip_code,
callback: function(response) {
if(response.message) {
// Aqui você pode usar os dados retornados pela API
// Por exemplo, você pode preencher os campos do seu formulário com os dados retornados
frm.set_value(“zip_code”, response.message.cep);
frm.set_value(“street”, response.message.street);
frm.set_value(“city”, response.message.city);
frm.set_value(“state”, response.message.state);
frm.set_value(“neighborhood”, response.message.neighborhood);
return
}
}
});
}
}
});

He performed the query and brought the result but it did not display in the fields

I apologize in advance for anything, I’m new here, and I’m learning little by little

Hi @weldonet:
API is not answering message, so …

frappe.ui.form.on('Address', {
    zip_code: function(frm) {
        if(frm.doc.zip_code) {
            frappe.call({
                type: 'GET',
                async: true,
                url: 'https://brasilapi.com.br/api/cep/v1/' + frm.doc.zip_code,
                callback: function(response) {
                    if(response) {
                        // Aqui você pode usar os dados retornados pela API
                        // Por exemplo, você pode preencher os campos do seu formulário com os dados retornados
                        frm.set_value('zip_code', response.cep);
                        frm.set_value('street', response.street);
                        frm.set_value('city', response.city);
                        frm.set_value('state', response.state);
                        frm.set_value('neighborhood', response.neighborhood);
                        return
                    }
                }
            });
        }
    }
});

Hope this helps.
Cumprimentos! :wink:

1 Like

Thanks for the support!

I took what it did and supplemented it with a few more functions, but it’s still not working.

It does not fill in the form inputs

frappe.ui.form.on('Address', {
    zip_code: function(frm) {
        if(frm.doc.zip_code) {
            var cep = frm.doc.zip_code.replace(/\D/g, '');  // Remove caracteres especiais
            if(cep.length === 8) {
                frappe.call({
                    type: "GET",
                    async: true,
                    url: "https://brasilapi.com.br/api/cep/v1/" + cep,
                    
                    success: function(response) {
                        frm.set_value("zip_code", response.cep);
                        frm.set_value("street", response.street);
                        frm.set_value("city", response.city);
                        frm.set_value("state", response.state);
                        frm.set_value("neighborhood", response.neighborhood);
                        return
                    },
                    error: function() {
                        frappe.msgprint("CEP não encontrado");
                    },
                    always: function() {
                        console.log("Request completed");
                    },
                    freeze: false,
                    freeze_message: "Aguardando"
                });
            }
        }
    }
});

Hi @weldonet :

Use callback instead success . This should work.

1 Like

@avc thanks

I made the modification you suggested and it was perfect!

frappe.ui.form.on(‘Address’, {
zip_code: function(frm) {
if(frm.doc.zip_code) {
var cep = frm.doc.zip_code.replace(/\D/g, ‘’); // Remove caracteres especiais
if(cep.length === 8) {
frappe.call({
type: “GET”,
async: true,
url: “https://brasilapi.com.br/api/cep/v1/” + cep,
callback: function(response) {
if (response) {
frm.set_value(“zip_code”, response.cep);
frm.set_value(“street”, response.street);
frm.set_value(“city”, response.city);
frm.set_value(“state”, response.state);
frm.set_value(“neighborhood”, response.neighborhood);
} else {
frappe.msgprint(“CEP não encontrado”);
}
},
freeze: false,
freeze_message: “Aguardando”
});
}
}
}
});