function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dev87Dev87 

Pass List <object> from lightning to apex

Hello, 
I want to pass an object from my lightning js to my class apex.

My class apex:
  @AuraEnabled
    public static void updateEntityInfos(string entityWrapperList, String year) {         
     List<Opportunity> opportunityList = new List<Opportunity>();            
      opportunityList =(List<Opportunity>)System.JSON.deserialize(entityWrapperList, List<Opportunity>.class);
...
}

In my debug log I have in entityWrapperList :
[{"b2b":0,
"globalObjective":"56"
,"name":"test1"
,"newBusiness":"05"
,"opportunityId":"0065E00000CDMurQAH"
,"others":0,
"turnover":672},
{"b2b":0,
"globalObjective":"05"
,"name":"test2"
,"newBusiness":"05"
,"opportunityId":"0065E00000CDMuqQAH"
,"others":0,
"turnover":60},
{"b2b":0,
"globalObjective":"05"
,"name":"test3",
"newBusiness":"50",
"opportunityId":"0065E00000CDMupQAH"
,"others":0,
"turnover":60},
{"b2b":0
,"globalObjective":"65"
,"name":"test4"
,"newBusiness":"05"
,"opportunityId":"0065E00000CDMuoQAH"
,"others":0,
"turnover":780}]

but in my opportunityList  I have just the name:
opportunityList (Opportunity:{Name=EMEA RUSSIE},
Opportunity:{Name=APAC}, 
Opportunity:{Name=Grande Chine},
Opportunity:{Name=Ameriques})
   

Can someone help me.      


  
Narender Singh(Nads)Narender Singh(Nads)
Hi Dev,
This is happening because during deserialization 'Name' field is the only field that is common between your JSON object and Opportunity object, so only the 'name' in JSON object gets mapped to the name field of opportunity object.

Thanks.
Dev87Dev87
Thank your for your reponse, 
If I understand correctly, to display all the values, you have to name the fields of the entity constructor identical to the field of the Opportunity object?
I proceeded as follows but I receive an error:
"Invalid character in identifier: Objectif_Global_Zone__c"
User-added imageUser-added image
Narender Singh(Nads)Narender Singh(Nads)
Hi Dev,

You will need to modify your code something like this:
public YOUR_MAIN_CLASS{

    //You will have to create a wrapper class to store the JSON response
    //You wrapper class will look like this

    public class EntityWrapperClass {

	public Integer b2b;
	public String globalObjective;
	public String name;
	public String newBusiness;
	public String opportunityId;
	public Integer others;
	public Integer turnover;
	}

     //Now your updateEntityInfos function will look like this
    @AuraEnabled
    public static void updateEntityInfos(string entityWrapperList, String year) { 

        
        List<EntityWrapperClass> ObjList = new List<EntityWrapperClass>();
        ObjList =(List<EntityWrapperClass>)System.JSON.deserialize(entityWrapperList, 	List<EntityWrapperClass>.class);
	
	//Now you will have a list of EntityWrapperClass type object
	//Now you can traverse this list and create opportunity records

	//Something like this
	List<Opportunity> opportunityList = new List<Opportunity>(); 
	for(EntityWrapperClass obj:ObjList ){
	   opportunity o=new opportunity();
	   o.name=obj.name;
	   //Assign rest of the field values however you want
	   	
           opportunityList.add(o);
	}

     //rest of your code

}

Let me know if it helps.
Thanks!
 
Dev87Dev87
Thank you for your response,
I implemented this code but I have s fatal error in line 
   ObjList =(List<EntityWrapperClass>)System.JSON.deserialize(entityWrapperList,List<EntityWrapperClass>.class);
 
Narender Singh(Nads)Narender Singh(Nads)
Hey Dev,
I am tried running the same code, and it is giving no error. It is giving results as expected. Maybe someother part of your code is causing this error.
Otherwise there is no reason, it shouldn't work.
If you could send me the whole code I might be able to help you with the error.

 
Dev87Dev87
Thank u very myuch it works  but it d'ont get updated values 
in py debug log I have my entityWrapperList :
entityWrapperList = [{"name":"1","Objectif_Global_Zon":"4","ObjZoneEuro_c":48,"opportunityId":"0065E00000CDMurQAH","Zone_Major_Account":"04","Zone_New_Business":"04","Zone_Other":0},
{"name":"2","Objectif_Global_Zon":"04","ObjZoneEuro_c":48,"opportunityId":"0065E00000CDMuqQAH","Zone_Major_Account":"04","Zone_New_Business":"40","Zone_Other":0},
{"name":"3","Objectif_Global_Zon":"04","ObjZoneEuro_c":48,"opportunityId":"0065E00000CDMupQAH","Zone_Major_Account":"04","Zone_New_Business":"04","Zone_Other":0},
{"name":"4","Objectif_Global_Zon":"04","ObjZoneEuro_c":48,"opportunityId":"0065E00000CDMuoQAH","Zone_Major_Account":0,"Zone_New_Business":"04","Zone_Other":0}]

and my OpportunityList:
o = (Opportunity:{Name=1, Objectif_Global_Zone__c=null, Zone_New_Business__c=null, Zone_Major_Account__c=null, Zone_Other__c=null},
 Opportunity:{Name=2, Objectif_Global_Zone__c=null, Zone_New_Business__c=null, Zone_Major_Account__c=null, Zone_Other__c=null}, 
 Opportunity:{Name=3, Objectif_Global_Zone__c=null, Zone_New_Business__c=null, Zone_Major_Account__c=null, Zone_Other__c=null}, 
 Opportunity:{Name=4, Objectif_Global_Zone__c=null, Zone_New_Business__c=null, Zone_Major_Account__c=null, Zone_Other__c=null})

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,

The reason you are getting NULL values is because may be you are not assigning values to opportunity fields.

In your for loop assign values to opportunity object like this:
o.name=obj.name;
o.Objectif_Global_zone__c=obj.globalObjective;
o.Zone_New_Business__c=obj.newBusiness;
o.Zone_Other__c=obj.others;