0

I am Giving the RequestBody in Postman like

{  
   "contact":[  
      {  
         "name":"ravtrtr",
         "phone":"4333",
         "accountid":"0012800000iJfGi"
      },
      {  
         "name":"rwerw",
         "phone":"sfsfs",
         "accountid":"0012800000iJfGi"
      }
   ]
}

for this body I am writing the Code in Apex class

@RestResource(urlMapping='/createcontact3/')
global class Contact2Apex1 {
public list<contact> contact2 = new list<contact>();
@HttpPost
   global static list<contact>Eventhelper()
    {
     //public list<contact> contact2 = new list<contact>();
      Restrequest req  = Restcontext.request;
      Restresponse res = Restcontext.response;
      Blob body = req.requestbody;
      Contact2Apex1 obj= Contact2Apex1.parse(body.tostring());
      createcontact(obj);
      return null;  
      }

public static  void createcontact(Contact2Apex1 ci)
{
for(contact ca:ci.contact1)
{
contact cy = new Contact();
cy.LastName=ca.lastname;
cy.phone=ca.phone;
cy.accountid = ca.accountid;
insert cy;
}
}
public class contact1{
public String lastname;
public String phone;
public String accountid;
}
public static Contact2Apex1 parse(String json) {
return (Contact2Apex1) System.JSON.deserialize(json, Contact2Apex1.class);
}
}

It is Showing Error

Variable does not exist: contact1

How to Rectify the Error

  • You have class name Contact which is overriding the standard contact object.You need to rename it.I don't think there is any need of inner class Contact.Try again with removing it. – Mr.Frodo Jul 6 '16 at 5:49
  • After Renaming the Class as Contact1,it is Showing Error like Variable does not exist: Contact1 – user31717 Jul 6 '16 at 5:52
  • @Frodo Please Share the Answer – user31717 Jul 6 '16 at 5:53
  • could you try with removing the Contact class as well? – Mr.Frodo Jul 6 '16 at 5:55
  • It is Not Working – user31717 Jul 6 '16 at 5:57
1

Some changes in your JSON and code and things started working for me...

JSON:

{  
   "contacts":[  
      {  
         "LastName":"ravtrtr",
         "phone":"4333"

      },
      {  
         "LastName":"rwerw",
         "phone":"sfsfs"

      }
   ]
}

Apex Class:

@RestResource(urlMapping='/createcontact3/')
global class Contact2Apex1 {
public list<Contact> contacts = new list<Contact>();
@HttpPost
global static list<contact> Eventhelper()
{

  Restrequest req  = Restcontext.request;
  Restresponse res = Restcontext.response;
  Blob body = req.requestbody;
  Contact2Apex1 obj= Contact2Apex1.parse(body.tostring());
  createcontact(obj);
  return null;  
  }

public static  void createcontact(Contact2Apex1 ci)
{
    List<Contact> conToInsert = new List<Contact>();
    for(Contact ca:ci.contacts)
    {
        Contact cy = new Contact();
        cy.LastName=ca.lastname;
        cy.phone=ca.phone;
        conToInsert.add(cy);

    }
    insert conToInsert;
}

public static Contact2Apex1 parse(String json) {
    return (Contact2Apex1) System.JSON.deserialize(json, Contact2Apex1.class);
}
}

I will try to update this with pointing out each and everything i updated.Right now i'm in middle of something :)

I hope you will be able to find out the differences and play with them a little bit.

  • [ { "errorCode": "APEX_ERROR", "message": "System.NullPointerException: Attempt to de-reference a null object\n\nClass.Contact2Apex1.createcontact: line 19, column 1\nClass.Contact2Apex1.Eventhelper: line 12, column 1" } ] – user31717 Jul 6 '16 at 6:52
  • Put JSON and code as it is.It will work – Mr.Frodo Jul 6 '16 at 6:53
  • How to Deserialize the body with the help of Wrapper Class or Dto Class Frodo Guide Me – user31717 Jul 6 '16 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy