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

Calling Custom Action in MS CRM in Html page

(0) ShareShare
ReportReport
Posted on by 233

Hello Experts,

I have used the Below code for passing parameters to the custom action , am getting Unexpected token u in JSON at position 0,please suggest me regarding this

<html>

<head>
    <title></title>
    <script src="../../ClientGlobalContext.js.aspx"></script>
    <link rel="stylesheet" href="">maxcdn.bootstrapcdn.com/.../bootstrap.min.css">

</head>

<body onload="setFocusOnComments()" style="overflow-wrap: break-word;">
    <h5>Security Role Manager</h5>
    <div class="container-fluid">


        <div class="row">
            <div class="col-md-6">
                <label for="User_Type">Type Of Users:</label>
            </div>
            <div class="col-md-6">
                <select id="User_Type" name="User_Type">
                    <option value="International">International</option>
                    <option value="Local">Local</option>
                </select>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <label class="Userlabel" for="Application_Type">Application Type:</label>
            </div>
            <div class="col-md-6">
                <select id="Application_Type" name="Application_Type">
                    <option value="KAM Tool">KAM Tool</option>
                    <option value="Sales Mobile">Sales Mobile</option>
                </select>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12"><button type="button" class="btn-primary" onclick="onSubmit()">Assign Roles</button>
            </div>
        </div>


    </div>
    <script>
        function setFocusOnComments() {

        }
        function GetURLParameter(sParam) {
            var sPageURL = window.location.search.substring(1);
            var sURLVariables = sPageURL.split('&');
            for (var i = 0i < sURLVariables.lengthi++) {
                var sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] == sParam) {
                    var vals = new Array();
                    vals = decodeURIComponent(sParameterName[1]).split("&");
                    for (var i in vals) {
                        vals[i] = vals[i].replace(/\+/g" ").split("=");
                    }
                    return vals;
                }
            }
        }

        function onSubmit() {
            debugger;
            var queryStrings = GetURLParameter('data');

            var id = queryStrings[0][1];

            var userType = document.getElementById("User_Type");
            var ghi_usertype = userType.options[userType.selectedIndex].value;

            var appType = document.getElementById("Application_Type");
            var ghi_applicationname = appType.options[appType.selectedIndex].value;

            var target = { entityType: "systemuser"id: id };
            var reqObject = {};
            reqObject.entity = target;
            reqObject.UserType = ghi_usertype.toString();
            reqObject.ApplicationType = ghi_applicationname.toString();

            reqObject.getMetadata = function () {
                return {
                    boundParameter: "entity",
                    operationType: 0,
                    operationName: "ghi_AssignSecurityRolefromSecurityManager",
                    parameterTypes: {
                        "entity": {
                            "typeName": "mscrm.systemuser",
                            "structuralProperty": 5
                        },
                        "UserType": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1
                        },
                        "ApplicationType": {
                            "typeName": "Edm.String",
                            "structuralProperty": 1
                        }
                    }

                }
            };

            window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
                "systemuser": "systemusers",
            });

            Xrm.WebApi.online.execute(reqObject).then(
                function success(result) {
                    debugger;
                    console.log(result'result');
                    if (result.ok) {
                        console.log(result'result11');
                    }
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                }
            );

        }
    </script>

</body>


</html>
I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Amrutha,

    I would suggest you check the following points:

    1. Whether name of the action you are calling is correct, it should be the "Unique Name" value of the action.

    2. Whether name of input parameters are correct.

    z1.JPG

    3. Whether ID extracted from URL is correct.

    var id = queryStrings[0][1];

    (It is should be something like 0215534a-0d42-40e9-8a0a-c973bb6bfc8f.)

    Try troubleshooting by following operations:

    1. Call the action directly with static value to test whether it would execute successfully.

    2. Replace Xrm.Utility.alertDialog(error.message) with console.log(error), the error object may contains more error details.

    3. Clear browser cache.

    In addition, I think the reason would be that you were running web resource, the location of web resource is child of form.

    You could change window.location.search to parent.window.location.search to check whether the URL parameters could be searched.

    z2.JPG

    (And due to the reason, replace Xrm.WebApi with parent.Xrm.WebApi if necessary.)

    Regards,

    Clofly

  • Amrutha B K Profile Picture
    233 on at

    Hi Clofly,

    I tried the above operation what u mentioned still same error "SyntaxError: Unexpected token u in JSON at position 0↵    at JSON.parse (<anonymous>)↵    at Function.XrmClientApi.WebApi.Serialization.ODataSerializer.getPrimaryKey"

  • cloflyMao Profile Picture
    25,210 on at

    Hi Amrutha,

    Please refer to following links:

    https://community.dynamics.com/crm/b/jasonlattimersblog/posts/use-xrm-webapi-in-a-stand-alone-web-resource

    https://stackoverflow.com/questions/51416490/using-xrm-webapi-method-in-web-resource-opened-in-a-new-window/

    In addition to answers from Jason Lattimer and the StackOverflow question:

    From my test, I removed ClientGlobalContext.js.aspx and following part of code:

    window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
                    "systemuser": "systemusers",
                });

    It works for me.

    You could remove them if unnecessary.

    Regards,

    Clofly

  • Suggested answer
    Pawar Pravin  Profile Picture
    5,237 on at

    Hi Amruta,

    First thing, you are trying with unsupported way to get field values in html web resource.

    I will suggest you to use form context to get field values, please refer below url for reference:

    debajmecrm.com/.../

    Also try with parent before Xrm library as below:

    parent.Xrm.WebApi.online.execute

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 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans