0

I have made a component which will create the Account records, i want to upload image as well along with the rest of the fields while creating the Account record. i tried Lightning:fileUpload, but it does not have the 'onClick' attribute rather it has 'onuploadfinished' so whenever file uploads it fires and i don't want it to be happen. I just need to upload image and do nothing until the 'Save' button is clicked. for that i figured out to create 'lightning:input type=file' from here (https://sfdcmonkey.com/2017/09/25/file-upload-lightning-component/#comment-20975) . but i am out of luck because it's not working even i can't save my component when i create separate component so i have included this '<lightning:input type=file' in my previous component. but now i am clueless how can i get it worked altogether . i mean i need to know what new fields i will be needing in object to store image, what more code would be required and all. Thanks in advance enter image description here

newAccount.cmp

<aura:component controller="createAccountRecords" 
            implements="lightning:isUrlAddressable,force:appHostable,
                        flexipage:availableForAllPageTypes,
                        flexipage:availableForRecordHome,
                        force:hasRecordId,forceCommunity:availableForAllPageTypes,
                        force:lightningQuickAction,lightning:actionOverride" access="global">
<aura:attribute name="createAcc" type="Account" default="{'sObjectType' : 'Account','Name':'Rating'}"/>
<aura:attribute name="objName" type="String" default="Account"/>
<aura:attribute name="fldName" type="String" default="Rating"/>
<aura:attribute name="ratingList" type="List"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<div class="slds-p-around_small">
    <lightning:input type="Text" label="Name" value="{!v.createAcc.Name}"/>
    <lightning:input type="Text" label="Account Number" value="{!v.createAcc.AccountNumber}"/>
    <lightning:input type="Email" name="email2" value="{!v.createAcc.Email}" label="Email ID"/>
    <lightning:input type="Phone" label="Phone Number" value="{!v.createAcc.Phone}"/>
    <lightning:select label="Rating" value="{!v.createAcc.Rating}">
        <option value="">—None—</option>
        <aura:iteration items="{!v.ratingList}" var="ac">
            <option value="{!ac}">{!ac}</option>
        </aura:iteration>
    </lightning:select>
    <lightning:input aura:id="fileId" type="file" 
                     name="file" label="Upload Attachment" multiple="false"/>
    <lightning:button label="Save" iconPosition="left" variant="brand" onclick="{!c.doSave}"/>
    <lightning:button label="Cancel" iconPosition="right" variant="destructive" onclick="{!c.docancel}"/>
</div>

</aura:component>

newAccountController.js

({
doInit : function(component, event, helper) {
    var action = component.get("c.getPickList");
    action.setParams({
        objName : component.get("v.objName"),
        fldName : component.get("v.fldName")
    });
    action.setCallback(this,function(result){
        var resultValue = result.getReturnValue();
        component.set("v.ratingList",resultValue);
    });
    $A.enqueueAction(action);
},
doSave : function(component, event, helper) {
    var action = component.get("c.createAccount");
    action.setParams({ac : component.get("v.createAcc")});
    action.setCallback(this,function(result){
        var getAllValue = component.get("v.createAcc");
        alert(JSON.stringify(getAllValue));
    });
    $A.enqueueAction(action);
},
docancel : function(component, event, helper) {
    component.set("v.createAcc", null);
},

})

createAccountRecords.apxc

public class createAccountRecords {
@AuraEnabled
public static List<String> getPickList(String objName,String fldName) {
    List<String> pkList = new List<String>();
    Map<String,Schema.SObjectType> allObj = Schema.getGlobalDescribe();
    Map<String,Schema.SObjectField> allFlds = allObj.get(objName).getDescribe().fields.getMap();
    List<Schema.PicklistEntry> pickList = allFlds.get(fldName).getDescribe().getPickListValues();
    for(Schema.PicklistEntry pk : pickList){
        pkList.add(pk.getValue());
    }
    return pkList;
}
@AuraEnabled
public static Account createAccount(Account ac){
    insert ac;
    return ac;
}

}

5
  • 1
    Maybe you can break this single process in two parts in step1 take all inputs except file and in step2 use lightning:fileUpload and upload the the file. Dec 17 '20 at 11:38
  • That won't work according to requirement.
    – maaz ahmed
    Dec 17 '20 at 12:00
  • 1
    Yep, I've done it both ways and the two step approach is 70% easier. Otherwise, you need to perform error handling for files that are too large, set restore point for rollback if one of the inserts fail, etc
    – JimG
    Dec 17 '20 at 13:44
  • @JimG i understand, as you said you've done it can you share you you did?
    – maaz ahmed
    Dec 18 '20 at 10:48
  • I don't have the project code available at this time. It was a while back. Good luck!
    – JimG
    Dec 18 '20 at 15:27

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.