0

I created a lightning component which holds a small form of a specific record and I want to set a field value to 'Submitted' once the user submits the form. The field is not part of the form.

How on earth do I do that ??

The component:

<aura:component implements="flexipage:availableForAllPageTypes, force:hasRecordId" access="global" controller="HandOverSubmit">

<aura:attribute name="record" type="Hand_Over__c" />
<aura:attribute name="recordFields" type="Hand_Over__c" />
<aura:attribute name="isError" type="boolean" default="false"/>
<aura:attribute name="message" type="String" />
<aura:attribute name="messageType" description="Message type, can be error/warning/info/success" type="String" default="info"/>
<aura:attribute name="fileToBeUploaded" type="Object[]"/>
<aura:attribute name="isInputFileRequired" type="boolean" default="false" />

<force:recordData layoutType="FULL"
              targetRecord="{!v.record}"
              recordId="{!v.recordId}"
              targetFields="{!v.recordFields}" />

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<article aria-describedby="rlql-headerLabel" class="slds-card slds-card_boundary">
    <lightning:recordEditForm recordId="{!v.recordId}" objectApiName="Hand_Over__c" onsubmit="{!c.submit}">
        <div id="rlql-header" class="slds-card__header slds-grid">
            <header class="slds-media slds-media_center slds-has-flexi-truncate">
                <div class="slds-media__body">
                    <h2 id="rlql-headerLabel" class="slds-text-heading--small">Handover Submit</h2>
                </div>
                <aura:if isTrue="{!!v.isError}">
                    <div class="slds-no-flex">
                        <lightning:button variant="brand" 
                                            type="submit"
                                            label="Submit"
                                            title="Submit"
                                            />
                    </div>
                </aura:if>
            </header>
        </div>

        <aura:if isTrue="{!v.isError}">
            <div class="{!'slds-notify slds-notify--alert slds-banner slds-theme--alert-texture slds-theme--'+v.messageType}" role="alert">
                <!-- <lightning:icon iconName="{! 'utility:'+ v.messageType }" size="x-small" alternativeText="{!v.messageType}" variant="inverse" class="iconContainer"/>&nbsp; -->
                <span class="slds-text-body-regular">
                    <h2>
                        <aura:unescapedHtml value="{!v.message}" />
                    </h2>
                </span>

            </div>

        </aura:if>

        <div class="slds-card__body_inner">
            <aura:if isTrue="{!!v.isError}">

                    <lightning:inputField fieldName="Email_Comments__c" /> <br/>
                    <lightning:input type="file" files="{!v.fileToBeUploaded}"
                         accept="image/png, .zip"
                         label="Signed I/O"
                         name="file1" multiple="true" 
                         required="{!v.isInputFileRequired}"/> <br/>

            </aura:if>
        </div>
    </lightning:recordEditForm>
</article>

The controller:

({
doInit : function(component, event, helper) {

    var action = component.get("c.validate");
    action.setParams({ handoverId : component.get("v.recordId") });

    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {

            var res = response.getReturnValue();

            //INIT SUCCESS
            if (res !== 'SUCCESS'){
                component.set("v.isError",true);
                component.set("v.messageType","error");
                component.set("v.message", res);
            }

        }
        else {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });

    $A.enqueueAction(action);

}, submit: function(component, event, helper) {

    var record = component.get("v.record");
    var recordFields = component.get("v.recordFields");
    var files = component.get("v.fileToBeUploaded"); //files[0] is the file list

    if (files && files[0].length > 0){

    }
    else if (recordFields.Signed_I_O__c === "Yes"){
        component.set("v.isInputFileRequired", true);
    }

    $A.get('e.force:refreshView').fire();   }})
  • 2
    Is this lightning:recordEditForm - Set or modify field values onsubmit what you are looking for? – Keith C Aug 19 '18 at 12:02
  • Can you elaborate your problem more crystal clear? – sanket kumar Aug 19 '18 at 12:05
  • Once the user submits the form, I want to update a field in the related record called Status to the value "Submitted". component.set(record.Status__c, "Submitted") doesn't work – osos9 Aug 19 '18 at 12:07
  • @KeithC thanks! that helped! – osos9 Aug 19 '18 at 12:33
0

You missed to set the field value in javascript controller. You can set the fields which are not part of forms. I have set Lastname and Firstname along with account id. Here is an example where i have created a button on Account where you can click button to create contact with a name value set by me in javascript controller:-

<aura:attribute name="newContact" type="Object"/>
<aura:attribute name="simpleNewContact" type="Object"/> 
<aura:attribute name="newContactError" type="String"/> 
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<force:recordData aura:id="contactRecordCreator" layoutType="FULL" targetRecord="{!v.newContact}"
                  targetFields="{!v.simpleNewContact}" targetError="{!v.newContactError}" />

<div class="slds-page-header" role="banner">
    <p class="slds-text-heading_label">Create Contact</p>
</div>

<!-- Display Lightning Data Service errors -->

<aura:if isTrue="{!not(empty(v.newContactError))}">
    <div class="recordError">
        <ui:message title="Error" severity="error" closable="true">
            {!v.newContactError}
        </ui:message></div>
</aura:if>

<!-- Display the new contact form -->
<div class="slds-form_stacked">

    <lightning:button label="Save contact" onclick="{!c.handleSaveContact}"
                      variant="brand" class="slds-m-top_medium"/></div>
</aura:component>

Javascript controller:-

({
doInit: function(component, event, helper) {
    // Prepare a new record from template
    component.find("contactRecordCreator").getNewRecord(
        "Contact", // sObject type (objectApiName)
        null, // recordTypeId
        false, // skip cache?
        $A.getCallback(function() {
            var rec = component.get("v.newContact");
            var error = component.get("v.newContactError");
            if(error || (rec === null)) {
                console.log("Error initializing record template: " + error);
                return;
            }
            console.log("Record template initialized: " + rec.sobjectType);
        })
    );
},

handleSaveContact: function(component, event, helper) {
// here i am setting the field value
        component.set("v.simpleNewContact.AccountId", component.get("v.recordId"));
        component.set("v.simpleNewContact.FirstName", "SAnket"); 
        component.set("v.simpleNewContact.LastName", "kumar");  
        component.find("contactRecordCreator").saveRecord(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // record is saved successfully
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The record was saved."
                });
                resultsToast.fire();                
            } else if (saveResult.state === "INCOMPLETE") {
                // handle the incomplete state
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                // handle the error state
                console.log('Problem saving contact, error: ' +
                            JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        });
    }
})

You can refractor it according to object and fields you want to set. Hope it helps you.

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.