You need to sign in to do that
Don't have an account?
Haneef S
insert a new record using custom button
Hi All,
I am new to lightning, can some one please help me with the below code:
I have my existing code that displays accounts and when clicked particular account it shows its own contacts.
Now, on the same page there should be one button for Add Account. On click of this button, popup will ask for account details and after click on submit button this new account will add to database, go to previous page and previous page also will refresh.
when I click on preview I get below error:
Below is my code:
component:
<aura:component controller="AccountPagination" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<ltng:require styles="/resource/SLDS244/styles/salesforce-lightning-design-system.css" />
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<h1 class="slds-text-heading--medium">Accounts List</h1>
</div>
</div>
</div>
<aura:attribute name="acccountList" type="Account[]" />
<aura:attribute name="contacts" type="Contact[]"/>
<aura:attribute name="paginationList" type="Account[]"/>
<aura:attribute name="pageSize" type="Integer" default="15"/>
<aura:attribute name="totalSize" type="Integer"/>
<aura:attribute name="start" type="Integer" />
<aura:attribute name="end" type="Integer"/>
<table class="slds-table slds-table--bordered ">
<thead>
<tr style="color:Grey;font-weight:bold">
<!--<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading-/-medium">Id</h1></th>
</div>
</div>
</div></td>-->
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Name</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">AccountNumber</h1></th>
</div>
</div>
</div></td>
<!--<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading-/-medium">AccountOwnerId</h1></th>
</div>
</div>
</div></td>-->
<!--<th>Id</th>
<th>Name</th>
<th>Phone</th>
<th>AccountNumber</th>
<th>BillingAddress</th>-->
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.paginationList}" var="item">
<tr>
<!--<td><lightning:input value="{!item.Id}"/></td>
<td><lightning:input value="{!item.Name}"/></td>
<td><lightning:input value="{!item.AccountNumber}"/></td>
<td><lightning:input value="{!item.OwnerId}"/></td>
<td><lightning:input value="{!item.Owner.Name}"/></td>-->
<!--<td><ui:outputText value="{!item.Id}" /></td>-->
<td><a href="javascript:void(0);" onclick="{!c.Clicked}" data-value="{!item.Id}">{!item.Name}
</a></td>
<td><ui:outputText value="{!item.AccountNumber}"/></td>
<!--<td><ui:outputText value="{!item.OwnerId}"/></td>-->
</tr>
</aura:iteration>
<!-- <lightning:button label="Previous" disabled="{!v.start == 0}" onclick="{!c.previous}" />
<lightning:button label="Next" disabled="{!v.end >= v.totalSize}" onclick="{!c.next}" />-->
</tbody>
</table>
<lightning:button label="Previous" disabled="{!v.start == 0}" onclick="{!c.previous}" />
<lightning:button label="Next" disabled="{!v.end >= v.totalSize}" onclick="{!c.next}" />
<lightning:button label="Create New account" onclick="{!c.createRecord}" />
<div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<h1 class="slds-text-heading--medium">Contacts associated with account</h1>
</div>
</div>
</div>
<table class="slds-table slds-table--bordered ">
<thead>
<tr style="color:Grey;font-weight:bold">
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Contact Id</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Contact Name</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Account Id</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Account Name</h1></th>
</div>
</div>
</div></td>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.contacts}" var="con">
<tr>
<td><ui:outputText value="{!con.Id}" /></td>
<td><ui:outputText value="{!con.Name}" /></td>
<td><ui:outputText value="{!con.AccountId}" /></td>
<td><ui:outputText value="{!con.Account.Name}" /></td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
Accountpaginationcontroller.js
({
doInit : function(component, event, helper)
{
var pageSize = component.get("v.pageSize");
var action = component.get("c.getAccounts");
action.setCallback(this, function(response)
{
var state = response.getState();
if (component.isValid() && state === "SUCCESS")
{
console.log(response.getReturnValue());
component.set('v.acccountList', response.getReturnValue());
component.set("v.totalSize", component.get("v.acccountList").length);
component.set("v.start",0);
component.set("v.end",pageSize-1);
var paginationList = [];
for(var i=0; i< pageSize; i++)
{
paginationList.push(response.getReturnValue()[i]);
}
component.set('v.paginationList', paginationList);
}
});
$A.enqueueAction(action);
},
next : function(component, event, helper)
{
var accountList = component.get("v.acccountList");
var end = component.get("v.end");
var start = component.get("v.start");
var pageSize = component.get("v.pageSize");
var paginationList = [];
var counter = 0;
for(var i=end+1; i<end+pageSize+1; i++)
{
if(accountList.length > end)
{
paginationList.push(accountList[i]);
counter ++ ;
}
}
start = start + counter;
end = end + counter;
component.set("v.start",start);
component.set("v.end",end);
component.set('v.paginationList', paginationList);
},
previous : function(component, event, helper)
{
var accountList = component.get("v.acccountList");
var end = component.get("v.end");
var start = component.get("v.start");
var pageSize = component.get("v.pageSize");
var paginationList = [];
var counter = 0;
for(var i= start-pageSize; i < start ; i++)
{
if(i > -1)
{
paginationList.push(accountList[i]);
counter ++;
}
else
{
start++;
}
}
start = start - counter;
end = end - counter;
component.set("v.start",start);
component.set("v.end",end);
component.set('v.paginationList', paginationList);
},
Clicked : function(component, event, helper){
var ctarget = event.currentTarget;
var id_str = ctarget.dataset.value;
console.log(id_str);
var action = component.get("c.getContactRecords");
action.setParams({ accId : id_str});
action.setCallback(this, function(response) {
var state = response.getState(); //Checking response status
console.log("contactsss... "+JSON.stringify(response.getReturnValue()));
if (component.isValid() && state === "SUCCESS"){
if(response.getReturnValue() !=''){
component.set("v.contacts", response.getReturnValue());
}
else{
alert('contacts are not available for this account');
// Adding values in Aura attribute variable.
}
} });
$A.enqueueAction(action);
},
createRecord: function(component, event, helper) {
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
"entityApiName": "Account" // using account standard object for this sample
});
createRecordEvent.fire();
}
})
accountpagination.apxc
public with sharing class AccountPagination
{
/*@AuraEnabled
public static List<Account> getAccounts()
{
List<Account> accs = [SELECT Id, Name,Phone,AccountNumber,BillingAddress,Fax,OwnerId,Owner.Name,(SELECT Id,FirstName,LastName,Email from contacts) FROM Account];
return accs;
}*/
@AuraEnabled
public static List<Account> getAccounts() {
return new List<Account>([SELECT Id, Name,Phone,AccountNumber,BillingAddress,Fax,OwnerId,Owner.Name FROM Account]);
}
@AuraEnabled
public static List<Contact> getContactRecords(string accId) {
return new List<Contact>([Select id,Name,Account.Name,AccountId from Contact Where AccountId =: accId LIMIT 100]);
}
}
accountpaginationapp.app:
<aura:application >
<c:AccountPagination/>
</aura:application>
Can anyone please help
I am new to lightning, can some one please help me with the below code:
I have my existing code that displays accounts and when clicked particular account it shows its own contacts.
Now, on the same page there should be one button for Add Account. On click of this button, popup will ask for account details and after click on submit button this new account will add to database, go to previous page and previous page also will refresh.
when I click on preview I get below error:
Below is my code:
component:
<aura:component controller="AccountPagination" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<ltng:require styles="/resource/SLDS244/styles/salesforce-lightning-design-system.css" />
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<h1 class="slds-text-heading--medium">Accounts List</h1>
</div>
</div>
</div>
<aura:attribute name="acccountList" type="Account[]" />
<aura:attribute name="contacts" type="Contact[]"/>
<aura:attribute name="paginationList" type="Account[]"/>
<aura:attribute name="pageSize" type="Integer" default="15"/>
<aura:attribute name="totalSize" type="Integer"/>
<aura:attribute name="start" type="Integer" />
<aura:attribute name="end" type="Integer"/>
<table class="slds-table slds-table--bordered ">
<thead>
<tr style="color:Grey;font-weight:bold">
<!--<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading-/-medium">Id</h1></th>
</div>
</div>
</div></td>-->
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Name</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">AccountNumber</h1></th>
</div>
</div>
</div></td>
<!--<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading-/-medium">AccountOwnerId</h1></th>
</div>
</div>
</div></td>-->
<!--<th>Id</th>
<th>Name</th>
<th>Phone</th>
<th>AccountNumber</th>
<th>BillingAddress</th>-->
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.paginationList}" var="item">
<tr>
<!--<td><lightning:input value="{!item.Id}"/></td>
<td><lightning:input value="{!item.Name}"/></td>
<td><lightning:input value="{!item.AccountNumber}"/></td>
<td><lightning:input value="{!item.OwnerId}"/></td>
<td><lightning:input value="{!item.Owner.Name}"/></td>-->
<!--<td><ui:outputText value="{!item.Id}" /></td>-->
<td><a href="javascript:void(0);" onclick="{!c.Clicked}" data-value="{!item.Id}">{!item.Name}
</a></td>
<td><ui:outputText value="{!item.AccountNumber}"/></td>
<!--<td><ui:outputText value="{!item.OwnerId}"/></td>-->
</tr>
</aura:iteration>
<!-- <lightning:button label="Previous" disabled="{!v.start == 0}" onclick="{!c.previous}" />
<lightning:button label="Next" disabled="{!v.end >= v.totalSize}" onclick="{!c.next}" />-->
</tbody>
</table>
<lightning:button label="Previous" disabled="{!v.start == 0}" onclick="{!c.previous}" />
<lightning:button label="Next" disabled="{!v.end >= v.totalSize}" onclick="{!c.next}" />
<lightning:button label="Create New account" onclick="{!c.createRecord}" />
<div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<h1 class="slds-text-heading--medium">Contacts associated with account</h1>
</div>
</div>
</div>
<table class="slds-table slds-table--bordered ">
<thead>
<tr style="color:Grey;font-weight:bold">
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Contact Id</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Contact Name</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Account Id</h1></th>
</div>
</div>
</div></td>
<td> <div class="slds-page-header" role="banner" >
<div class="slds-grid">
<div class="slds-col" >
<th><h1 class="slds-text-heading--medium">Account Name</h1></th>
</div>
</div>
</div></td>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.contacts}" var="con">
<tr>
<td><ui:outputText value="{!con.Id}" /></td>
<td><ui:outputText value="{!con.Name}" /></td>
<td><ui:outputText value="{!con.AccountId}" /></td>
<td><ui:outputText value="{!con.Account.Name}" /></td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
Accountpaginationcontroller.js
({
doInit : function(component, event, helper)
{
var pageSize = component.get("v.pageSize");
var action = component.get("c.getAccounts");
action.setCallback(this, function(response)
{
var state = response.getState();
if (component.isValid() && state === "SUCCESS")
{
console.log(response.getReturnValue());
component.set('v.acccountList', response.getReturnValue());
component.set("v.totalSize", component.get("v.acccountList").length);
component.set("v.start",0);
component.set("v.end",pageSize-1);
var paginationList = [];
for(var i=0; i< pageSize; i++)
{
paginationList.push(response.getReturnValue()[i]);
}
component.set('v.paginationList', paginationList);
}
});
$A.enqueueAction(action);
},
next : function(component, event, helper)
{
var accountList = component.get("v.acccountList");
var end = component.get("v.end");
var start = component.get("v.start");
var pageSize = component.get("v.pageSize");
var paginationList = [];
var counter = 0;
for(var i=end+1; i<end+pageSize+1; i++)
{
if(accountList.length > end)
{
paginationList.push(accountList[i]);
counter ++ ;
}
}
start = start + counter;
end = end + counter;
component.set("v.start",start);
component.set("v.end",end);
component.set('v.paginationList', paginationList);
},
previous : function(component, event, helper)
{
var accountList = component.get("v.acccountList");
var end = component.get("v.end");
var start = component.get("v.start");
var pageSize = component.get("v.pageSize");
var paginationList = [];
var counter = 0;
for(var i= start-pageSize; i < start ; i++)
{
if(i > -1)
{
paginationList.push(accountList[i]);
counter ++;
}
else
{
start++;
}
}
start = start - counter;
end = end - counter;
component.set("v.start",start);
component.set("v.end",end);
component.set('v.paginationList', paginationList);
},
Clicked : function(component, event, helper){
var ctarget = event.currentTarget;
var id_str = ctarget.dataset.value;
console.log(id_str);
var action = component.get("c.getContactRecords");
action.setParams({ accId : id_str});
action.setCallback(this, function(response) {
var state = response.getState(); //Checking response status
console.log("contactsss... "+JSON.stringify(response.getReturnValue()));
if (component.isValid() && state === "SUCCESS"){
if(response.getReturnValue() !=''){
component.set("v.contacts", response.getReturnValue());
}
else{
alert('contacts are not available for this account');
// Adding values in Aura attribute variable.
}
} });
$A.enqueueAction(action);
},
createRecord: function(component, event, helper) {
var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
"entityApiName": "Account" // using account standard object for this sample
});
createRecordEvent.fire();
}
})
accountpagination.apxc
public with sharing class AccountPagination
{
/*@AuraEnabled
public static List<Account> getAccounts()
{
List<Account> accs = [SELECT Id, Name,Phone,AccountNumber,BillingAddress,Fax,OwnerId,Owner.Name,(SELECT Id,FirstName,LastName,Email from contacts) FROM Account];
return accs;
}*/
@AuraEnabled
public static List<Account> getAccounts() {
return new List<Account>([SELECT Id, Name,Phone,AccountNumber,BillingAddress,Fax,OwnerId,Owner.Name FROM Account]);
}
@AuraEnabled
public static List<Contact> getContactRecords(string accId) {
return new List<Contact>([Select id,Name,Account.Name,AccountId from Contact Where AccountId =: accId LIMIT 100]);
}
}
accountpaginationapp.app:
<aura:application >
<c:AccountPagination/>
</aura:application>
Can anyone please help
Greetings to you!
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Component:
Controller:
CSS:
Apex:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Thank you so much for your response.
My requirement is partially solved and got output with out errors.
when I click on create account nothing happens ,
requirement is when clicked on the button, a pop up should open and ask for mandatory details and when clicked save , the new account should visible on the list of existing accounts, when clicked cancel the pop up should be closed.
Thanks,
Haneef
The above code which I have shared will fulfill your requirement. You can see I have used a lightning button with name Create Account PopUp. If you click on it, a popup will open with Account's input fields.
Please go through my code, it will definitely solve your problem. I have checked it in my org, it is working fine.
Regards,
Khan Anas