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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Web API createRecord ERROR: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

(0) ShareShare
ReportReport
Posted on by 283

I am trying to create a Text Message activity record via JS that is called when an HTML button is clicked. I've gotten most of the kinks worked out but now when I click the button I get the following error:

"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

I assume it has something to do with the object "data" that lists the values needed to create the record but there's not indexing on anything. Maybe it has to do with the object property "cdi_txtmessage_activity_parties", which is an array, but maybe I'm too novice to understand what the issue is.

window.parentExecutionContext = null;
    window.parentFormContext = null;

    function InitializeButton(executionContext) {
        // Assign executionContext and formContext to global variables within the web resource
        window.parentExecutionContext = executionContext;
        window.parentFormContext = executionContext.getFormContext();
    }

function smsResponse() {

    window.parentFormContext.data.entity.save;

    var userID = Xrm.Utility.getGlobalContext().userSettings.userId;
    console.log(userID);

    var contactID = window.parentFormContext.data.entity.getId();
    console.log(contactID);
    contactID = contactID.substring(1,contactID.length-1);
    console.log(contactID)

    var smsResponse = window.parentFormContext.getAttribute("new_smsresponse").getValue();
    console.log(smsResponse);

    var parties = [];
    var recipient = {};
    recipient["partyid_contact@odata.bind"] = "/contacts("   contactID   ")";
    recipient["participationtypemask"] = 2;
    parties.push(recipient);

    var data =
        {
            "subject": "SMS Reply",
            "cdi_txtmessage_activity_parties": parties,
            "ownerid@odata.bind": userID,
            "cdi_message": smsResponse,
            "regardingobjectid_contact@odata.bind": "/contacts("   contactID   ")"
        }

    console.log(data);

    // create account record
    Xrm.WebApi.createRecord("cdi_txtmessage", data).then(
    
    .....more code

I have the same question (0)
  • Verified answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    Hi Lucas,

    First you need to remove '{' and '}' from userID.

    userID= userID.substring(1,userID.length-1);

    Then correct the syntax to set owner of the record.

    "ownerid@odata.bind": "/systemusers(" + userID + ")",

  • Suggested answer
    Lucas H Profile Picture
    283 on at

    Well that fixed it! Don't know why I didn't realize I needed to do that with the userId as well as the contactId...

    Thank you so much!

  • Charan Raju C R Profile Picture
    7 Moderator on at

    Good to hear that.

    Please mark the post as suggested answer if it has helped to resolve your problem.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 73 Super User 2026 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#3
11manish Profile Picture

11manish 50

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans