0

The LWC component gets the few parameters as input in UI and a upload file functionality is also there. If I upload the file, the file and the above given metatags should also be sent via the POST callout to the URL. Only after the record gets saved in postgress, it should be synced with sfdc and get saved in sfdc. It should also fetch the record ID of the Account record or the opportunity record to which it has a lookup relationship dynamically from the url. I have built the above code but I am not able to save the docUpload class cause of the followig errors. Also can you guide me what changes should be done to the code so i can upload the file and the metatags along with it and should also be passed as a single POST callout without the record actually getting saved in salesforce to heroku connect. I have the following error remaining.

Unable to find Apex action class referenced as 'docUpload'.

Also in the class the following error is still remaining.

Invalid character in identifier: Version__c (12:144) Variable does not exist: recordId (16:75) This is my html in LWC

<template>
     <lightning-card title="" icon-name="custom:custom19">
        <lightning-file-upload
            label="Attach receipt"
            name="fileUploader"
            accept={acceptedFormats}
            record-id={recordId}
            onuploadfinished={handleUploadFinished}
            multiple>
    </lightning-file-upload>
    </lightning-card>
    <lightning-record-edit-form object-api-name="DMS__c" onsuccess={handleSuccess} onsubmit ={handleSubmit} >
        <div class="slds-grid">
            <div class="slds-col slds-size_1-of-2">
        <lightning-input-field field-name='Name' class='Name'></lightning-input-field>
        <lightning-input-field field-name='Version__c' class='Version__c'></lightning-input-field>
        <lightning-input-field field-name='Key_Words__c' class='Key_Words__c'></lightning-input-field>
        <lightning-input-field field-name='Doc_type__c' class='Doc_type__c'></lightning-input-field>
        <lightning-input-field field-name='Description__c' class='Description__c'></lightning-input-field>
        </div>
        <div class="slds-col slds-size_1-of-2">
            <lightning-input-field field-name='Contract_LOB__c' class='Contract_LOB__c'></lightning-input-field>
            <lightning-input-field field-name='Active_Contract__c' class='Active_Contract__c'></lightning-input-field>
            <lightning-input-field field-name='Competitor_Contract__c' class='Competitor_Contract__c'></lightning-input-field>
            <lightning-input-field field-name='Folder__c' class='Folder__c'></lightning-input-field>
         </div>
        </div>
        <div class="slds-clearfix">
            <div class="slds-align_absolute-center">
        <lightning-button class="slds-m-top_small" type="submit" label="Upload"  onclick={showSuccessToast}></lightning-button>
        <lightning-button class="slds-m-top_small" label="Cancel" onclick={allowReset}></lightning-button>
        </div>
    </div>
    </lightning-record-edit-form>
</template>

The followig is my .js file

import { LightningElement, api } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import POST from '@salesforce/apex/docUpload.POST';  
export default class Final extends LightningElement {
    @api recordId;
    get acceptedFormats() {
        return ['.pdf', '.png','.jpg','.jpeg'];
    }
    handleUploadFinished(event) {
        // Get the list of uploaded files
        const uploadedFiles = event.detail.files;
        let uploadedFileNames = '';
        for(let i = 0; i < uploadedFiles.length; i++) {
            uploadedFileNames += uploadedFiles[i].name + ', ';
        }
          var Name = this.template.querySelector('.Name');
            var Doc_type__c = this.template.querySelector('.Doc_type__c');
            var Active_Contract__c = this.template.querySelector('.Active_Contract__c');
            var Competitor_Contract__c = this.template.querySelector('.Competitor_Contract__c');
            var Version__c = this.template.querySelector('.Version__c');
            var Key_Words__c = this.template.querySelector('.Key_Words__c');
            var Folder__c = this.template.querySelector('.Folder__c');

           // var action = component.get("c.POST");
            POST({Name: Name,Doc_type__c:Doc_type__c ,Active_Contract__c : Active_Contract__c, Competitor_Contract__c : Competitor_Contract__c, Version__c : Version__c, Key_Words__c : Key_Words__c, Folder__c : Folder__c });
          //  action.setParams({ file : uploadedFiles, Name : Name, Doc_type__c : Doc_type__c, Active_Contract__c : Active_Contract__c, Competitor_Contract__c : Competitor_Contract__c, Version__c : Version__c, Key_Words__c : Key_Words__c, Folder__c : Folder__c });
            action.setCallback(this, function(response) {
                var returnValue = response.getReturnValue();
                console.log('returnValue===>'+returnValue);
            
                component.set("v.message", returnValue);
            });
    

            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: uploadedFiles.length + ' Files uploaded Successfully: ' + uploadedFileNames,
                    variant: 'success',
                }),
            );
    }
       
    handleSuccess(event) {
        console.log('onsuccess event ',event.detail.id)
        
    }
    handleSubmit(event) {
        console.log('onsubmit event Newcomp'+ event.detail.fields);
       
    }
    showSuccessToast() {
        const evt = new ShowToastEvent({
            title: 'Toast Success',
            message: 'Opearion sucessful',
            variant: 'success',
            mode: 'dismissable'
        });
        this.dispatchEvent(evt);
    }
    allowReset(event) {
        const inputFields = this.template.querySelectorAll(
            'lightning-input-field'
        );
        if (inputFields) {
            inputFields.forEach(field => {
                field.reset();
            });
        }
    }
}

The following is my Apex class

public with sharing class docUpload {
    
    String Doc_type;
    String Active_Contract;
    String Competitor_Contract;
    String Version;
    String Key_Words;
    String Folder;
    String recordId;

        @AuraEnabled
        public static String POST(Blob file, String recordID, String Name, String Doc_type, String Active_Contract, String Competitor_Contract, String Version, String Key_Words, String Folder) {
            
            Http h = new Http();
            String fileName;
            String url = 'https://csv-file-upload.herokuapp.com/leads/' + recordId;
            String separationString = 'da5cbc76-39ba-4f80-8b81-090b82e90cf0';
            String str = file.toString();
       //     String csvBody = EncodingUtil.base64Encode(csvBlob);
            // decode 
            Blob decodedBlob = EncodingUtil.base64Decode(str);
            system.debug('string ==>'+decodedBlob);
            // convert to string
            String result = decodedBlob.toString();
            
            // assemble the body payload
            String header = '--' + separationString + '\nContent-Disposition: form-data; name="file"; filename="' + fileName + '"\nContent-Type: application/octet-stream\n\n';
            //String body = EncodingUtil.base64Encode(file) + '\n';
            String body = result + '\n';
            String footer = '--' + separationString + '--';
            String bodyPayload = header + body + footer;
            system.debug('PAYLOAD: ' + bodyPayload);
    
            HttpRequest req = new HttpRequest();
            req.setHeader('Content-Type', 'multipart/form-data; boundary=' + separationString);
            
            req.setEndpoint(url);
            req.setMethod('POST');
            req.setBody(bodyPayload);
            system.debug('REQUEST: ' + req);
    
            // Send the request, and return a response
            HttpResponse res = h.send(req);
            System.debug(res.getBody());
            return res.getBody();
        }

}

I want the metatags also to be passed with the uploaded file as a single button functionality. Currently i have given it as 2 button functions in html and .js. Can anybody help me in this. Thanks in advance.

Your Answer

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

Browse other questions tagged or ask your own question.