-3

How to insert, update... records by using the HTTP classes?

I need to insert and delete records over an apex controller by using HTTP classes. I need to do it over HTTP because the DML has to be done by the administrator and during the call the user is a regular user. By using HTTP I can specify the user and the password of the administrator, this will allow me to perform a DML as administrator.

closed as unclear what you're asking by Gortonington, Eric, Markus Slabina, Himanshu, glls Aug 16 '17 at 17:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Deserialize it into an object – EricSSH Aug 16 '17 at 16:46
1

There's a number of steps you need need to take into consideration to insert/update records using HTTP Classes.

as mentioned before, you can deserialize json object and use it or you can Parse it if JSON response include fields that you don't want to include in the record.

Since you're asking for http, the I'd expect that records are coming from 3rd party.

  1. Make sure you whitelist the ip
  2. Create the and sent http request
  3. Get the response and create a JSONParser object from it
  4. Iterate over the JSON object and retrieve your data
  5. Create your record
  6. Insert/Update your records

Simple example :

.....
public void makeCallout {

Http http = new Http();
HttpRequest request = new HttpRequest();
Transient string cookie = 'X';
request.setEndpoint(url);
request.setMethod('POST');       
request.setHeader('Authorization', authheader);
request.setHeader('Cookie', cookie);
if (response.getStatusCode() == 200) {
    while (parser.nextToken() != null){
        if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
            if (parser.getCurrentToken() == JSONToken.START_OBJECT){
                try {
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() =='name')){
                        name = parser.getText();
                    }
                } 
            } 
        } 
    }
    insert a;
}
}
  • 1
    The overhead on JSON class is pretty high, just deserlize it in an object – EricSSH Aug 16 '17 at 17:15

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