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 :
Microsoft Dynamics CRM (Archived)

Cannot read property 'addPreSearch' of null at Object.attributeOnChange

(0) ShareShare
ReportReport
Posted on by 297

Hello Everyone!

Hope all of you are happy. A problem has occured in my JS web resource. Web resource will activate of change of an attribute field. This is about filtering lookup field on a specific condition when another attribute value change. Following error occurs when i change attribute value:

" Cannot read property 'addPreSearch' of null at Object.attributeOnChange "

Following is my JS web resource code. Can anyone tell me how to resolve this issue?


// a unique namespace for your libraries
var Sdk = window.Sdk || {};
(function () {

// Code to run in the form OnLoad event
this.attributeOnChange = function (executionContext) {
var formContext = executionContext.getFormContext();
console.log("after getting form context");
formContext.getControl("ownerid").addPreSearch(function(){
Sdk.addCustomLookupFilter});
}

function addCustomLookupFilter() {
console.log("entered in filter method");
var formContext = executionContext.getFormContext();
var obj = formContext.getAttribute("cms_committee");
var committeeName = obj.getAttribute("cms_name").getValue();
console.log(committeeName);
var committeeId = obj.getAttribute("cms_committeeid").getValue();
console.log(committeeId);
// Only show accounts with the type 'Preferred Customer'
var customerAccountFilter = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+
" <entity name='systemuser'>"+
" <attribute name='fullname' />"+
" <attribute name='systemuserid' />"+
" <order attribute='fullname' descending='false' />"+
" <link-entity name='cms_committee_member' from='createdby' to='systemuserid' link-type='inner' alias='aa'>"+
" <filter type='and'>"+
" <condition attribute='cms_usermember' operator='not-null' />"+
" <condition attribute='cms_committeeid' operator='eq' uiname='"+committeeName+"' uitype='cms_committee' value='{"+committeeId+"}' />"+
" </filter>"+
" </link-entity>"+
" </entity>"+
"</fetch>";
console.log("after query");
formContext.getControl("ownerid").addCustomFilter(customerAccountFilter, "systemuser");
console.log("after adding filter");
}

}).call(Sdk);

*This post is locked for comments

I have the same question (0)
  • gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Make sure  ownerid field is available in the forms . The error you are getting as control getting null as a result you can not get addPreSearch method from control object.

    So add the owner field in the forms.

    In addition if the field is available in the header area you should write like below -

    formcontext.getControl(‘header_ ownerid’)

  • Suggested answer
    crm development Profile Picture
    870 on at

    Hi,

    Have you passed execution context as first parameter option in the Handler Properties? Check below link. If not, you have to do it on change of an attribute field event handler properties.

    docs.microsoft.com/.../clientapi-execution-context

    After that check if formContext and formContext.getControl("ownerid") are not null. Also add below code before calling addPreSearch function

    if (formContext.getControl("ownerid") != null || formContext.getControl("ownerid") != undefined)

    {

    formContext.getControl("ownerid").addPreSearch(function(){

    Sdk.addCustomLookupFilter(executionContext)});

    }

    Further, code will fail as you have not passed executionContext to addCustomLookupFilter() function.

    It should be in all place --> addCustomLookupFilter(executionContext)

    Hope this helps.

    Thanks,

    Anand

    Please mark answer as verified if it is helpful.

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    this part is done but in "addCustomLookupFilter" method i want to get lookup field and its name and id so now i am getting name but id come with "undefined" error. So can you tell me how to resolve this?

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    Can you tell me is my query correct?

    i want that such committee member which contain user and committee value matches with committee field of my form so those users fetched which are in committee members are fetched and add to filter.

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    No doubt "header_ownerid" was answer but logical name is just "ownerid" when i open solution and look metadata of task entity so i didnot see anywhere "header_ownerid" , i just saw "ownerid"

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Try with this  -

                // Attach belpw method in onchange of the field
                function attributeOnChange(executionContext) {
                    var formContext = executionContext.getFormContext();
                    formContext.getControl("ownerid").addPreSearch(function () {
                        addCustomLookupFilter(formContext);
                    });
                }
    
                function addCustomLookupFilter(formContext) {
                    console.log("entered in filter method");
                    var formContext = executionContext.getFormContext();
                    var committeeName = formContext.getAttribute("cms_committee").getValue()[0].name; // Assuming cms_committee field is lookup field
                    console.log(committeeName);
                    var committeeId = formContext.getAttribute("cms_committee").getValue()[0].id;// Assuming cms_committee field is lookup field
                    console.log(committeeId);
    
                    if (formContext.getAttribute("ownerid") != null) {
                        customerAccountFilter =  "<filter type='and'>"+
                           "<condition attribute='cms_usermember' operator='not-null' />"+
                           "<condition attribute='cms_committeeid' operator='eq' uiname='"+committeeName+"' uitype='cms_committee' value='{"+committeeId+"}' />"+
                           "</filter>";
                        console.log("after query");
                        formContext.getControl("ownerid").addCustomFilter(customerAccountFilter);
                        console.log("after adding filter");
                    }
                }
  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    error comes that "systemuser" entity has no "cms_usermember" field.

    "cms_usermember" and "cms_committeeid" are fields of "committeemember" entity. i want such users which are in committee member and committee in committee member match which committee in task.

  • gdas Profile Picture
    50,091 Moderator on at

    what is entity type of lookup cms_committe ? is it user lookup ?

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    "cms_committee" is custom entity . its not user lookup

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at

    In "Task" form "cms_committe" is lookup field and there is also "Owner" field in form so i want when "cms_committe" change so js execute and fetch those users which are in "committee_member" entity and "committe_member" also has same "cms_committe" value which is in "Task" form. "Committee_member" has "cms_committee" and "User" lookup field.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans