Skip to content

Instantly share code, notes, and snippets.

@martyychang
Created November 8, 2014 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martyychang/0d855a144d2e3a850a91 to your computer and use it in GitHub Desktop.
Save martyychang/0d855a144d2e3a850a91 to your computer and use it in GitHub Desktop.
It appears that instead of setting a null property to null when returning an sObject via Apex, the property simply isn't set at all and creates odd problems in the UI
<aura:application controller="ccmt.NullSObjectPropertyBugDemoController">
<aura:attribute name="thisLead" type="Lead"
default="{ 'sobjectType': 'Lead' }"/>
<aura:attribute name="leads" type="Lead[]"/>
<!-- Event handlers -->
<aura:handler name="init" value="{!this}" action="{!c.handleInit}"/>
<!-- App view header -->
<h1><code>null</code> sObject Property Bug Demo</h1>
<!-- List leads -->
<ul>
<aura:iteration items="{!v.leads}" var="lead">
<li>
<a onclick="{!c.selectLead}"
href="#" data-record-id="{!lead.Id}">{!lead.Name}</a>
</li>
</aura:iteration>
</ul>
<!-- Edit this lead -->
<ui:inputText label="Lead ID" value="{!v.thisLead.Id}" disabled="true"/>
<ui:inputText label="First Name" value="{!v.thisLead.FirstName}"/>
<ui:inputText label="Last Name" value="{!v.thisLead.LastName}"/>
<ui:inputText label="Company" value="{!v.thisLead.Company}"/>
<ui:inputPhone label="Phone" value="{!v.thisLead.Phone}"/>
<ui:inputEmail label="Email" value="{!v.thisLead.Email}"/>
<ui:button label="Save" press="{!c.save}"/>
<ui:button label="New" press="{!c.reset}"/>
</aura:application>
public class NullSObjectPropertyBugDemoController {
@AuraEnabled
public static Id editLead(Lead newLead) {
Id newLeadId = null;
try {
upsert newLead;
newLeadId = newLead.Id;
}
catch (System.Exception caught) {
ApexPages.addMessages(caught);
}
return newLeadId;
}
@AuraEnabled
public static Lead getLead(Id leadId) {
List<Lead> matchingLeads = [
SELECT Id,
Company,
Email,
FirstName,
LastName,
Phone
FROM Lead
WHERE Id = :leadId
];
return matchingLeads.isEmpty()
? null : matchingLeads.get(0);
}
@AuraEnabled
public static List<Lead> getLeads() {
return [
SELECT Id, Name
FROM Lead
ORDER BY LastModifiedDate DESC
LIMIT 10
];
}
@AuraEnabled
public static Lead newLead() {
return (Lead)Lead.sObjectType.newSObject(null, true);
}
}
({
handleInit : function(component, event, helper) {
helper.initThisLead(component);
helper.refreshLeads(component);
},
reset : function(component, event, helper) {
helper.initThisLead(component);
},
save : function(component, event, helper) {
var self = this; // safe reference
// Create an action to save the values entered
// for thisLead, which will result in an insert
// or update operation in Salesforce depending
// on whether the lead is new or pre-existing
var saveAction = component.get("c.editLead");
saveAction.setParams({
"newLead": component.get("v.thisLead")
});
saveAction.setCallback(self, function(a) {
var recordId = a.getReturnValue();
if (recordId != null) {
component.set("v.thisLead.Id", recordId);
helper.refreshLeads(component);
}
});
// Enqueue the action
$A.enqueueAction(saveAction);
},
selectLead : function(component, event, helper) {
var self = this; // safe reference
var recordId = event.target.dataset.recordId;
// Create an action to retrieve the specified
// Lead, based on the given salesforce.com ID
var selectAction = component.get("c.getLead");
selectAction.setParams({
"leadId": recordId
});
selectAction.setCallback(self, function(a) {
component.set("v.thisLead", a.getReturnValue());
});
// Enqueue the action
$A.enqueueAction(selectAction);
}
})
({
initThisLead : function(component) {
var self = this; // safe reference
// Create an action to set thisLead to a new,
// fresh Lead object obtained via Apex
var initAction = component.get("c.newLead");
initAction.setCallback(self, function(a) {
component.set("v.thisLead", a.getReturnValue());
});
// Enqueue the action
$A.enqueueAction(initAction);
},
refreshLeads : function(component) {
var self = this; // safe reference
// Create an action to retrieve leads from
// Salesforce and set the leads attribute to
// whatever is retrieved
var refreshAction = component.get("c.getLeads");
refreshAction.setCallback(self, function(a) {
component.set("v.leads", a.getReturnValue());
});
// Enqueue the action
$A.enqueueAction(refreshAction);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment