0

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 :)

1 Answer 1

0

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.
1
  • thank you very much, you are great Chun !! :) Commented Dec 14, 2020 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .