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
Athira VenugopalAthira Venugopal 

Unable to insert an image into a custom object using apex code


I am working in Lightning web components.

HTML

<template>
<lightning:card>
<lightning-file-upload label="Attach receipt" name="fileUploader" accept={acceptedFormats} record-id={recordId} onuploadfinished={handleUploadFinished}> </lightning-file-upload>
<lightning-button label="Save" variant="brand" onclick={handleClick}></lightning-button>
</lightning:card>
</template>

js

import { LightningElement , wire, track, api} from 'lwc';
export default class PriceScreen extends LightningElement { fileName ;
@api recordId;
get acceptedFormats() { return ['.pdf', '.png','.jpg','.jpeg']; }

handleUploadFinished(event) {
// Get the list of uploaded files
  this.fileName =  event.detail.files[0].Id;  
}
handleClick(event) {
newClient({ photo:this.fileName })
.then(result =>
{ const evt = new ShowToastEvent({ title: 'Saved succesfully', variant: 'success', });
this.dispatchEvent(evt); }) 
.catch(error => { this.error = error; });
}

APEX

@AuraEnabled(cacheable = false)
public static Boolean newClient(String photo ){
Client__c client = new Client__c();
client.Photo__c = photo;
try { insert client; return true; }
catch (Exception e) {
throw new AuraHandledException('exceptionText' + e.getMessage()); } }

'Client' is a custom object, having a rich text area field 'Photo'. I am uploading the photo using lightning:file:upload. But the uploaded image is not getting saved into the custom object. Is there any mistake in my code? Actually, I don't know how to save an image into a custom object via apex code.
Tintu_BabuTintu_Babu
Hi,
A lightning-file-upload allows you to upload files to Attcahments /Files, I believe. You can store The urls in a rich text after uploading to these places and display using url whenver needed. 
But You can do it by Aura component .No equivalent is found in LWC in the component library
This provides an editor to the rich text lightning-input-rich-text
https://developer.salesforce.com/docs/component-library/bundle/lightning-input-rich-text/documentation