1

We have the following:

  • Source SFDC Org: making a REST Callout towards SFDC Target Org
  • Target SFDC Org: Custom PUT Webservice being referenced from the Source SFDC Org

Source SFDC Org Future method making a callout towards the Target org Webservice. But we need to generate a specific payload. Thus we need to create a JSONGenerator instance using JSONGenerator gen = new JSON.createGenerator(true); However, upon saving it's giving us the error Invalid type: JSON.createGenerator

Structure of the class looks like this:

public class className {
  @Future(callout=true)
  public static void doTheCallout() {
   JSONGenerator gen = new JSON.createGenerator(true);

    gen.writeStartObject();
    // creation of the payload
    gen.writeEndObject();
  }
}

Any idea?

  • 1
    Check if you have any class name with JSON – Tushar Sharma Nov 5 '16 at 11:14
3

The method is static - its a factory for creating instances - so you need this code that does not include a new:

JSONGenerator gen = JSON.createGenerator(true);

In your code the compiler thinks you are trying to create an instance of a class called createGenerator nested inside a class called JSON.

Note that it is usually easier to create JSON by creating a graph of simple Apex objects or by using maps and then using the JSON.serialize method.

|improve this answer|||||
1

Don't use the new keyword. It should only be used when calling constructors, not for calling methods.

Works

JSONGenerator gen = JSON.createGenerator(true);

Fails

JSONGenerator gen = new JSON.createGenerator(true);
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.