function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MilesSMilesS 

need help saving a record & refresh the page

I'm trying to create a lwc that updates a field.  I'm able to pull the record information in, however I can either save or refresh the record.  Once the record is saved I'd like to go back to view mode.  What am I missing?  Thank you in advance!  

Component
<aura:component controller="apexCls_fiberPortalPageReferences" implements="forceCommunity:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:themeLayout,flexipage:availableForAllPageTypes,force:appHostable,force:lightningQuickAction" access="global">
    <aura:attribute name="recordId" type="string"/> 
    <aura:attribute name="su" type="Site_Survey__c"/>
    
    <aura:attribute name="updateAction" type="String" default="UNSAVED" />
    <aura:handler name="init" value="{!this}" action="{!c.doInitAction}" />
    
    <br/>Update Action: {!v.updateAction}<br/><br/>
    <aura:if isTrue="{!v.updateAction=='UNSAVED'}">
        <lightning:recordEditForm recordId="{!v.recordId}" objectApiName="Site_Survey__c">
            <lightning:messages />
            <lightning:outputField fieldName="Question__c" />
            <lightning:inputField fieldName="Completed_Detail__c" />
            <lightning:button class="button" type="submit" label="Save" onclick="{!c.save}" />
        </lightning:recordEditForm>
        <aura:set attribute="else">
            Question: {!v.su.Question__c}<br/>
            Completed Detail: {!v.su.Completed_Detail__c}<br/><br/>
            <lightning:button class="button" label="Edit" onclick="{!c.handleClick2}"/>
        </aura:set>
    </aura:if>    
</aura:component>

Controller
({
    doInitAction : function(component, event, helper) {
		var action = component.get("c.SiteSurvey_Detail");
        action.setParams({ "SiteSurveyRecordId": component.get("v.recordId") });
        action.setCallback( this, function(response) {
			var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.su", response.getReturnValue());
                console.log(response.getReturnValue());
            }
        });
        $A.enqueueAction(action);      

    },
    print : function(component, event, helper) {
        window.print();
    },

    save : function(cmp,event,helper) {
        
        cmp.find("edit").get("e.recordSave").fire();
        cmp.set("v.updateAction", "SAVED");
        
    },
    
    handleClick2 : function(cmp, event) {
        var attributeValue = cmp.get("v.updateAction");
        console.log("current text: " + attributeValue);
        
        var target = event.getSource();
        cmp.set("v.updateAction", "UNSAVED");
    }
})


 
PriyaPriya (Salesforce Developers) 
Hey Miles,

Do you want to go detail page of the record you saved, then kindly use the concept of pageReference in the Apex controller.

Here is the sample code :- 
public class mySecondController {
    Account account;

    public Account getAccount() {
        if(account == null) account = new Account();
        return account;
    }

    public PageReference save() {
        // Add the account to the database. 
        insert account;
        // Send the user to the detail page for the new account.
        PageReference acctPage = new ApexPages.StandardController(account).view();
        acctPage.setRedirect(true);
        return acctPage;
    }
}
Kinldy mark it as a best answer if it helps.

Thanks!

 
Jessica HawkJessica Hawk
I was also facing this problem and finally got the best solution on this website (https://ultimateapparels.com/).