0

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;
}
  • You would have to populated the attribute from init or any client side controller. This kind of value cannot be set inside aura:attribute – Raul Nov 27 '17 at 11:08
  • you cannot directly set Site_Name__c using value provider 'Site_Name__c': {!v.defaultSite.siteName}}" in attribute section . instead Set the value via doinit. – vignesh Nov 27 '17 at 11:08
  • 2
    wow, 3 responses at almost same time. Its cool, keep up the good work guys! – Raul Nov 27 '17 at 11:10
  • 1
    In Lightning, an expression must be the entire attribute value if you use it at all. In addition, I would expect that trying to use that syntax might have unintended side effects even if it would work, because {!...} is a two-way binding, which means there might be unexpected side effects. Basti's answer is one possible correction (you could do it other ways, too), but I just wanted to provide a bit more of an explanation. – sfdcfox Nov 27 '17 at 14:40
  • Thank you all for your inputs. Answer provided by Basti helped me to nail this down. – user28452 Nov 27 '17 at 15:08
1

Since Expressions are not allowed in your attributes, you could set it on init:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

({
    doInit: function(cmp) {
        var record = cmp.get("v.SEComp");
        record.Site_Name__c = cmp.get("v.defaultSite").siteName;

        cmp.set("v.SEComp", record);
    }
})
  • Can you share your final code? How did you set site_name__c – GastonProbo Jan 9 '18 at 2:22
  • @GastonProbo Not sure if I understand your question, in the third line of the controller in the given example the field is set. – Basti Jan 9 '18 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.