web
You’re offline. This is a read only version of the page.
close
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
I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    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

  • preetham716 Profile Picture
    10 on at

    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,210 on at

    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

    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

  • Suggested answer
    cloflyMao Profile Picture
    25,210 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans