Skip to main content

Notifications

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

  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    Hi,

    Based on what you have mentioned and my understanding, You cannot use addCustomFilter for this scenario. This is because you can only add filter which are directly related to the entity whereas in your case you want to retrieve the users from the related committee i.e. Committee >> Committee Members  >> User Member.  You can refer the below thread on the same -

    community.dynamics.com/.../119649

    You can however use traditional AddCustomView method to set the custom view but again I am in doubt as the field is Owner (and not just normal lookup field). You can try and see if it works.

    madcomputerist.blogspot.com/.../adding-additional-condition-to-lookup.html

    Also, as you have already marked this as an answered, I would suggest to create a new thread for your new questions/ issues. If you haven't got the answer yet for this then unmark the verified answer so that it is visible to other as well as pending answer.

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    So what I ask you earlier that "committe" field should be user lookup if my understanding is correct . So you can simply write filter like this -

    customerAccountFilter =  "<filter type='and'>"+                

                         "<condition attribute='systemuserid' operator='eq' value= '{"+committeeId+"}'/>"+

                         "</filter>";

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    i want to filter "systemuser" based on "committee" field which is lookup in task form. I explain you in detail. Task entity has "committee" lookup field and my js web resource will execute when "committee" lookup field will change in task form. So "committee_member" has user lookup field and also "committee" lookup field. Now i want to fetch all "users" which are in "committee_member" and that "committee_member" has same lookup field of "committee" which is in "Task" form "committee" lookup field.

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    Yes i am trying to filter owner field on the task form base on "committee" lookup field of task form. "committee_member" has "user" and "committee" lookup field so i want to fetch all users which are in "committee_member" and that committe member has same "committee" which is in task form .

  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    Can you explain more on "fetch those users which are in "committee_member"". And are you trying to filter owner field on the task form?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    Think like normal query , you are filtering system user table/Lookup based on which field ? is it same name user which exists name  in cms_committe field   ?

    In that case you need to put filter criteria with fields exists in system user entity like below -

     customerAccountFilter =  "<filter type='and'>"+                

                          "<condition attribute='fullname' operator='eq' value='"+committeeName+"' />"+

                          "</filter>";

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    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.

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    "cms_committee" is custom entity . its not user lookup

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

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

  • Microsoft Dynamics 365 Consultancy Profile Picture
    297 on at
    RE: Cannot read property 'addPreSearch' of null at Object.attributeOnChange

    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.

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 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…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans