Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Autopopulate code

Posted on by 250

Hello everyone

Does anyone knows what is wrong with this code.

I want to autopopulate the addreses field by zip code.

function GetCEP(cep) {
//Nova variável "cep" somente com dígitos.
var cep = $(this).val().replace(/\D/g, '');

//Verifica se campo cep possui valor informado.
if (cep != "") {

//Expressão regular para validar o CEP.
var validacep = /^[0-9]{8}$/;

//Valida o formato do CEP.
if (validacep.test(cep)) {

//Consulta o webservice viacep.com.br/
$.getJSON("https://viacep.com.br/ws/" + cep + "/json/?callback=?", function (dados) {

if (!("erro" in dados)) {
//Atualiza os campos com os valores da consulta.
var aaddress1_line1 = Xrm.Page.getAttribute("aaddress1_line1").getValue();
var address1_county = Xrm.Page.getAttribute("address1_county").getValue();
var address1_stateorprovince = Xrm.Page.getAttribute("address1_stateorprovince").getValue();
var address1_upszone = Xrm.Page.getAttribute("address1_upszone").getValue();
} //end if.
else {
//CEP pesquisado não foi encontrado.
limpa_formulário_cep();
alert("CEP não encontrado.");
}
});
} //end if.
else {
//cep é inválido.
limpa_formulário_cep();
alert("Formato de CEP inválido.");
}
} //end if.
else {
//cep sem valor, limpa formulário.
limpa_formulário_cep();
}
}

function limpa_formulário_cep() {
// Limpa valores do formulário de cep.
var aaddress1_line1 = Xrm.Page.getAttribute("aaddress1_line1").getValue();
var address1_county = Xrm.Page.getAttribute("address1_county").getValue();
var address1_stateorprovince = Xrm.Page.getAttribute("address1_stateorprovince").getValue();
var address1_upszone = Xrm.Page.getAttribute("address1_upszone").getValue();
}

*This post is locked for comments

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Autopopulate code

    1. Verify all the attribute names used in script for its correctness

    2. When you register the event handler for onchange in field properties, don’t put () just keep function name GetCEP

    3. If you are registering field onchange on form load using addOnChange method then make sure of passing parameter correctly when triggering using fireOnChange. You can read the value from attribute always inside the method rather passing parameter

    4. Debug the code/use try catch block

  • TaniSantos Profile Picture
    TaniSantos 250 on at
    RE: Autopopulate code

    and also is giving an alert saying the following message:

    TypeError: Cannot read property 'Initialize' of undefined at eval (eval at RunHandlerInternal (crmprojeto.crm2.dynamics.com/.../ClientApiWrapper.aspx), <anonymous>:1:27)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans