0

I am using code that someone else worked on. From the code, I can tell it is smart enough to recognize that the record it is processing is the current record for the lightning component. How can I then modify the name to explicitly say that for my users?

if(so.get('id') == currentRecordId seems to be the key part. so.get('name') also seems to work. How can I change the value in so.get('name')?

This is the apex controller:

//Run query and store results
    List<sObject> queryOutput = new List<sObject>();        
    queryOutput = database.query(finalQueryString);        

    //Loop through the values
    Integer level = 0;
    for(sObject so: queryOutput){
        HierarchyData ss = new HierarchyData();
        if(so.get('id') == currentRecordId /*|| level == 0*/){
            ss.expanded = true;
            //This next line is where I am trying to modify the name.
            so.put(so.get('name'), so.get('name')+': Current Record');
        }else{
            ss.expanded = false;
        }
        ss.rec = so;
        returnValue.add(ss);
        level++;
    }

    //return values to the component
    system.debug('**********returnValue:'+returnValue);
    return returnValue;
}

//Wrapper class
public class HierarchyData{
    @AuraEnabled
    public sObject rec{get;set;}
    @AuraEnabled
    public boolean expanded{get;set;}
    public HierarchyData(){
        expanded = false;
    }
}        

The top part of the code is just defining the variable finalQueryString, which works as expected.

6
  • What happens when you do this? Not all sObjects have mutable Name fields, and I'm not sure this is the optimal solution for your objective.
    – David Reed
    Mar 20, 2018 at 15:05
  • I am using this on custom objects that all have name fields. I thought all objects in SF have the name and id field? @battery.cord had the solution as putting just so.put('Name', 'Some Value'); and that works. What do you recommend?
    – Chance
    Mar 20, 2018 at 15:17
  • 2
    I'm not sure what you're doing is the best way to even do what you're doing. It appears that you're expanding/collapsing elements in a tree or table using server-side code. This should be done client-side. It would also help alleviate the problem with trying to set the name, which may not be writable in all cases in Apex Code. Further, I'd generally advise just creating a variable in your wrapper to show the modified text. This is all conjecture based on what I can see, but I suspect you need some refactoring.
    – sfdcfox
    Mar 20, 2018 at 15:17
  • @sfdcfox this is all used to create a lightning:tree. This data is then run through client side stuff too, and it seems to use 'expanded' as a flag. I tried to solve this first client side, but I'd be lying if I said I understood exactly what all is going on. Simply put, I don't have any other way to dynamically create the tree structure I need, and I lack the skills to write it myself.
    – Chance
    Mar 20, 2018 at 15:21
  • 2
    @Chance I have an old-school (pre-lightning:tree) tree demo you can look at here. I dunno if it'll help, but there you go.
    – sfdcfox
    Mar 20, 2018 at 15:26

0

You must log in to answer this question.

Browse other questions tagged .