Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

JavaScript throwing error "Web resource method does not exist"

(0) ShareShare
ReportReport
Posted on by 10

I have a javascript function which is calculating a score based on the data entered in a number of fields. I'm doing this by looping through all the fields on a form, checking the values for attributes and keeping a running score. When I publish the first example, I get no error and the data is right. When I publish the second, I get this error (screenshot below): "Web resource method does not exist: SetRelationshipStrength". I can't see anything syntaxually incorrect. Is there some limitation that I can't seem to find documented? I've encountered this mostly when there is a syntax issue in the script, but don't see that in this.

pastedimage1620247349119v1.png

Good Example

function SetRelationshipStrength(executionContext) {

  var formContext = executionContext.getFormContext(); // get the form context

  var score = 0;

  formContext.data.entity.attributes.forEach(function (attributeindex) {

    var attr = attribute.getName();
    var value = formContext.getAttribute(attr).getValue();

    if (formContext.getAttribute(attr).getAttributeType() === "optionset") {
      var value = formContext.getAttribute(attr).getText();
    }
    else {
      var value = formContext.getAttribute(attr).getValue();
    }

    switch (attr) {
      case "firstname":
        if (value != "" && value != null) {
          score = score + 2;
        }
        break;
      case "jobtitle":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;
      case "cre5e_jobtype":
        if (value != "" && value != null) {
          switch (value) {
            case "Technical":
              score = score + 2;
              break;
            case "Functional":
              score = score + 2;
              break;
          }
        }
        break;
      case "cre5e_keybusinessinterests":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;
      case "telephone1":
        if (value != "" && value != null) {
          score = score + 2;
        }
        break;/*
      case "emailaddress1":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;
    case "managername":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;
    case "cre5e_hasdirectreports":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;
    case "cre5e_favoritesport":
      if (value != "" && value != null) {?
        score = score + 3;
      }?
      break;
    case "cre5e_favoritefood":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;*/
    }

    /*if (value != "" && value != null && attr == "firstname") {
      score += 2;;
    }
    if (value != "" && value != null && attr == "jobtitle") {
     score += 5;
    }*/
  });

  Xrm.Page.getAttribute("cre5e_relstrengthtest").setValue(score);
  Xrm.Page.getAttribute("cre5e_relstrengthtest").setSubmitMode("always");
}
Bad Example
function SetRelationshipStrength(executionContext) {

  var formContext = executionContext.getFormContext(); // get the form context

  var score = 0;

  formContext.data.entity.attributes.forEach(function (attributeindex) {

    var attr = attribute.getName();
    var value = formContext.getAttribute(attr).getValue();

    if (formContext.getAttribute(attr).getAttributeType() === "optionset") {
      var value = formContext.getAttribute(attr).getText();
    }
    else {
      var value = formContext.getAttribute(attr).getValue();
    }

    switch (attr) {
      case "firstname":
        if (value != "" && value != null) {
          score = score + 2;
        }
        break;
      case "jobtitle":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;
      case "cre5e_jobtype":
        if (value != "" && value != null) {
          switch (value) {
            case "Technical":
              score = score + 2;
              break;
            case "Functional":
              score = score + 2;
              break;
          }
        }
        break;
      case "cre5e_keybusinessinterests":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;
      case "telephone1":
        if (value != "" && value != null) {
          score = score + 2;
        }
        break;
      case "emailaddress1":
        if (value != "" && value != null) {
          score = score + 1;
        }
        break;/*
    case "managername":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;
    case "cre5e_hasdirectreports":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;
    case "cre5e_favoritesport":
      if (value != "" && value != null) {?
        score = score + 3;
      }?
      break;
    case "cre5e_favoritefood":
      if (value != "" && value != null) {?
        score = score + 2;
      }?
      break;*/
    }

    /*if (value != "" && value != null && attr == "firstname") {
      score += 2;;
    }
    if (value != "" && value != null && attr == "jobtitle") {
     score += 5;
    }*/
  });

  Xrm.Page.getAttribute("cre5e_relstrengthtest").setValue(score);
  Xrm.Page.getAttribute("cre5e_relstrengthtest").setSubmitMode("always");
}
  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Hi, 

    Sorry I didn't see your last post.

    No, the trial versions give you all the capabilities to test the product properly, including the development of web resources.

    Yes, special characters can be one of the causes too...that's why I asked which code editor you are using on one of the above posts.

    Anyway I'm glad that you managed to find a logical explanation for your problem.

  • Suggested answer
    Zach Wells Profile Picture
    Zach Wells 10 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Closing as I figured it out. There were some special characters hidden in the cases that failed. Either replacing them with a proper space or rekeying the case worked. My guess is the editor for comments does this by default which is why those helping had no issue.

  • Zach Wells Profile Picture
    Zach Wells 10 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    One thing I didn't add. This is in a trial environment. Would that make a difference? Again, I can't find anything documented about limitations, but I guess possible.

  • Zach Wells Profile Picture
    Zach Wells 10 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    I originally did it in Notepad++ given how simple it was. I did the most recent in VS Code.

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Hi Zach,

    I did the same thing on my environment. I don't get the error as you do. Which editor do you use to write your code?

  • Zach Wells Profile Picture
    Zach Wells 10 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Here they are. I have a few more debugging things to try with rearranging cases to identify a bit more closely where the issue might be.

    pastedimage1620318377382v4.png

    Form Properties

    pastedimage1620318005207v1.png

    Header Resource Content

    pastedimage1620318046636v3.png

    Handler Properties

    pastedimage1620318023808v2.png

  • meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Hi Zach,

    I just checked on my side and there is no problem with the web resource. The only thing left to do is to check your configuration for the Handler events. Could you please share a screenshot of your configuration. 

  • Zach Wells Profile Picture
    Zach Wells 10 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Thanks Mehdi - I did was you suggested and receive the exact same error. I removed all comments frmo the switch statement so all cases were in the new web resource. Just for reference, added the code below. As for Xrm.Page, I planned to update that. Just got hung up on this error before getting to the end of the code. I did validate the form is using the new web resource and the function name was copied and pasted. The below is the entirety of the js file.

    function SetRelationshipStrength(executionContext) {

     var formContext = executionContext.getFormContext(); // get the form context

     var score = 0;

     formContext.data.entity.attributes.forEach(function (attribute, index) {

       var attr = attribute.getName();

       var value = formContext.getAttribute(attr).getValue();

       if (formContext.getAttribute(attr).getAttributeType() === "optionset") {

         var value = formContext.getAttribute(attr).getText();

       }

       else {

         var value = formContext.getAttribute(attr).getValue();

       }

       switch (attr) {

         case "firstname":

           if (value != "" && value != null) {

             score = score + 2;

           }

           break;

         case "jobtitle":

           if (value != "" && value != null) {

             score = score + 1;

           }

           break;

         case "cre5e_jobtype":

           if (value != "" && value != null) {

             switch (value) {

               case "Technical":

                 score = score + 2;

                 break;

               case "Functional":

                 score = score + 2;

                 break;

             }

           }

           break;

         case "cre5e_keybusinessinterests":

           if (value != "" && value != null) {

             score = score + 1;

           }

           break;

         case "telephone1":

           if (value != "" && value != null) {

             score = score + 2;

           }

           break;

         case "emailaddress1":

           if (value != "" && value != null) {

             score = score + 1;

           }

           break;

       case "managername":

         if (value != "" && value != null) {​

           score = score + 2;

         }​

         break;

       case "cre5e_hasdirectreports":

         if (value != "" && value != null) {​

           score = score + 2;

         }​

         break;

       case "cre5e_favoritesport":

         if (value != "" && value != null) {​

           score = score + 3;

         }​

         break;

       case "cre5e_favoritefood":

         if (value != "" && value != null) {​

           score = score + 2;

         }​

         break;

       }

       /*if (value != "" && value != null && attr == "firstname") {

         score += 2;;

       }

       if (value != "" && value != null && attr == "jobtitle") {

        score += 5;

       }*/

     });

     Xrm.Page.getAttribute("cre5e_relstrengthtest").setValue(score);

     Xrm.Page.getAttribute("cre5e_relstrengthtest").setSubmitMode("always");

    }

  • Suggested answer
    Torrado Profile Picture
    Torrado 1,270 on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Hello,

    I compared both versions in Visual Code, tried each one individually, and they are also working for me. Please try again with a web resource created from scratch.

    Please avoid Xrm.Page functions. When developing for v9 you should follow the new Client API documentationhttps://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference 

    The Visual Code extension suggested by Mehdi can help you on using only supported functions since it's based on the JavaScript code snippets for Dynamics 365 Customer Engagement v9.

    Hope this helps you.

    Please mark the answer as verified if helpful. That will help others in future.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: JavaScript throwing error "Web resource method does not exist"

    Hi, 

    I have copied your code, it works without any problem for me. The problem is not about your function, I think the problem is about the
    whole web resource.

    I suggest that you create a new web resource that will contain the SetRelationshipStrength function only. 

    Another thing, avoid using Xrm.Page which is deprecated. You should use the following methods:

    • formContext.getAttribute('fieldName').setValue(value);
    • formContext.getAttribute('fieldName').setSubmitMode('always');

    There is an extension on Visual Studio Code that will help you to use the latest version of the client API: 

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans