0

I have a specific format of the Json that I save with opportunity and account values, as in the example below, but in the LINEAS section would depend on the number of products that have the opportunity, how could I do that?

req.setBody(
            ' "Negociación":'+'{'+
                '"PorcentajeAumentoAnual": "'             + SelOportunity.PAA__c                                +'",'   +  
                '"PorcentajeAumentoAnual": "'             + SelOportunity.Porcentaje_Aumento_Anual_Piso__c      +'",'   +            
                '"TipoNegociacion": "'                    + SelOportunity.Tipo_de_Renta__c                      +'",'   +
            ' "Lineas":'+'['+
            '{'+
                /*'"RecordIdLinea": "'                    + SelProduct.Id                      +'",'   +
                '"Categoria": "'                          + SelProduct.Articulo__c             +'",'   +
                '"Cantidad": "'                           + SelProduct.Quantity                +'",'   +
                '"CAM": "'                                + SelProduct.Total_CAM__c            +'",'   +            
                '"PrecioUnitarioLocal": "'                + SelProduct.Prorrateo__c            +'"'    +
                '"PrecioTotalLinea": "'                   + SelProduct.Prorrateo__c            +'",'   +       
                '"IDLocal": "'                            + SelProduct.ProductCode             +'",'   +*/   
            '}' +
            '],
)

With the following query I obtain the products related to the opportunity with the data that it would occupy, but that data would be assigned to the values ​​of the LINEAS segment of the Json

Opportunity op = [SELECT id, (SELECT Id,Articulo__c,Quantity,Total_CAM__c,Prorrateo__c,ProductCode FROM opportunitylineitems)FROM opportunity WHERE Id =: OpportunityId];

How could you make the line part dynamic depending on the number of products of the opportunity?

Saludos!

2 Answers 2

0

You could use JSONGenerator class and as part of the methods in this class, use writeStartArray and writeEndArray to build the array of opp line items related to an opportunity.

Here's a sample code to get you started:

Opportunity op = [SELECT id, Name,(SELECT Id,Name,quantity FROM opportunitylineitems)
FROM opportunity WHERE Id=: OpportunityId]; //replace relevant fields here

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('Negociación');
gen.writeStartObject();
gen.writeStringField('PorcentajeAumentoAnual', op.Name);//replace field
gen.writeStringField('TipoNegociacion', op.Name); //replace field 
//complete other fields from opportunity 

gen.writeFieldName('Lineas');
    gen.writeStartArray();
        for (integer i = 0; i < op.opportunitylineitems.size(); i++){
            gen.writeStartObject();
            gen.writeStringField('Categoria', op.opportunitylineitems[i].Name); //replace field 
            gen.writeStringField('Cantidad', op.opportunitylineitems[i].Name); //replace field
            //complete other fields from opportunitylineitems 
            gen.writeEndObject();
        }
    gen.writeEndArray();
gen.writeEndObject();

system.debug(gen.getAsString() );

Sample JSON result:

{
    "Negociación": {
        "PorcentajeAumentoAnual": "value here",
        "TipoNegociacion": "value here",
        "Lineas": [
            {
                "Categoria": "value here",
                "Cantidad": "value here"
            },
            {
                "Categoria": "value here",
                "Cantidad": "value here"
            },
            {
                "Categoria": "value here",
                "Cantidad": "value here"
            }
        ]
    }
}
1
  • Thank you very much Shamina! It really has helped me a lot. Commented Dec 7, 2020 at 22:48
0

I would suggest you to user a wrapper inner class for both, Negociacion and Lineas. Then for every record in the query I would create one new Linea() and, at the end, I will JSON.serialize() the main wrapper object. It should be "easy", does it make sense for you?

1
  • Hello, the truth is that no, it is the first time that I do this and the only thing that stops me is generating the line segment, depending on the number of products. I occupied JSON.serialize () and it does show me the products of the opportunity, but how could I occupy them so that they are the above format? that is, the first column are fields from a web service. Commented Dec 3, 2020 at 22:48

You must log in to answer this question.

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