You need to sign in to do that
Don't have an account?
jishan roy
how to solve this error: Invalid conversion from runtime type Map<ANY,ANY> to Contact
I found this error on console log.
here is my code:
public with sharing class contactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(String accId) {
return [
SELECT AccountId, Id, FirstName, LastName, Title, Phone, Email
FROM Contact
WHERE AccountId = :accId
];
}
@AuraEnabled
public static void saveContacts(List<Object> listContact)
{
list<contact> conlist = new list<contact>();
for(Object a:listContact){
conlist.add((contact) a);
}
update conlist;
}
}
thanks in advance.
here is my code:
public with sharing class contactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(String accId) {
return [
SELECT AccountId, Id, FirstName, LastName, Title, Phone, Email
FROM Contact
WHERE AccountId = :accId
];
}
@AuraEnabled
public static void saveContacts(List<Object> listContact)
{
list<contact> conlist = new list<contact>();
for(Object a:listContact){
conlist.add((contact) a);
}
update conlist;
}
}
thanks in advance.
I dont see any issue in the apex class. There may be issue in the Aura or LWC component.
Thanks
here is my code lwc:
js:
import { LightningElement,api,wire,track } from 'lwc';
import getContacts from '@salesforce/apex/contactController.getContacts';
import saveContacts from '@salesforce/apex/contactController.saveContacts';
//import { ShowToastEvent } from 'lightning/platformShowToastEvent';
//import { refreshApex } from '@salesforce/apex';
//import { updateRecord } from 'lightning/uiRecordApi';
import ID_FIELD from '@salesforce/schema/Contact.Id';
import FIRST_NAME_FIELD from '@salesforce/schema/Contact.FirstName';
import LAST_NAME_FIELD from '@salesforce/schema/Contact.LastName';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
export default class contactAccountInfo extends LightningElement {
@api recordId;
@api errorMessage;
@track record;
@track error;
@track contactId;
@track firstName;
@track lastName;
@track phone;
// @api record= [{FirstName, index},{LastName, index},{Phone, index}];
fields = [LAST_NAME_FIELD,FIRST_NAME_FIELD, ID_FIELD,PHONE_FIELD];
@wire(getContacts, { accId: '$recordId' })
cons({error,data}){
console.log('recordId',this.recordId);
if (data) {
console.log('>>>data: ' + JSON.stringify(data));
this.record = data;
this.error = undefined;
console.log('recordId',this.record);
} else if (error) {
this.error = error;
this.data = undefined;
}
}
handleChange(event){
this.Phone = event.target.value;
let newArray = [...this.record];
let toUpdate = {
...newArray[event.currentTarget.dataset.index],
Phone: this.Phone
};
newArray[event.currentTarget.dataset.index] = toUpdate;
this.record = newArray;
console.log(this.record);
//this.record[event.target.value].onclick = event.target.update;
}
handleUpdate(event) {
saveContacts ({listContact: this.record})
.then(()=>{
})
.catch((error)=>{
this.error=error;
console.log(error);
})
};
}
html:
<template>
<lightning-card title="Update Contacts on Account Page" custom-icon="custom:icon13">
<table class="slds-table slds-table_cell-buffer slds-table_bordered" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
<template if:true={record}>
<tr>
<td><b>FirstName</b></td>
<td><b>LastName</b></td>
<td><b>Phone</b></td>
</tr>
<template for:each={record} for:item="acc" for:index="index">
<tr key={acc.Id}>
<td><lightning-input label="First Name" value={acc.FirstName} data-field="FirstName" onchange={handleChange} class="slds-m-bottom_x-small" readonly="true"></lightning-input></td>
<td><lightning-input label="Last Name" value={acc.LastName} data-field="LastName" onchange={handleChange} class="slds-m-bottom_x-small" readonly="true" required></lightning-input></td>
<td><lightning-input label="Phone" name="phone" value={acc.Phone} data-field="Phone" data-index ={index} onchange={handleChange} class="slds-m-bottom_x-small" ></lightning-input></td>
</tr>
</template>
<lightning-button label="update" variant="brand" onclick={handleUpdate}></lightning-button>
<!-- <lightning-button label="update" variant="brand" onclick={handleUpdate}></lightning-button> -->
</template>
</table>
</lightning-card>
</template>
thanks in avvance,