Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript addpresearch(function) not calling and not showing any error too

Posted on by 10
function Alertmethod(executionContext)
{
var formContext = executionContext.getFormContext(); // get formContext
formContext.getControl("preetham_refferedid").addPreSearch(addFilter);
}

function addFilter(executionContext)
{
var formContext = executionContext.getFormContext();
var department=formContext.getAttribute("preetham_departmentid").getValue();
if(department!=null)
{
department=department[0].id;
var fetchxml=" <filter type='and'>"+
"<condition attribute='preetham_departmentsid' operator='eq' value='"+department+"' />"+
"</filter>";
formContext.getControl("preetham_departmentid").addCustomFilter(fetchxml)
}
My code
I want filter lookup depends upon another lookup
  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Javascript addpresearch(function) not calling and not showing any error too

    Hi preetham,

    To set lookup field(preetham_refferdid) value, we should create an array object with id, name and entityType properties.

    Please modify your Xrm.WebApi.retrieveMultipleRecords function like below.

    Xrm.WebApi.retrieveMultipleRecords("preetham_doctors", fetchxml).then(
        function success(response) {
            for (var i = 0; i < response.entities.length; i  ) {
                var responseobj = response.entities[i];
                var lookup = new Array();
                lookup[0] = new Object();
                lookup[0].id = responseobj.preetham_doctorsid;
                lookup[0].name = responseobj.preetham_name;
                lookup[0].entityType = 'preetham_doctors';
                formcontext.getAttribute("preetham_refferedid").setValue(lookup);
            }
        },
        function (error) {
            Xrm.Utility.alertDialog("failed");
        }
    )

    Alternatively, it also works if we shorten them in single line.(Not declaring array and object.)

    formcontext.getAttribute("preetham_refferedid").setValue([{id: responseobj.preetham_doctorsid, name: responseobj.preetham_name, entityType:'preetham_doctors'}]);

    Regards,

    Clofly

  • preetham716 Profile Picture
    preetham716 10 on at
    RE: Javascript addpresearch(function) not calling and not showing any error too

    Addfilter is not triggering is ther any otherway to do it .

    i have tried using webapi

    function Alertmethod(executionContext)

    {

       var formContext = executionContext.getFormContext(); // get formContext

          debugger;

    var department_id=formContext.getAttribute("preetham_departmentid").getValue()[0].id

    var fetchxml="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+

        "  <entity name='preetham_doctors'>"+

        "    <attribute name='preetham_doctorsid' />"+

        "    <attribute name='preetham_name' />"+

        "    <attribute name='createdon' />"+

        "    <order attribute='preetham_name' descending='false' />"+

        "    <filter type='and'>"+

        "      <condition attribute='preetham_departmentsid' operator='eq' value='"+department_id+"' />"+

        "    </filter>"+

        "  </entity>"+

        "</fetch>"

        fetchxml="?fetchXml="+encodeURIComponent(fetchxml);

        Xrm.WebApi. retrieveMultipleRecords("preetham_doctors",fetchxml).then(

            function success(response)

            {

                for(var i=0; i<=response.entities.length; i++)

                {

                    var responseobj=response.entities[i];

                    formcontext.getAttribute("preetham_refferedid").setValue(responseobj.preetham_doctorsid)

                }

            },

            function(error)

            {

               Xrm.Utility.alertDialog("failed");

            }

        )

    }

    I am getting the data in response but i am not able to set to "preetham_refferdid"

    Any idea is i am doing correct or any suggestion

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Javascript addpresearch(function) not calling and not showing any error too

    You can insert debugger at line 1 of addFilter function, to check whether the addFilter is actually triggered by addPreSearch.

  • preetham716 Profile Picture
    preetham716 10 on at
    RE: Javascript addpresearch(function) not calling and not showing any error too

    Hi Clofy Mao

    Thats not the problem

    formContext.getControl("preetham_refferedid").addPreSearch(addFilter);

    in this addPreSearch should call addfilter function but its not calling addfilter its terminating .

    community.dynamics.com/.../845868

    I found this i am also using version 9.0

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Javascript addpresearch(function) not calling and not showing any error too

    Hi preetham,

    Maybe it caused by this line

    department=department[0].id

    It will get the record id and curly bracket, please remove the extra bracket using regex and test again.

    department[0].id.replace(/\{|\}/gi, "").toLowerCase();

    Regards,

    Clofly

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans