I am trying to default Site_Name__c for an attribute "SEComp" from other attribute.
It is giving compilation error as Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}
It appears I am getting stuck defaulting from other attribute but default just works fine if I say 'Site_Name__c': 'Abc'
What is the right way of defaulting for this?
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasSObjectName" controller="KF_CompanyLookupController">
<ltng:require styles="/resource/SLDS231/assets/styles/salesforce-lightning-design-system.css"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="SEComp" type="Searcher_Express_Company__c"
default="{'sobjectType' : 'Searcher_Express_Company__c',
'Site_Name__c': {!v.defaultSite.siteName}}"/>
<aura:attribute name="defaultSite" type="KF_CompanyLookupController.Site"/>
helper class
getUserSite : function(component){
var action = component.get("c.getdefaultSite");
action.setCallback(this,function(response){
var state = response.getState();
if (state === "SUCCESS") {
component.set("v.defaultSite",response.getReturnValue());
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +
errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
$A.enqueueAction(action);
}
Apex controller
public class Site {
@AuraEnabled
public String siteName {get;set;}
@AuraEnabled
public String siteID {get;set;}
public Site (){
this.siteName = siteName;
this.siteID = siteID;
}
}
@AuraEnabled
public static Site getdefaultSite(){
Site dSite = new Site();
User currentUser = [select Division, Country, Office__c from User where id = :UserInfo.getUserId()];
system.debug('currentUser - Division: ' + currentUser.Division + ' Country: ' + currentUser.Country + ' Office__c: ' + currentUser.Office__c);
if (currentUser.Division == 'Futurestep') {
// 1. check to see if user is FS
dSite.siteID = '50';
dSite.siteName = 'Futurestep';
} else if (currentUser.Country == 'United States' || currentUser.Country == 'Canada' || currentUser.Country == 'Brazil') {
// 2. check to see if user is in NA
dSite.siteID = '0';
dSite.siteName = 'North America';
} else {
String userOffice = currentUser.Office__c;
if (!String.isEmpty(userOffice)) {
List<Business_Rule_Processor__c> defaultSites = [Select Site_Name__c,Site_Id__c from Business_Rule_Processor__c
where RecordType.Name = 'Default Site'
AND Type__c = 'Default Site'
AND Active__c = true
AND Office__c includes (:userOffice)];
if (defaultSites.size() > 0) {
string defaultSiteName = defaultSites[0].Site_Name__c;
String defaultSiteId = defaultSites[0].Site_Id__c;
if (!String.isEmpty(defaultSiteName)) {
dSite.siteName = defaultSiteName;
dSite.siteID = defaultSiteId;
}
}
}
}
System.debug('dSite value is'+dSite);
return dSite;
}
Best Answer
Since Expressions are not allowed in your attributes, you could set it on init: