You need to sign in to do that
Don't have an account?
Ragula Sivakumar
passing the input values into Apex controller In LWC
I could able to upload the file successfully but the input value not passing through the method
HTML:
<template>
<!--lightning-record-edit-form object-api-name="Credicard"-->
<lightning-card icon-name="custom:custom19" title='Credit card information uplaod'>
<div class="slds-m-around_medium">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<lightning-input type="number" name="Import Id" required label="Import Id" value={impnum}
onclick={handlechange} style="width:200px">
</lightning-input>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<template if:true={Programvalues.data}>
<lightning-combobox
name="Program"
label="Program"
value={Programvalue}
placeholder="-Select-"
options={Programvalues.data.values}
onchange={handleselect} style="width:200px"></lightning-combobox>
</template>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<template if:true={AXvalues.data}>
<lightning-combobox
name="AXvalue"
label="AxPID"
value={Avalue}
placeholder="-Select-"
options={AXvalues.data.values}
onchange={handleselect} style="width:200px" ></lightning-combobox>
</template>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<lightning-input type="number" name="Exchange Rate" required label="Exchange Rate" value={inpputnum}
onclick={handlechange1} style="width:200px" >
</lightning-input>
</lightning-layout-item>
</div>
</div>
</div>
<lightning-file-upload
name="Import Data"
label="Import Data"
accept={acceptedFormats}
record-id={recordId}
onuploadfinished={handleUploadFinished} ></lightning-file-upload>
<template if:true ={success} >
<c-error-panel errors={success}></c-error-panel>
</template>
<template if:true={failure}>
<c-error-panel errors={failure}></c-error-panel>
</template>
</lightning-card>
</template>
JS:
import { LightningElement ,wire,track,api} from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import CREDITCARD_OBJECT from '@salesforce/schema/Credit_card__c';
import PROGRAMM_Field from '@salesforce/schema/Credit_card__c.Choose_Program_Reason__c';
import AXPID_Field from '@salesforce/schema/Credit_card__c.AX_PID__c';
//import EXCHANERATE_FIELD from '@salesforce/schema/Credit_card__c.Exchange_Rate__c';
//import IMPORT_FIELD from '@salesforce/schema/Credit_card__c.ImportId__c';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import insertdata from '@salesforce/apex/importdata.readCSVFileInsertMember';
//const columns = [
//{ label: 'CampaignId', fieldName: 'CampaignId' },
// { label: 'ContactId', fieldName: 'ContactId' },
// { label: 'Status', fieldName: 'Status'}
//];
export default class Uploadfilewithinput extends LightningElement {
//Credicard=CREDITCARD_OBJECT;
// ExchaneRate=EXCHANERATE_FIELD;
// ImpotId=IMPORT_FIELD;
@api impnum ="";
inpputnum="";
Programvalue="";
Avalue="";
@track success;
@track failure;
@api recordId;
// @track data;
// @track columns =columns;
handlechange(event){
this.impnum=event.target.value;
}
handlechange1(event){
this.inpputnum=event.target.value;
}
@wire(getObjectInfo, { objectApiName: CREDITCARD_OBJECT })
objectInfo;
@wire(getPicklistValues, { recordTypeId: '012000000000000AAA', fieldApiName:PROGRAMM_Field})
Programvalues;
@wire(getPicklistValues, { recordTypeId: '012000000000000AAA', fieldApiName: AXPID_Field})
AXvalues;
@wire(insertdata , { impnum: '$impnum'})
deWired;
//inpputnum: '$inpputnum',Programvalue: '$Programvalue',Avalue: '$Avalue'
handleselect(event){
if(event.target.name ==='Program'){
this.Programvalue=event.target.value;
}
if(event.target.name === 'AXvalue'){
this.Avalue=event.target.value;
}
}
get acceptedFormats() {
return ['.csv'];
}
handleUploadFinished(event) {
const uploadedFiles = event.detail.files;
insertdata({idContentDocument : uploadedFiles[0].documentId})
.then(result => {
this.success = result;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success!!',
message: 'Campaign Members are created based CSV file!!!',
variant: 'success',
}),
);
})
.catch(error => {
this.failure=error;
this.dispatchEvent(
new ShowToastEvent({
title: 'Error!!',
message: JSON.stringify(error),
variant: 'error',
}),
);
});
}
}
Contoller:
public class importdata{
@AuraEnabled
public static List<Campaignmember> readCSVFileInsertMember(Id idContentDocument,string impnum ){
List<Campaignmember> lstCampaignMembers = new List<Campaignmember>();
if(idContentDocument != null) {
ContentVersion objVersion = [Select Id,VersionData FROM ContentVersion WHERE ContentDocumentId =: idContentDocument][0];
if(objVersion != null) {
Blob csvFileBody =objVersion.VersionData;
String csvAsString = csvFileBody.toString();
String[] csvFileLines = csvAsString.split('\n');
for(Integer i=1;i<csvFileLines.size();i++){
Campaignmember camobj = new Campaignmember() ;
string[] csvRecordData = csvFileLines[i].split(',');
camobj.CampaignId= csvRecordData[0] ;
camobj.ContactId= csvRecordData[1];
camobj.Status= csvRecordData[2];
camobj.impnum__c=impnum;
lstCampaignMembers.add(camobj );
}
if(!lstCampaignMembers.isEmpty()) {
insert lstCampaignMembers;
}
}
}
return lstCampaignMembers;
}
}
HTML:
<template>
<!--lightning-record-edit-form object-api-name="Credicard"-->
<lightning-card icon-name="custom:custom19" title='Credit card information uplaod'>
<div class="slds-m-around_medium">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<lightning-input type="number" name="Import Id" required label="Import Id" value={impnum}
onclick={handlechange} style="width:200px">
</lightning-input>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<template if:true={Programvalues.data}>
<lightning-combobox
name="Program"
label="Program"
value={Programvalue}
placeholder="-Select-"
options={Programvalues.data.values}
onchange={handleselect} style="width:200px"></lightning-combobox>
</template>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<template if:true={AXvalues.data}>
<lightning-combobox
name="AXvalue"
label="AxPID"
value={Avalue}
placeholder="-Select-"
options={AXvalues.data.values}
onchange={handleselect} style="width:200px" ></lightning-combobox>
</template>
</lightning-layout-item>
</div>
<div class="slds-col slds-size_1-of-2">
<lightning-layout-item size="6" padding="around-medium">
<lightning-input type="number" name="Exchange Rate" required label="Exchange Rate" value={inpputnum}
onclick={handlechange1} style="width:200px" >
</lightning-input>
</lightning-layout-item>
</div>
</div>
</div>
<lightning-file-upload
name="Import Data"
label="Import Data"
accept={acceptedFormats}
record-id={recordId}
onuploadfinished={handleUploadFinished} ></lightning-file-upload>
<template if:true ={success} >
<c-error-panel errors={success}></c-error-panel>
</template>
<template if:true={failure}>
<c-error-panel errors={failure}></c-error-panel>
</template>
</lightning-card>
</template>
JS:
import { LightningElement ,wire,track,api} from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import CREDITCARD_OBJECT from '@salesforce/schema/Credit_card__c';
import PROGRAMM_Field from '@salesforce/schema/Credit_card__c.Choose_Program_Reason__c';
import AXPID_Field from '@salesforce/schema/Credit_card__c.AX_PID__c';
//import EXCHANERATE_FIELD from '@salesforce/schema/Credit_card__c.Exchange_Rate__c';
//import IMPORT_FIELD from '@salesforce/schema/Credit_card__c.ImportId__c';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import insertdata from '@salesforce/apex/importdata.readCSVFileInsertMember';
//const columns = [
//{ label: 'CampaignId', fieldName: 'CampaignId' },
// { label: 'ContactId', fieldName: 'ContactId' },
// { label: 'Status', fieldName: 'Status'}
//];
export default class Uploadfilewithinput extends LightningElement {
//Credicard=CREDITCARD_OBJECT;
// ExchaneRate=EXCHANERATE_FIELD;
// ImpotId=IMPORT_FIELD;
@api impnum ="";
inpputnum="";
Programvalue="";
Avalue="";
@track success;
@track failure;
@api recordId;
// @track data;
// @track columns =columns;
handlechange(event){
this.impnum=event.target.value;
}
handlechange1(event){
this.inpputnum=event.target.value;
}
@wire(getObjectInfo, { objectApiName: CREDITCARD_OBJECT })
objectInfo;
@wire(getPicklistValues, { recordTypeId: '012000000000000AAA', fieldApiName:PROGRAMM_Field})
Programvalues;
@wire(getPicklistValues, { recordTypeId: '012000000000000AAA', fieldApiName: AXPID_Field})
AXvalues;
@wire(insertdata , { impnum: '$impnum'})
deWired;
//inpputnum: '$inpputnum',Programvalue: '$Programvalue',Avalue: '$Avalue'
handleselect(event){
if(event.target.name ==='Program'){
this.Programvalue=event.target.value;
}
if(event.target.name === 'AXvalue'){
this.Avalue=event.target.value;
}
}
get acceptedFormats() {
return ['.csv'];
}
handleUploadFinished(event) {
const uploadedFiles = event.detail.files;
insertdata({idContentDocument : uploadedFiles[0].documentId})
.then(result => {
this.success = result;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success!!',
message: 'Campaign Members are created based CSV file!!!',
variant: 'success',
}),
);
})
.catch(error => {
this.failure=error;
this.dispatchEvent(
new ShowToastEvent({
title: 'Error!!',
message: JSON.stringify(error),
variant: 'error',
}),
);
});
}
}
Contoller:
public class importdata{
@AuraEnabled
public static List<Campaignmember> readCSVFileInsertMember(Id idContentDocument,string impnum ){
List<Campaignmember> lstCampaignMembers = new List<Campaignmember>();
if(idContentDocument != null) {
ContentVersion objVersion = [Select Id,VersionData FROM ContentVersion WHERE ContentDocumentId =: idContentDocument][0];
if(objVersion != null) {
Blob csvFileBody =objVersion.VersionData;
String csvAsString = csvFileBody.toString();
String[] csvFileLines = csvAsString.split('\n');
for(Integer i=1;i<csvFileLines.size();i++){
Campaignmember camobj = new Campaignmember() ;
string[] csvRecordData = csvFileLines[i].split(',');
camobj.CampaignId= csvRecordData[0] ;
camobj.ContactId= csvRecordData[1];
camobj.Status= csvRecordData[2];
camobj.impnum__c=impnum;
lstCampaignMembers.add(camobj );
}
if(!lstCampaignMembers.isEmpty()) {
insert lstCampaignMembers;
}
}
}
return lstCampaignMembers;
}
}