function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
User SethUser Seth 

How to Update Contact record from LWC

One component is publishing the id of the contact and Other component needs to Update Account Id in the the Contact to Null based on the Id Published through Lightning Message Service.
======================================================
Html File:::
<template>
  <lightning-card title="Record view from for Account">
    <lightning-layout>
      <lightning-layout-item>
        <lightning-record-view-form
          record-id={contacts}
          object-api-name="Contact"
        >
          <lightning-output-field
            field-name="FirstName"
          ></lightning-output-field>
          <lightning-output-field
            field-name="LastName"
          ></lightning-output-field>
          <lightning-output-field
            field-name="AccountId"
          ></lightning-output-field>
          <lightning-output-field
            field-name="LastModifiedById"
          ></lightning-output-field>
        </lightning-record-view-form>
      </lightning-layout-item>
    </lightning-layout>
  </lightning-card>
</template>
======================================================
======================================================
 
JS File::::
import { LightningElement, wire } from "lwc";
import UpdateContact from "@salesforce/apex/AccountContactController.updateContact";
import {
  subscribe,
  unsubscribe,
  MessageContext
} from "lightning/messageService";
import SimpleChannel from "@salesforce/messageChannel/SimpleChannel__c";
export default class DisplayContact extends LightningElement {
  subscription = null;
  strCapturedText;
  @wire(MessageContext) messageContext;
  @wire(UpdateContact, { contactId: "$strCapturedText" }) contacts;
  subscribeToMessageChannel() {
    if (!this.subscription) {
      this.subscription = subscribe(
        this.messageContext,
        SimpleChannel,
        (message) => this.setCaptureText(message)
      );
    }
  }
  unsubscribeToMessageChannel() {
    unsubscribe(this.subscription);
    this.subscription = null;
  }
  connectedCallback() {
    this.subscribeToMessageChannel();
  }
  // This method will run once the component is removed from DOM.
  disconnectedCallback() {
    this.unsubscribeToMessageChannel();
  }
  // This method will update the value once event is captured.
  setCaptureText(message) {
    this.strCapturedText = message.data;
  }
}
============================================================================================================
 
Apex Method:
@AuraEnabled(cacheable=true)
    public static Id updateContact(string contactId){
        Contact c = [select Id,AccountId from Contact where Id =: contactId];
        c.AccountId =Null;
        update c;
        return c.Id;
    }
======================
 I am getting the data from the publish component and able to show the data What I receive  but when I want to Update with the help of Apex Method it is not working and not showing any data
==============
 
Can anyone suggest me how to resolve this
@ M  Coder@ M Coder
may i know what is the error you are getting , and one more thing what is your scenario ????
if you can mention we can help