+ Start a Discussion
Deepak Singh 116Deepak 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;}

}
}
Best Answer chosen by Deepak Singh 116
Ramesh DepaiahRamesh Depaiah
Add another wrapper class and assign the acc to it and parse 
Public class myAccount{
        acc Account {get; set;} 
    }
Full Class:
public class Jsonwrapclass {
    public String jsonString {get;set;}
    public  Jsonwrapclass(){
        jsonString = prepareData();
    }
    private string prepareData()
    {
        myAccount accnt=new myAccount();
        acc a=new acc();
        account acou=[select name,industry from account limit 1];
        list<contact> conl=[select id,lastname from contact limit 1];
        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);
            lcllist=new list<conlist>();
            lcllist.add(c);
        }
        system.debug(lcllist);
        a.conlist=lcllist;
        accnt.Account=a;
        string s=system.JSON.serialize(accnt);
        return s;
    }
    Public class myAccount{
        acc Account {get; set;} 
    }
    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
 

All Answers

Ramesh DepaiahRamesh Depaiah
Hi Deepak,
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
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<conlist> lcllist=null;
        for(contact co:conl){
            conlist c=new conlist();
            c.id=co.id;
            c.lastname=co.lastname;
            system.debug(c);
            lcllist=new list<conlist>();
            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
Deepak Singh 116Deepak Singh 116
Hi Ramesh, 
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;}
}
}

 
Ramesh DepaiahRamesh Depaiah
Add another wrapper class and assign the acc to it and parse 
Public class myAccount{
        acc Account {get; set;} 
    }
Full Class:
public class Jsonwrapclass {
    public String jsonString {get;set;}
    public  Jsonwrapclass(){
        jsonString = prepareData();
    }
    private string prepareData()
    {
        myAccount accnt=new myAccount();
        acc a=new acc();
        account acou=[select name,industry from account limit 1];
        list<contact> conl=[select id,lastname from contact limit 1];
        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);
            lcllist=new list<conlist>();
            lcllist.add(c);
        }
        system.debug(lcllist);
        a.conlist=lcllist;
        accnt.Account=a;
        string s=system.JSON.serialize(accnt);
        return s;
    }
    Public class myAccount{
        acc Account {get; set;} 
    }
    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
 
This was selected as the best answer