0

Here is my entire code, I am working in Visual studio code  I have created a custom object 'Price', having two custom look up fields (Project, Unit) and some other fields,. I am trying to insert values into this custom object, but the look up field values(Project, Unit) are not getting inserted, all other fields got inserted.

Html template

<template>
  <lightning-card>
          <p class="slds-var-p-horizontal_small">

            <lightning-input label="Date" name="datefld" type="date" value={fDate} onchange={handledChange} ></lightning-input>
            <lightning-input label="Enquirer" name="enquiry" type="textarea" value={enq} onchange={handledChange} ></lightning-input>
    <lightning-record-edit-form object-api-name="Price__c">
    <lightning-input-field field-name="Project__c" required onchange={projectChange}>
    </lightning-input-field>
    </lightning-record-edit-form>
    <lightning-record-edit-form object-api-name="Price__c">
        <lightning-input-field field-name="Unit__c"  onchange={unitChange}>
        </lightning-input-field>
        </lightning-record-edit-form>
    
    
   <lightning-input label="Unit Rate" name="rate" type="Double" value={rNumber} onchange={handledChange}></lightning-input>
    <lightning-input label="Additional cost" name="cost" type="Double" value={cNumber} onchange={handledChange}></lightning-input>
    <lightning-input label="Discount" name="dis" type="Double" value={dNumber} onchange={handledChange}></lightning-input>
    <lightning-input label="Agreement cost" name="agree" type="Double" value={aNumber} onchange={handledChange}></lightning-input>
    <lightning-input label="Remarks" name = "remark" type="textarea" value={resultsum} onchange={handledChange}></lightning-input> 
    
     <lightning-button label="Save" onclick={handleClick}></lightning-button>
    
    
           </p>
           </lightning-card>
</template>

JS

import { LightningElement ,api, wire, track} from 'lwc';

import newPrice from '@salesforce/apex/PriceFetch.newPrice';
export default class PriceScreen extends LightningElement {
    datefld;
    enquiry;
    projSelected;
    unitSelected;
    rate;
    cost;
    dis;
    agree;
    remark;
 
    handledChange(event){

        if(event.target.name==='datefld'){

            console.log('handle Change'+event.target.value);

            this.datefld = event.target.value;

        }
        else if(event.target.name==='enquiry'){

            console.log('handle Change'+event.target.value)

            this.enquiry = event.target.value;    
       }

        else if(event.target.name==='rate'){

            console.log('handle Change'+event.target.value)

            this.rate = event.target.value;    
       }
       else if(event.target.name==='cost'){

        console.log('handle Change'+event.target.value)

        this.cost = event.target.value;    

    }
    else if(event.target.name==='dis'){

        console.log('handle Change'+event.target.value)

        this.dis = event.target.value;    
        
   
    }
    else if(event.target.name==='agree'){

        console.log('handle Change'+event.target.value)

        this.agree = event.target.value;    

    }
    else if(event.target.name==='remark'){

        console.log('handle Change'+event.target.value)

        this.remark = event.target.value;    

    }
    }
    projectChange(event) {
        projSelected = event.detail;
    }
    unitChange(event) {
        unitSelected = event.detail;
    }
   handleClick(event) {
        newPrice({ entryDate: this.datefld, enqName : this.enquiry, proj:projSelected, unit:unitSelected,
             unitRate:this.rate, addtnlCost:this.cost, discount:this.dis, agreeCost:this.agree,rem:this.remark })

        .then(result => {

            this.result = result;
           if(this.result == 'true') {
               alert("SUCCESS");
           }
        })
        .catch(error => {
             this.error = error;
             alert("FAILURE" + error);
              //this.error = error;
        });
    }
}

PriceFetch.cls (Apex controler class)

public with sharing class PriceFetch {
  
 
    @AuraEnabled
    public static Boolean newPrice(Date entryDate, String enqName, Id proj, Id unit,Double unitRate,Double addtnlCost, Double discount,Double agreeCost, String rem ){
     
       Price__c price = new Price__c();
       price.Date__c = entryDate;
       price.Name = enqName;
       price.Project__c = proj;
       price.Unit__c = unit;
       price.Unit_Rate__c = unitRate;
       price.Additional_cost__c = addtnlCost;
       price.Discount__c = discount;
       price.Agreement_cost__c = agreeCost;
       price.Remarks__c = rem;
       try {
        insert price;
        return true;
           
       } catch (Exception e) {
           throw new AuraHandledException('exceptionText' + e.getMessage());
         
       }
      
    }
}

​​​​​​​Is there any mistake in my code

0

There are two problems with your code.

  1. You are missing this keyword while passing some attributes to the imperative function
handleClick(event) {
    newPrice({
        entryDate: this.datefld,
        enqName: this.enquiry,
        proj: this.projSelected, // this was missing on this line
        unit: this.unitSelected,// this was missing on this line
        unitRate: this.rate,
        addtnlCost: this.cost,
        discount: this.dis,
        agreeCost: this.agree,
        rem: this.remark
    })
        .then(result => {
            this.result = result;
            if (this.result == "true") {
                alert("SUCCESS");
            }
        })
        .catch(error => {
            this.error = error;
            alert("FAILURE" + error);
            //this.error = error;
        });
}
  1. The value returned by lightning-input-field component for a field of type lookup, is an array so you need to change the method.

    projectChange(event) { this.projSelected = event.detail.value[0]; }

Again, you were missing this in the below function, and to get the value you need to access the value property of an event or the target like event.detail.value or event.target.value.

unitChange(event) {
    this.unitSelected = event.detail.value;
}

Note:- that every time you access the class property in the js code you need to access that with this keyword, otherwise, you will run into an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.