You need to sign in to do that
Don't have an account?

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
======================================================
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
if you can mention we can help