-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathConnect SF with Server side controllers
144 lines (115 loc) · 5.54 KB
/
Connect SF with Server side controllers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
1.campingList.cmp
<aura:component controller="campingListController" >
<aura:attribute name="items" type="Camping_Item__c[]" />
<aura:attribute name="newItem" type="Camping_Item__c" default="{'Price__c': 0, 'Packed__c': false, 'Quantity__c': 0, 'Name':'Test', 'sobjectType': 'Camping_Item__c'}" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:layout>
<lightning:layoutItem padding="around-small" size="4">
<!-- CREATE NEW EXPENSE -->
<div aria-labelledby="newCampingItem">
<fieldset class="slds-box slds-theme--default slds-container--small">
<legend id="newCampingItem" class="slds-text-heading--small
slds-p-vertical--medium">
Add Camping Item
</legend>
<!-- CREATE NEW EXPENSE FORM -->
<form class="slds-form--stacked">
<lightning:input aura:id="campingItemForm" label="Name"
name="itemName"
value="{!v.newItem.Name}"
required="true"/>
<lightning:input type="number" aura:id="campingItemForm" label="Quantity"
name="itemQuantity"
min="1"
step="1"
value="{!v.newItem.Quantity__c}"
messageWhenRangeUnderflow="Enter the quantity atleast 1"/>
<lightning:input type="number" aura:id="campingItemForm" label="Price"
name="itemPrice"
min="0.1"
formatter="currency"
step="0.01"
value="{!v.newItem.Price__c}" />
<lightning:input type="checkbox" aura:id="campingItemForm" label="Packed ?"
name="itemPacked"
checked="{!v.newItem.Packed__c}"/>
<lightning:button label="Create Item"
class="slds-m-top--medium"
variant="brand"
onclick="{!c.clickCreateItem }"/>
</form>
<!-- / CREATE NEW EXPENSE FORM -->
</fieldset>
</div>
<!-- / CREATE NEW EXPENSE -->
</lightning:layoutItem>
<lightning:layoutItem padding="around-small" size="3">
<aura:Iteration items="{!v.items}" var="item">
<c:campingListItem item="{!item}" />
</aura:Iteration>
</lightning:layoutItem>
</lightning:layout>
<!--
<ol>
<li>Bug Spray</li>
<li>Bear Repellant</li>
<li>Goat Food</li>
</ol>
-->
</aura:component>
2.campingListHelper.js
({
createItem : function(component,newCampingItem) {
var action = component.get("c.saveItem");
action.setParams({
"campingItem" : newCampingItem
});
action.setCallback(this,function(response){
var state = response.getState();
if (state === "SUCCESS") {
var parsedCampingItem = JSON.parse(JSON.stringify(newCampingItem));
console.log(JSON.parse(JSON.stringify(parsedCampingItem)), JSON.stringify(parsedCampingItem));
var campingItems = JSON.parse(JSON.stringify(component.get("v.items")));
campingItems.push(parsedCampingItem);
component.set("v.items",campingItems);
component.set("v.newItem", {'Price__c': 0, 'Packed__c': false, 'Quantity__c': 0, 'Name':'', 'sobjectType': 'Camping_Item__c'})
}
});
$A.enqueueAction(action);
}
})
3.campingListController.js
({
clickCreateItem : function(component, event, helper) {
var isFormValid = component.find("campingItemForm").reduce(function(isValid, inputCmp){
inputCmp.showHelpMessageIfInvalid();
return isValid && inputCmp.get("v.validity").valid;
});
if (isFormValid) {
var newCampingItem = component.get("v.newItem");
helper.createItem(component,newCampingItem);
}
},
doInit : function (component, event, helper) {
var action = component.get("c.getItems");
action.setCallback(this,function (response) {
var campingItems = response.getReturnValue();
component.set("v.items",campingItems);
});
$A.enqueueAction(action);
}
})
4.campingListController.apxc
public with sharing class campingListController {
//for insert item
@AuraEnabled
public static void saveItem (Camping_Item__c campingItem) {
insert campingItem;
}
//fetch record and return as a list
// @AuraEnabled
public static List<Camping_Item__c> getItems() {
List<Camping_Item__c> campingItems = [SELECT Id,Name,Price__c,Packed__c,Quantity__c FROM Camping_Item__c];
return campingItems;
}
}