7
SaveAccessRequest : function(component, event, helper) {
    var ARLIlist = component.get("v.AccessRequestLineItems");
    var action = component.get("c.getSaveRecord");
    var accessreq = component.get("v.AccessRequest");
    action.setParams({"AR":accessreq,
                     "ARLI":ARLIlist});
    $A.enqueueAction(action);
}

Here "AccessRequestLineItems" & "AccessRequest" is an sobject array & sobject defined on the component. while I invoke this function neither received an error nor the function on apex is invoked. can anyone help me in resolving the issue?

Apex code

 @AuraEnabled
public static void getSaveRecord(Access_Request__c AR,List<Access_Request_Line_Item__c> ARLI){
    system.debug('====>'+ARLI);
    Insert AR;
    system.debug('====>'+ARLI);
    for(Integer i=0; i<ARLI.size(); i++){
        ARLI[i].Access_Request_ID__c = AR.id;
    }
    Insert ARLI;
    system.debug('====>'+ARLI[0].id);

}

Also tried with return type

 @AuraEnabled
public static Access_Request__c getSaveRecord(Access_Request__c AR,List<Access_Request_Line_Item__c> ARLI){
    system.debug('====>'+ARLI);
    Insert AR;
    system.debug('====>'+ARLI);
    for(Integer i=0; i<ARLI.size(); i++){
        ARLI[i].Access_Request_ID__c = AR.id;
    }
    Insert ARLI;
    system.debug('====>'+ARLI[0].id);
    return AR;
}
12
  • You're not setting a callback, so how do you know it's not doing anything? – sfdcfox Nov 8 '16 at 4:44
  • @sfdcfox I have debug logs in the apex. I could have received logs once the function is invoked. – Venky Nov 8 '16 at 4:48
  • did you add controller in component. Because it will not give any error if you not add that. Also share your apex controller code. – Tushar Sharma Nov 8 '16 at 5:04
  • @TusharSharma Yes, i have added controller on the component. – Venky Nov 8 '16 at 5:07
  • @TusharSharma Added controller. – Venky Nov 8 '16 at 5:33
6
+50

Take a look at this post. I was able to pass the sobject and sobject array using json.encode util.I have used account sobject for the sake of testing.

Passing a sobject param was/ is causing error in Lightning: https://success.salesforce.com/issues_view?id=a1p300000008XW6AAM

component:

<aura:component controller="Saveaccesscontroller">
    <aura:attribute name="AccountItems" type="Account[]" />
    <aura:attribute name="Accountval" type="Account" default="{ 'sobjectType': 'Account',
                         'Name': 'test'}"/>

     <button type="button" onclick="{!c.SaveAccessRequest}">Click Me!</button> 
</aura:component>

controller: I have queried the account list and passed it to the getReturnValue method in the server side controller.

({
    SaveAccessRequest : function(component, event, helper) {      
        var ARLIlist = component.get("v.AccountItems");
        var accessreq = component.get("v.Accountval");
        var queryaction = component.get("c.queryRecord");
        queryaction.setCallback(this, function(response) {            
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.AccountItems",response.getReturnValue());
                var action = component.get("c.getSaveRecord");
                action.setParams({"AR":$A.util.json.encode(accessreq),
                                  "ARLI":$A.util.json.encode(component.get("v.AccountItems"))});
                $A.enqueueAction(action);
             }
        });    
        $A.enqueueAction(queryaction);              

        }
})

Apex controller:

Define the param as string and use the convertJSONToListOfSObject method to from the post above to convert the string to list

 public class Saveaccesscontroller {
    @AuraEnabled
    public static void getSaveRecord(String AR,String ARLI){
        system.debug('====>'+ARLI);
         List<SObject> newSObjectsList = convertJSONToListOfSObject(ARLI);
        system.debug('====>'+newSObjectsList);
        system.debug('====>'+AR);
    }
    @AuraEnabled
    public static List<account> queryRecord(){
        return [select id,name from Account limit 5];
    }

     private static List<SObject> convertJSONToListOfSObject(String json) {
        object[] values = (object[])System.JSON.deserializeUntyped(json);

        List<SObject> newSObjectsList = new List<SObject>();
        for (Object v : values) {
            Map<String, Object> m = (Map<String, Object>)v;
            Schema.SObjectType targetType = Schema.getGlobalDescribe().get((String)m.get('sobjectType'));

            SObject o = targetType.newSObject();

            Map<String, Schema.SObjectField> fields = targetType.getDescribe().fields.getMap();
            for (String fieldName : m.keySet()) {
                // Filter out any psuedo fields such as LastNameLocal
                Schema.SObjectField fi = fields.get(fieldName);
                if (fi != null) {
                    if (fi.getDescribe().isCreateable() && fi.getDescribe().isUpdateable()) {
                        o.put(fieldName, m.get(fieldName)); 
                    }
                }
            }

            newSObjectsList.add(o);
        }

        return newSObjectsList;
    }
}

Debug logs:

09:38:31.3 (4142574)|USER_DEBUG|[4]|DEBUG|====>[{"Id":"001d000001xDnrmAAC","Name":"Smith Enterprises"},{"Id":"001d0000026DD5JAAW","Name":"test"},{"Id":"001d000001StO98AAF","Name":"abc"},{"Id":"001d000001StOT1AAN","Name":"abc"},{"Id":"001d000000ySkIvAAK","Name":"TEST"}] 09:38:31.3 (4172328)|STATEMENT_EXECUTE|[5] 09:38:31.3 (4180526)|HEAP_ALLOCATE|[5]|Bytes:44 09:38:31.3 (4189407)|ENTERING_MANAGED_PKG| 09:38:31.3 (4198943)|USER_DEBUG|[5]|DEBUG|====>{"sobjectType":"Account","Name":"test"}

8
  • Hi Rao, i got this error on using $A.util.json.encode. This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'encode' of undefined] Failing descriptor: {markup://c:AccessRequest_cmp} – Venky Nov 9 '16 at 4:29
  • FYI I'm trying to pass a sobject list to apex, not a string. Thank you for quick responses. – Venky Nov 9 '16 at 4:39
  • @Venky I have passed the sobject (Account) list, look at the AccountItems attribute in the component, I have passed it as a string to the controller byusing json.encode utility. For some reason the array of account is coming up as null, same issue I faced when I was passing a date for which I converted the date into string and type casted it later in the apex controller to be a date. – Rao Nov 9 '16 at 17:39
  • Is there any mistake in below code. – Venky Nov 10 '16 at 4:36
  • Do you ask if there is error in the code i poated as an answer?? I will double check it but as far as i know i did a copy paste – Rao Nov 10 '16 at 4:46
2

Component:

    <aura:component controller="Saveaccesscontroller">
    <aura:attribute name="AccountItems" type="Account[]" default="{'name' : 'karthik'}" />
    <aura:attribute name="Accountval" type="Account" default="{ 'sobjectType': 'Account',
                                                              'Name': 'test'}"/>
    <ui:button label="ClickMe" press="{!c.SaveAccessRequest}"/>
</aura:component>

controller:

I have queried the account list and passed it to the getReturnValue method in the server side controller.

    ({
    SaveAccessRequest : function(component, event, helper) {      
        var ARLIlist = component.get("v.AccountItems");
        var accessreq = component.get("v.Accountval");
        var queryaction = component.get("c.queryRecord");
        queryaction.setCallback(this, function(response) {            
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.AccountItems",response.getReturnValue());
                var ARLIlist = component.get("v.AccountItems");
                var action = component.get("c.getSaveRecord");
                action.setParams({"AR":accessreq,
                                  "ARLI":component.get("v.AccountItems")});
                action.setCallback(this, function(response) {
                    var state = response.getState();
                }) 
                $A.enqueueAction(action);
            }
        });    
        $A.enqueueAction(queryaction);              

    }
})

Apex code:

Here i pass sObject List into Apex

    public class Saveaccesscontroller {

    @AuraEnabled
    public static void getSaveRecord(Account AR,List<Account> ARLI){
        system.debug('====>'+AR);
        Insert AR;
        system.debug('AccountList====>'+ARLI); 

        for(Integer i=0; i<ARLI.size(); i++){
            ARLI[i].Name = AR.Name;
        }
        update ARLI;
        system.debug('====>'+ARLI[0].Name);
    }

    @AuraEnabled
    public static List<account> queryRecord(){
        return [select id,name from Account limit 5];
    }

}
2

I did this few days back. Simply just by stringifying & deserializing the list.

Pass your params as string using JSON.stringify function.

SaveAccessRequest : function(component, event, helper) {
    var ARLIlist = component.get("v.AccessRequestLineItems");
    var action = component.get("c.getSaveRecord");
    var accessreq = component.get("v.AccessRequest");
var ARLIlistAsString = JSON.stringify(ARLIlist);
var accessreqAsString = JSON.stringify(accessreq);
    action.setParams({"ARAsString":accessreqAsString,
                     "ARLIAsString":ARLIlistAsString});
    $A.enqueueAction(action);
}

Update your apex method like this: Take the inputs as string and use JSON.deserialize to convert it back to Object or List.

@AuraEnabled
public static void getSaveRecord(String ARAsString, String ARLIAsString){

Access_Request__c AR = JSON.deserialize(ARAsString, Access_Request__c.class);
List<Access_Request_Line_Item__c> ARLI = JSON.deserialize(ARLIAsString, List<Access_Request_Line_Item__c>.class);
    Insert AR;
    system.debug('====>'+ARLI);
    for(Integer i=0; i<ARLI.size(); i++){
        ARLI[i].Access_Request_ID__c = AR.id;
    }
    Insert ARLI;
    system.debug('====>'+ARLI[0].id);

}
0

I am doing the similar thing in my component. Instead of passing of type List I am passing JSON string to method. Convert your list attribute into JSON string in lightning code (JSON.stringify()) and pass it to apex method as method parameter (String). In apex method deserialize the JSON string into required List.

This is working fine for me. In the same way I am passing List of object from component to apex methods. JSON string > Apex method > deserialize.

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.