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
VijayasaharanVijayasaharan 

lightning web component Pass Javascript array of object to APEX and typecase it to APEX Class

HI I am building a Lightning Web Component (LWC)
I did all the edits and I have an array with ID and Perecentage(custom field). The array is marked with @track in the js file.
I am lost on how I can convert this array in APEX to a LIST so that I can update the data in APEX.

Thanks
Best Answer chosen by Vijayasaharan
VijayasaharanVijayasaharan
I was able to solve this, first JSON.stringify the data and send it to Apex Class.

In the Apex Class use JSON parser and iterator and create a list of objects of your custom object type
 
public static List<Custom_object__c> buildResourceObject(string updateStr){
	List<Custom_object__c> objList = new List<Custom_object__c>();
	system.debug(updateStr);
	JSONParser parser = JSON.createParser(updateStr);
	while (parser.nextToken() != null) {
		if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
			while (parser.nextToken() != null) {
				system.debug('current name' + parser.getCurrentName() + ' current token' + parser.getCurrentToken());
				if (parser.getCurrentToken() == JSONToken.START_OBJECT) {                        
					Custom_object__c objVal = new Custom_object__c();
					Custom_object__c temp = (Custom_object__c)parser.readValueAs(Custom_object__c.class);
					if(temp.Id != null){
						objVal.Id	= temp.Id;
						objVal.Field_One__c = temp.Field_One__c;
						system.debug('current text' + objVal.Field_One__c);
						objList.add(objVal);
					}
					/*String s = JSON.serialize(tdef);system.debug('Serialized invoice: ' + s);*/
					parser.skipChildren();
				}
			}
		}
	}
	return objList;
}

 

All Answers

VijayasaharanVijayasaharan
I was able to solve this, first JSON.stringify the data and send it to Apex Class.

In the Apex Class use JSON parser and iterator and create a list of objects of your custom object type
 
public static List<Custom_object__c> buildResourceObject(string updateStr){
	List<Custom_object__c> objList = new List<Custom_object__c>();
	system.debug(updateStr);
	JSONParser parser = JSON.createParser(updateStr);
	while (parser.nextToken() != null) {
		if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
			while (parser.nextToken() != null) {
				system.debug('current name' + parser.getCurrentName() + ' current token' + parser.getCurrentToken());
				if (parser.getCurrentToken() == JSONToken.START_OBJECT) {                        
					Custom_object__c objVal = new Custom_object__c();
					Custom_object__c temp = (Custom_object__c)parser.readValueAs(Custom_object__c.class);
					if(temp.Id != null){
						objVal.Id	= temp.Id;
						objVal.Field_One__c = temp.Field_One__c;
						system.debug('current text' + objVal.Field_One__c);
						objList.add(objVal);
					}
					/*String s = JSON.serialize(tdef);system.debug('Serialized invoice: ' + s);*/
					parser.skipChildren();
				}
			}
		}
	}
	return objList;
}

 
This was selected as the best answer
Amit Jodha 9Amit Jodha 9
As a beginner who was stuck at the exact same spot, I'd like to thank you.
JAVED AHMED 1JAVED AHMED 1
hii all,
i want result like this with lwc(https://rajvakati.com/2018/02/18/usage-of-lightningduallistbox/),
can anyone tell what i m doing wrong in this code below is the code

js file
import { LightningElement,track,wire,api} from 'lwc';
import getopp from '@salesforce/apex/WrapperclassForOpportunity.getopp';
export default class MultiPickList1 extends LightningElement {
@api contactlist=[];
@wire(getopp) opplist({error,data}){
         if (data) {
                this.contactlist = data[0].conlist;  
                alert('this.contactlist@@ '+ JSON.stringify (data[0].conlist) );
                alert('contactlist:'+this.contactlist);                               
        
        }
    }
}


html file 
<template>
    <lightning-card title="Multi select Picklist ">
        <lightning-dual-listbox 
      name="languages"
      label="Select Record"
      source-label="Available Contact Record"
      selected-label="Selected Contacted Record"
      options={contactlist}
      onchange={handleChange}>
  </lightning-dual-listbox>
    </lightning-card>
</template>


wrapper class
public class WrapperclassForOpportunity {
    @AuraEnabled(cacheable=true)
    public static List<payWrap> getopp(){
        List<payWrap> accWrapperList = new List<payWrap>();
        opportunity getopp=[select id, accountid,closedate,stagename from opportunity where id=:'0062v00001TDCVoAAP'];
         system.debug(getopp);
        account acc= [select id, name from account where id=:getopp.accountId];
         system.debug(acc);
        list<contact> conlist1=new list<contact>();
        conlist1=[select name from contact where accountid=:acc.id]; 
        if(!conlist1.isEmpty()){
            payWrap myWrap= new payWrap();
            mywrap.conlist=conlist1;
            accWrapperList.add(myWrap);
        }
        system.debug(accWrapperList);
        return accWrapperList;    
    }  
    public class payWrap{
        @AuraEnabled
        public List<Contact> conlist{get;set;}    
    }
}
 
VijayasaharanVijayasaharan
Can you tell us the issue that you are seeing?
may be console.log the result. Also add a else if (error) in your wire and see if there are any issues there..

Side note: In your apex, why cant you combine the opportunity, Account and contact query all in one
VijayasaharanVijayasaharan
I dont think you need the payWrap class too in you combine the queries, you can do all the validations in your js code, there by reducing the number of lines in your apex.