[SalesForce] How to pass one field value in an object to an other object? (LWC)

I have an object in which i enter an email and I have an other object in which I want that email to display by default without having to rewrite it (same email for same person) .
as you can see in the picture.

   <lightning-input-field   field-name='Candidat__c' onchange={handleCandidatChange}   > </lightning-input-field>
   <lightning-input-field   field-name='Email_de_Candidat__c' value='Candidat__r.Email' onchange={handleEmailChange}> </lightning-input-field>

handleEmailChange(event) {
this.email = event.target.value;

}

enter image description here

Best Answer

Apex Controller

public with sharing class ContactAccessor {
    @AuraEnabled(cacheable=true)
    public static Contact fetchContact(String conId){
        return [SELECT Id,Name,Email FROM Contact WHERE ID=:conId];
    }
}

HTML Form

<template>

    <lightning-record-edit-form object-api-name="dsrprash__Expense__c">
        <lightning-input-field field-name="dsrprash__Candidat__c" onchange={handleCandidatChange}></lightning-input-field>
        <lightning-input-field field-name="dsrprash__Email_de_Candidat__c" value={candidateEmail} data-my-id="cand-email"></lightning-input-field>
        <lightning-button class="slds-m-top_small" type="submit" label="Create new">
        </lightning-button>
    </lightning-record-edit-form>

</template>

JS Code

import { LightningElement } from 'lwc';
import fetchContactDetails from '@salesforce/apex/ContactAccessor.fetchContact';

export default class ContactEmail extends LightningElement {

    candidateEmail; //this property is binded with the "Email de Candidat" 

    handleCandidatChange(event){
        console.log('selected candidate = '+event.target.value);
        let candidateId =  event.target.value; //this contains the user selected Candidat's Id

        //getting reference of the HTML element of "Email de Candidat"
        let canEmailRef = this.template.querySelector('lightning-input-field[data-my-id=cand-email]');
        if(candidateId && canEmailRef){
            //invoking apex method to fetch the selected Candidat Details...
            fetchContactDetails({conId:candidateId})
            .then( result => {
                console.log(' ## result from the apex method = '+JSON.stringify(result));
                this.candidateEmail = result.Email;
            })
            .catch( error => {
                //handle error...
                alert(' something went wrong in apex call..');
            });           
        }
        else{
            console.log(' cannot find the Candidat Email field ref in DOM...');
        }
    }
}

onchange event is added on Candidat field and calling handleCandidatChange method. Then it will fetch the details of the selected candidate and updates the email in the "Email de Candidat" field value attribute

Hoping that, this will be helpful!