TransWikia.com

How can I update opportunity / account / product with a Json that gives a web service as a response?

Salesforce Asked by Jesús Merino on January 20, 2021

enter code here
currently the web service receives as a parameter a Json that is sent with salesforce information of opportunity / account / products. In turn, the web service generates a Json response with information from the same objects with data that must be updated. How could i update those records?
This is a Json example that gives as an answer:

{
"rCuenta": {
    "RecordIdCliente": "243567890",
    "codigoCliente": "CC0000671"
},
"rOppotunity": {
    "RecordIdContrato": "458765349",
    "NumeroContratoRenta": "0000000017",
    "NumeroContratoCAM": "0000000017",
    "NumeroPedido": "ND"
},
"rProductos": [
    {
        "RecordIdLineaCRM": "923460736",
        "TotalImpuesto": 341.74000000,
        "PrecioTotal": 2970.48000000,
        "PorcentajeImpuesto": 13.00000000
    },
    {
        "RecordIdLineaCRM": "834620974",
        "TotalImpuesto": 233.98000000,
        "PrecioTotal": 2033.84000000,
        "PorcentajeImpuesto": 13.00000000
    }
    ]
    }

My end point:

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http(); 
      req.setMethod('GET');//Tipo de método 
      req.setEndpoint('https:...'); // Url web service
      req.setHeader('Content-Type', 'application/json');
      req.setHeader('Accept', 'application/json');

thanks for your help 🙂

One Answer

You can define an Apex class and deserialize the JSON string into a record of the class. Then you can retrieve the values from the class properties and update your relevant SObject records based on the record. e.g.

public class Service {
    public class Result {
        public Cuenta rCuenta {get; set;}
        public Oppotunity rOppotunity {get; set;}
        public Producto[] rProductos {get; set;}
    }
    
    public class Cuenta {
        public String RecordIdCliente;
        public String codigoCliente;
    }
    
    public class Oppotunity {
        public String RecordIdContrato;
        public String NumeroContratoRenta;
        public String NumeroContratoCAM;
        public String NumeroPedido;
    }
    
    public class Producto {
        public String RecordIdLineaCRM;
        public Decimal TotalImpuesto;
        public Decimal PrecioTotal;
        public Decimal PorcentajeImpuesto;
    }
}

Then use it this way:

// This jsonString is for illustration purposes; it can simply be the web service response body
String jsonString = '{' +
    '   "rCuenta":{' +
    '      "RecordIdCliente":"243567890",' +
    '      "codigoCliente":"CC0000671"' +
    '   },' +
    '   "rOppotunity":{' +
    '      "RecordIdContrato":"458765349",' +
    '      "NumeroContratoRenta":"0000000017",' +
    '      "NumeroContratoCAM":"0000000017",' +
    '      "NumeroPedido":"ND"' +
    '   },' +
    '   "rProductos":[' +
    '      {' +
    '         "RecordIdLineaCRM":"923460736",' +
    '         "TotalImpuesto":341.74000000,' +
    '         "PrecioTotal":2970.48000000,' +
    '         "PorcentajeImpuesto":13.00000000' +
    '      },' +
    '      {' +
    '         "RecordIdLineaCRM":"834620974",' +
    '         "TotalImpuesto":233.98000000,' +
    '         "PrecioTotal":2033.84000000,' +
    '         "PorcentajeImpuesto":13.00000000' +
    '      }' +
    '   ]' +
    '}';

Service.Result sResult = (Service.Result) JSON.deserialize(jsonString, Service.Result.class);
System.debug('xxx NumeroContratoRenta: ' + sResult.rOppotunity.NumeroContratoRenta);
// Find the relevant Contact, Opporutnity, Products and use DML to update them.

Correct answer by Chun on January 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP