You need to sign in to do that
Don't have an account?
Vishakha Soman
Update Account details using recordID
Hi,
I am trying to create a component which will update any account feild by fetching record details and update it using update button. But how will I acess record using recordID and display it as outputfeild.
and How will I set those values back.
It will really help if anyone can guide me with this.
Here is my code according to my knowledge:
Component.cmp
<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId">
<aura:attribute name="recordId" type="Id" />
<aura:attribute type="List" name="accountList"/>
<aura:attribute name="name" type="string" />
<aura:attribute name="type" type="string" />
<aura:attribute name="phone" type="string" />
<aura:attribute name="website" type="string" />
<lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Account">
<lightning:card title="Update Account">
<aura:set attribute="actions">
<lightning:button variant="brand" label="Update Account" title="Update Account" onclick="{!c.updateAccountController}" />
</aura:set>
<p class="slds-p-horizontal_small">
<div class="slds-grid slds-wrap">
<span class="slds-size_1-of-2">
<lightning:outputField name="outputAccountName" value="{!v.accountName}" label="Account Name"/>
</span>
<span class="slds-size_1-of-2 slds-p-left_x-small">
<lightning:outputField type="tel" label="Phone" name="phone1" value="{!v.phone}"/>
</span>
<span class="slds-size_1-of-2">
<lightning:outputField name="outputWebsite" label="Website" value="{!v.website}"/>
</span>
<span class="slds-size_1-of-2 slds-p-left_x-small">
<lightning:outputField name="outputType" label="Type" value="{!v.type}"/>
</span>
</div>
</p>
</lightning:card>
</lightning:recordViewForm>
</aura:component>
====================================================
controller.js
({
ChangeText : function(component, event, helper) {
helper.getAccountListHelper(component);
},
UpdateAccountController : function(component, event, helper) {
var action = component.get("c.UpdateAccountApex");
action.setParams({
accName : component.set("v.accountName"),
phone : component.set("v.phone"),
website : component.set("v.website"),
type : component.set("v.type")
})
action.setCallback(this,function(response){
var state = response.getState();
if(state === "SUCCESS"){
console.log("====response===="+response.getReturnValue());
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "Account updated Successfully!"
});
toastEvent.fire();
}
else if(state === "ERROR"){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Error!",
"message": "An Error Occured!"
});
toastEvent.fire();
}
});
$A.enqueueAction(action);
}
})
===================================================
helper.js
({
getAccountListHelper: function(component) {
component.set('v.mycolumns',[
{label: 'Account Name' ,fieldName: 'Name',type: 'text', sortable:true},
{label: 'Account Type' ,fieldName: 'Type',type: 'text'},
{label: 'Website' ,fieldName: 'Website',type: 'url'},
{label: 'Phone' ,fieldName: 'Phone',type: 'Phone'},
]);
var action = component.get("c.getAccListApex");
action.setCallback(this,function(response){
var state = response.getState();
if (state == "SUCCESS")
{
console.log("====response====",response.getReturnValue());
component.set("v.accountList",response.getReturnValue());
}
if (state == "Error")
{
alert('error');
}
});
$A.enqueueAction(action);
}
====================================================
apexclass
public class TestControllerUpdateApex {
@AuraEnabled
public static List<Account> getAccListApex(){
List<Account> accList = new List<Account>();
accList = [Select Id,Name, Type, Website,Phone,Industry from account];
return accList;
}
@AuraEnabled
public static void UpdateAccountApex(string[] recordId){
List<Account> accList = [Select id from Account where id=:recordId];
update accList;
}
@AuraEnabled
public static string createAccountApex(string accName, string phone, string website, string type){
Account acc = new Account();
acc.name = accName;
acc.Phone = phone;
acc.website = website;
acc.type = type;
insert acc;
return acc.Id;
}
})
Thankyou in Advance
I am trying to create a component which will update any account feild by fetching record details and update it using update button. But how will I acess record using recordID and display it as outputfeild.
and How will I set those values back.
It will really help if anyone can guide me with this.
Here is my code according to my knowledge:
Component.cmp
<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId">
<aura:attribute name="recordId" type="Id" />
<aura:attribute type="List" name="accountList"/>
<aura:attribute name="name" type="string" />
<aura:attribute name="type" type="string" />
<aura:attribute name="phone" type="string" />
<aura:attribute name="website" type="string" />
<lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Account">
<lightning:card title="Update Account">
<aura:set attribute="actions">
<lightning:button variant="brand" label="Update Account" title="Update Account" onclick="{!c.updateAccountController}" />
</aura:set>
<p class="slds-p-horizontal_small">
<div class="slds-grid slds-wrap">
<span class="slds-size_1-of-2">
<lightning:outputField name="outputAccountName" value="{!v.accountName}" label="Account Name"/>
</span>
<span class="slds-size_1-of-2 slds-p-left_x-small">
<lightning:outputField type="tel" label="Phone" name="phone1" value="{!v.phone}"/>
</span>
<span class="slds-size_1-of-2">
<lightning:outputField name="outputWebsite" label="Website" value="{!v.website}"/>
</span>
<span class="slds-size_1-of-2 slds-p-left_x-small">
<lightning:outputField name="outputType" label="Type" value="{!v.type}"/>
</span>
</div>
</p>
</lightning:card>
</lightning:recordViewForm>
</aura:component>
====================================================
controller.js
({
ChangeText : function(component, event, helper) {
helper.getAccountListHelper(component);
},
UpdateAccountController : function(component, event, helper) {
var action = component.get("c.UpdateAccountApex");
action.setParams({
accName : component.set("v.accountName"),
phone : component.set("v.phone"),
website : component.set("v.website"),
type : component.set("v.type")
})
action.setCallback(this,function(response){
var state = response.getState();
if(state === "SUCCESS"){
console.log("====response===="+response.getReturnValue());
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "Account updated Successfully!"
});
toastEvent.fire();
}
else if(state === "ERROR"){
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Error!",
"message": "An Error Occured!"
});
toastEvent.fire();
}
});
$A.enqueueAction(action);
}
})
===================================================
helper.js
({
getAccountListHelper: function(component) {
component.set('v.mycolumns',[
{label: 'Account Name' ,fieldName: 'Name',type: 'text', sortable:true},
{label: 'Account Type' ,fieldName: 'Type',type: 'text'},
{label: 'Website' ,fieldName: 'Website',type: 'url'},
{label: 'Phone' ,fieldName: 'Phone',type: 'Phone'},
]);
var action = component.get("c.getAccListApex");
action.setCallback(this,function(response){
var state = response.getState();
if (state == "SUCCESS")
{
console.log("====response====",response.getReturnValue());
component.set("v.accountList",response.getReturnValue());
}
if (state == "Error")
{
alert('error');
}
});
$A.enqueueAction(action);
}
====================================================
apexclass
public class TestControllerUpdateApex {
@AuraEnabled
public static List<Account> getAccListApex(){
List<Account> accList = new List<Account>();
accList = [Select Id,Name, Type, Website,Phone,Industry from account];
return accList;
}
@AuraEnabled
public static void UpdateAccountApex(string[] recordId){
List<Account> accList = [Select id from Account where id=:recordId];
update accList;
}
@AuraEnabled
public static string createAccountApex(string accName, string phone, string website, string type){
Account acc = new Account();
acc.name = accName;
acc.Phone = phone;
acc.website = website;
acc.type = type;
insert acc;
return acc.Id;
}
})
Thankyou in Advance
Please follow below example
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh