Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue setting lookup field value from DynamicForm onBeforeSubmit method #1915

Open
EdAlexander opened this issue Dec 8, 2024 · 2 comments
Open

Comments

@EdAlexander
Copy link

[x] Question using release 3.19.0

I am attempting to set the value of a Lookup field in the onbeforesubmit method invoked by a DynamicForm control. When I try to create a new list item from the dynamic form the onbeforesubmit is intended to set the value of a lookup field to a specific Id.

component from render:

<DynamicForm webAbsoluteUrl={'https://xxxxxx.sharepoint.com/sites/Intranet'}
                    listId={"<list guid>"}
                    onCancelled={() => { this.setState({ showNewTask: false }) }}
                    onBeforeSubmit={this._newTaskOnBeforeSubmit}
                    onSubmitError={(listItem, error) => { alert(error.message); }}
                    onSubmitted={async (listItemData) => {
                      this._closeTaskPanel();
                    }}
                    context={this.props.context as any}
                    hiddenFields={["Case"]}
                  />

method I am testing:

private _newTaskOnBeforeSubmit = async (listItemData: any): Promise<boolean> => {
    try {
      console.log("In _newTaskOnBeforeSubmit...")
      console.log(this.state.selectedCaseId)
      const usethisId: number = this.state.selectedCaseId;
      listItemData.Case = {
        "__metadata": { "type": "SP.FieldLookupValue" },
        "LookupId": usethisId
      };
      return false; 
    }
    catch (err) {
      alert(err);
      return true;
    }
  }

The "Case" field is a simple lookup to another list in the same web and was created with default Title option. Works fine in sharepoint online out of the box forms.

When I use the DynamicForm to create a new list item I get the following error when clicking [Save]:
Error making HttpClient request in queryable [400] ::> {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Incompatible type kinds were found. The type 'SP.FieldLookupValue' was found to be of kind 'Complex' instead of the expected kind 'Entity'."}}}

image

Copy link

github-actions bot commented Dec 8, 2024

Thank you for submitting your first issue to this project.

@EdAlexander
Copy link
Author

I think I was able to get this working.... In case it helps someone else in the same boat here is my updated code

 private _newTaskOnBeforeSubmit = async (listItemData: any): Promise<boolean> => {
    try {
      console.log("In _newTaskOnBeforeSubmit...")
      console.log(this.state.selectedCaseId)
      const usethisId: number = this.state.selectedCaseId;

      // const lookupFieldValue = {
      //   '__metadata': { 'type': 'SP.FieldLookupValue' },
      //   'LookupId': usethisId
      // };
    
      const lookupFieldValue = {
        'LookupId': usethisId
      };
    
      listItemData.Case = lookupFieldValue;

      return false; //continue with save.
    }
    catch (err) {
      console.log("Error creating new task:");
      console.log(err);
      alert(err);
      return true;
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant