+ Start a Discussion
Abraham kumar 4Abraham kumar 4 

To change an array to normal form in Json data

I have the below JSON POST callout class. The data is sent in an array format whereas the destination requires it in just normal format. please suggest to remove the array in this code an execute in normal format. Please help. I just want it to send data in normal format and not in an array.
 
public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(string Id,string Email,string First_Name,string Last_Name,string phone,string Title,string Account) {
conweb cont=new conweb(Id,Email,First_Name,Last_Name,Phone,Title,Account);
List<conweb> conwebs=new List<conweb>();
conwebs.add(cont);
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
req.setTimeout(2000); // timeout in milliseconds
        req.setEndpoint('http://178.62.64.210/api/user');
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization','gfkj^%$654GF65yhtd54');
       req.setBody(JSON.serialize(conwebs));
        //req.setBody('Id='+EncodingUtil.urlEncode(Id, 'UTF-8')+'&Email='+EncodingUtil.urlEncode(Email, 'UTF-8')+'&First_Name='+EncodingUtil.urlEncode(First_Name, 'UTF-8')+'&Last_Name='+EncodingUtil.urlEncode(Last_Name, 'UTF-8'));
        //req.setCompressed(true); // otherwise we hit a limit of 32000

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }

    }

 
    
    
    public class conweb
    {
    String salesForceId;
    String emailAddress;
    String firstName;
    string lastName;
    string contactNumber;
    string jobTitle;
    string companyName;
   
    public conweb(string sid,string semail,string sfirstname,string slastname,string sphone,string Title,string Account)
    
    {
    salesForceId=sid;
    emailAddress=semail;
    firstName=sfirstname;
    lastName=slastname;
    contactNumber=sPhone;
    jobTitle=Title;
    companyName=Account;
  
    }
    }}

 
Best Answer chosen by Abraham kumar 4
Abhi_TripathiAbhi_Tripathi
Re-check are you querying Account.Name in the Contact query ?

All Answers

Abhi_TripathiAbhi_Tripathi
Make your conwebs list to a variable of the wrapper class,
As of now you are creating a list of the wrapper which an array, make an instance of the wrapper class and then populate it ans send it.

Like
public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(string Id,string Email,string First_Name,string Last_Name,string phone,string Title,string Account) {
conweb cont=new conweb(Id,Email,First_Name,Last_Name,Phone,Title,Account);
List<conweb> conwebs=new List<conweb>();
conwebs.add(cont);
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
req.setTimeout(2000); // timeout in milliseconds
        req.setEndpoint('http://178.62.64.210/api/user');
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization','gfkj^%$654GF65yhtd54');
       req.setBody(JSON.serialize(conwebs[0]));
        //req.setBody('Id='+EncodingUtil.urlEncode(Id, 'UTF-8')+'&Email='+EncodingUtil.urlEncode(Email, 'UTF-8')+'&First_Name='+EncodingUtil.urlEncode(First_Name, 'UTF-8')+'&Last_Name='+EncodingUtil.urlEncode(Last_Name, 'UTF-8'));
        //req.setCompressed(true); // otherwise we hit a limit of 32000

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }

    }

 
    
    
    public class conweb
    {
    String salesForceId;
    String emailAddress;
    String firstName;
    string lastName;
    string contactNumber;
    string jobTitle;
    string companyName;
   
    public conweb(string sid,string semail,string sfirstname,string slastname,string sphone,string Title,string Account)
    
    {
    salesForceId=sid;
    emailAddress=semail;
    firstName=sfirstname;
    lastName=slastname;
    contactNumber=sPhone;
    jobTitle=Title;
    companyName=Account;
  
    }
    }}


Let me know if it works

 
Abraham kumar 4Abraham kumar 4
Amazing Abhi., Thank u soo much.. i have one more problem pls can you suggest me your thoughts as the company name field ie)Account name in the contact form is a lookup it is sending the Account id to the other system.. i want to send the name of the account to my destination and not the id ...is there any ways i can achieve this...To send account name to the destination system problem is because this is  a lookup field...

Many Thanks in advance.. Made my Day.. :)
Abhi_TripathiAbhi_Tripathi
To send Account Name create a variable and assign Account.Name to it, if you are quering AccountId from the Contact modify it to 

Contact con = [Select Id, Account.Name From Contact];
Abraham kumar 4Abraham kumar 4
Sorry Abhi ..Im not able to get you im new to this can you be little more specific... In the above code what changes shall i make should i have to put account.name .. pls help..
Abhi_TripathiAbhi_Tripathi
In your code rather then passing Account pass Account.Name when you are calling the method, 
 
//Call method and Pass Account.Name
WebserviceCallout.sendNotification( Account.Id, 'test@email.com', 'First_Name',' Last_Name', '4334343434', 'Title', Account.Name) ;

 
Abraham kumar 4Abraham kumar 4
just account.name is only giving me null.. I want the Account linked to the contact..So i tried like c.account.name but still no luck
 WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,c.Account.name);
Abhi_TripathiAbhi_Tripathi
Re-check are you querying Account.Name in the Contact query ?
This was selected as the best answer
Abraham kumar 4Abraham kumar 4
thanks Abhi.. :)