You need to sign in to do that
Don't have an account?
Deepak Singh 116
Method does not exist or incorrect signature: void add(Jsonwrapclass.conlist) from the type List<Contact>
I am getting error:-
Error: Jsonwrapclass Compile Error: Method does not exist or incorrect signature: void add(Jsonwrapclass.conlist) from the type List<Contact> at line 20 column 9
Controller
public with sharing class Jsonwrapclass {
public String jsonString {get;set;}
public Jsonwrapclass(){
jsonString = prepareData();
}
private string prepareData()
{
acc a=new acc();
account acou=[select name,industry from account where id='0010K00001px8HpQAI'];
list<contact> conl=[select id,lastname from contact where accountid='0010K00001px8HpQAI'];
a.name=acou.name;
a.industry=acou.industry;
List<contact> lcllist=null;
for(contact co:conl){
conlist c=new conlist();
c.id=co.id;
c.lastname=co.lastname;
system.debug(c);
lcllist=new list<contact>();
lcllist.add(c);
}
system.debug(lcllist);
/* if(lcllist==null){
lcllist=new list<contact>();
lcllist.add(c);
}*/
//a.conlist=lcllist;
string s=system.JSON.serialize(a);
return s;
}
Public class acc{
string name {get; set;}
string industry {get; set;}
list<contact> conlist{get;set;}
}
public class conlist{
string id {get; set;}
string lastname{get; set;}
}
}
Error: Jsonwrapclass Compile Error: Method does not exist or incorrect signature: void add(Jsonwrapclass.conlist) from the type List<Contact> at line 20 column 9
Controller
public with sharing class Jsonwrapclass {
public String jsonString {get;set;}
public Jsonwrapclass(){
jsonString = prepareData();
}
private string prepareData()
{
acc a=new acc();
account acou=[select name,industry from account where id='0010K00001px8HpQAI'];
list<contact> conl=[select id,lastname from contact where accountid='0010K00001px8HpQAI'];
a.name=acou.name;
a.industry=acou.industry;
List<contact> lcllist=null;
for(contact co:conl){
conlist c=new conlist();
c.id=co.id;
c.lastname=co.lastname;
system.debug(c);
lcllist=new list<contact>();
lcllist.add(c);
}
system.debug(lcllist);
/* if(lcllist==null){
lcllist=new list<contact>();
lcllist.add(c);
}*/
//a.conlist=lcllist;
string s=system.JSON.serialize(a);
return s;
}
Public class acc{
string name {get; set;}
string industry {get; set;}
list<contact> conlist{get;set;}
}
public class conlist{
string id {get; set;}
string lastname{get; set;}
}
}
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
All Answers
You created a list of type contact and trying to add different type(conlist) to the list which doesnt accept. You must add same type
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
Thanks It works.
I have one more query.
JSON STRING
{"name":"acc","industry":"Technology","conlist":[{"lastname":"Singh","id":"0030K00001ozaZdQAI"},{"lastname":"acc","id":"0030K00001ZzzVkQAJ"}]}
Why the object name is not there in parent string.
I want Make this string like below:-
{ "Account" : { "name " : "acc", "Account Number" : "1234554321", "Industry" : "Technology" }, "conlist" : [ { "id" : "0030K00001ozaZdQAI", "LastName" : "Singh" }, { "id" : "0030K00001ZzzVkQAJ", "LastName" : "acc" } ] }
Controller:-
public with sharing class Jsonwrapclass {
public String jsonString {get;set;}
public Jsonwrapclass(){
jsonString = prepareData();
}
private string prepareData()
{
account acou=[select name,industry from account where id='0010K00001px8HpQAI'];
list<contact> conl=[select id,lastname from contact where accountid='0010K00001px8HpQAI'];
acc a=new acc();
a.name=acou.name;
a.industry=acou.industry;
List<conlist> lcllist=null;
for(contact co:conl){
conlist c=new conlist();
c.id=co.id;
c.lastname=co.lastname;
system.debug(c);
if(lcllist==null){
lcllist=new list<conlist>();
}
lcllist.add(c);
}
system.debug(lcllist);
a.conlist=lcllist;
string s=system.JSON.serialize(a);
return s;
}
Public class acc{
string name {get; set;}
string industry {get; set;}
list<conlist> conlist{get;set;}
}
public class conlist{
string id {get; set;}
string lastname{get; set;}
}
}
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D