Skip to main content

Notifications

Announcements

No record found.

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.

Posted on by 279

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

  • Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Super User 2024 Season 1 on at
    RE: Web API createRecord ERROR: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

    Good to hear that.

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

  • Suggested answer
    Lucas H Profile Picture
    Lucas H 279 on at
    RE: Web API createRecord ERROR: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

    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!

  • Verified answer
    Charan Raju C R Profile Picture
    Charan Raju C R 3,155 Super User 2024 Season 1 on at
    RE: Web API createRecord ERROR: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

    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 + ")",

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

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans