1

I'm having some difficulty with lists and apex.

I have a list declared in my lightning component, please see below:

<aura:attribute name="AssetsList" type="List" default="[]"/>

I then set some attributes in the controller upon intialisation. I have two input fields in the component, the first 3 attributes apply to one input and the last 3 apply to the other. Please see this below:

component.set('v.AssetsList', {'TypeLabel': 'Asset Type', 'TypeValue': '','TypeId': 'AssetType1', 
                               'AmountLabel': 'Asset Amount', 'AmountValue': '', 'AmountId': 'AssetAmount1'});

I have a save button in the component and when it is clicked it will run a method in the controller where I pass the list to the apex as a parameter.

    var AssetList= cmp.get('v.AssetsList');
    var FactfindId = cmp.get('v.ffRecord.Id');
    var inputAssetListAction = cmp.get("c.inputAssetList");

    inputAssetListAction.setParams({
        "AssetList": AssetList,
        "FactfindId": FactfindId
    });

    inputAssetListAction.setCallback(this, function (response) {
        //store state of response
        var state = response.getState();
        if (state === "SUCCESS") {
            console.log(state);
            if (response.getReturnValue() != null) {
                // if getting any error while update the records , then display a alert msg/
                alert('The following error has occurred. while updating record-->' + response.getReturnValue());
            } else {
                console.log('check it--> update successful');
            }
        }
    });
    $A.enqueueAction(inputAssetListAction);   

Once this parameter is in the apex, I'm then trying to insert a record to a custom object. I currently have this code.

public static void inputAssetList(list<String> AssetList, string FactfindId) {

    FactfindMultiples__c[] Assets = new List<FactfindMultiples__c>();

    for(Integer i=1;i<AssetList.size();i++)
    {
        FactfindMultiples__c Asset = new FactfindMultiples__c(Name='Asset' + i, 
                                                              AssetType__c= AssetList[i].TypeValue);
        Assets.add(Asset);

    }
}

I'm going through the Asset List and trying to push it into a new record for my custom object. Once this is done, I'll then add to a list of the new custom object which I'll insert.

The issue I'm having is for when I'm setting the AssetType__c custom field, I don't want to push every attribute for each part of the array. I just want to use the TypeValue attribute that I set in the list upon initialisation.

I've tried syntax like AssetList[i].TypeValue but this returns errors.

Any help would be massively appreciated.

Thank you,

Owen

  • check attribute name which should be the same AssetsType, and are you getting AssestList in Apex? try with debug log – Sarvesh Mar 9 at 8:32
0

The component you are initializing in js controller is v.AssetsType where the attribute name for the list is different "AssetsList". shouldn't these be same. I think you just need to update attribute name to AssetsType.

<aura:attribute name="AssetsType" type="List" default="[]"/>
| improve this answer | |
  • Apologies I've made this change and I'm still having the issue. – Owen Mar 10 at 9:38
  • Console log the list in JavaScript controller before passing it to the apex controller and also debug log the AssetsType list in apex controller to see if you are getting list in apex or not – Bilal Malik Mar 10 at 10:10
  • Hi, I've checked it in the controller and I'm getting an output in my console. When I go to the controller and try and show it a system.debug I can't find anything in my logs. – Owen Mar 10 at 11:12
  • Looks like its not getting passed to your apex controller. Data in AssetsType is more like a map. You can change list<String> AssetList input parameter to a map like this Map<String, String> AssetList and see if it logs something in apex debug logs. – Bilal Malik Mar 10 at 11:31
  • Thank you so much for the help so far, sorry but I am inexperienced with the use of maps. I am now getting simple static text outputs in my logs. I guess the next step would to try and print a value of the map into a log. Do you know how I'd do this? Once again thanks for all the help. – Owen Mar 12 at 8:54

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.