Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

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

(0) ShareShare
ReportReport
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
    25,208 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
    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
    25,208 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
    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
    25,208 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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 91

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans