web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Model Driven app looku...
Power Apps
Unanswered

Model Driven app lookup field validation using javascript web resource

(1) ShareShare
ReportReport
Posted on by 2
i have a work flow group form and when you click on each workflow group you can add resources to them. 
1 resource can be only added to 1 work flow group.
i have a client form where i have a resource allocation tab and in that i can add new resources to a client.
so, for example, if jay is assigned to workflow group 2 (WG2) than that resource cannot be added to workflow group 3 (WG3)
 
 

So in the Screenshot below i have added jay to be part of (WG3) but jay was initially added to (WG 2)
when user tries to save this they should get an error saying jay belongs to a wg2


I was told to use a javascript web resource to achieve this. This is the javascript i have got so far and it is triggering on save even of the form.
 
function validateResourceWorkflowGroup(executionContext) {
    var formContext = executionContext.getFormContext();
  
    // Get the selected Resource and Workflow Group values
    var resourceId = formContext.getAttribute("icando_resource").getValue();
    var workflowGroupId = formContext
      .getAttribute("icando_workflowgroup")
      .getValue();
      var fetchXml = `?fetchXml=
      <fetch mapping='logical'>
         <entity name='icando_clientresourceallocation'>
            <attribute name='icando_resource'/>
            <attribute name='icando_workflowgroup'/>
               <filter type='and'>
                  <condition attribute='icando_resource' operator='eq' value='`+resourceId[0].id+`'/>
               </filter>
         </entity>
      </fetch>`;
  
  Xrm.WebApi.retrieveMultipleRecords("icando_clientresourceallocation", fetchXml).then(
      function success(result) {
          for (var i = 0; i < result.entities.length; i++) {
              console.log(result.entities[i]);
          }                    
  
          // perform additional operations on retrieved records
      },
      function (error) {
          console.log(error.message);
          // handle error conditions
      }
  );

i am getting a scripting error for any resource i save. How can i do proper validation so that when a resource is added to a wrong workflow group users gets an error.

Categories:
I have the same question (0)
  • Suggested answer
    MichaelFP Profile Picture
    1,847 Super User 2025 Season 2 on at
    Model Driven app lookup field validation using javascript web resource
    I would recommend to no use that on save. In save you need to cancel the process before do it your validation.
     
    You must use the onChange() of the lookup field, and once the validation occurs you can set a error so the system will not allow the user to save the record because has error in the form validation. You can achieve that using AddNotification,
     
     
    you can add the notification of type error on the field and it can show a custom message and will block to save the record. it will be more cleaner e easy to manage that.
     
    If my answer helped you, please give me a thumbs up (👍). If solve your question please mark as answer ✔️. This is help the community.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 757 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 322 Super User 2025 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 209 Super User 2025 Season 2

Last 30 days Overall leaderboard