+ Start a Discussion
Pedro SallesPedro Salles 

How to use JSON class as a inner class instead of separate class

Main Class:
@RestResource(urlMapping='/updateFaturamento/*')
global  class contractFat { 

public class ContractJSON {

    public List<Contract> Contract;

	public class Contract {
		public String idSalesForce;
		public String dtEmissao;
		public String nrContrato;
		public String vlVenda;
		public String vlDevolucao;
		public String qtTotalClientes;
		public String qtClientesAtivosMes;
		public String flagMensal;
	}

	
	public  ContractJSON parse(String json) {
		return (ContractJSON) System.JSON.deserialize(json, ContractJSON.class);
	}
}
    @HttpPatch
    global static String updateFaturamento(String contractId,Integer nrContrato, Boolean flagMensal, Double vlVenda, Double vlDevolucao,
                                          integer qtTotalClientes, integer qtClientesAtivosMes, Date dtEmissao )
    {
    RestRequest req = RestContext.request; 
    RestResponse res = RestContext.response;
        
        if (flagMensal == false){
                
                Opportunity oppInsert = [SELECT Id,Data_Emissao__c, Numero_Contrato_Univers__c, Valor_Vendas__c, Valor_Devolucao__c, 
                                         Quantidade_Total_Clientes__c, Quantidade_Cliente_Ativos_Mes__c 
                                         from Opportunity WHERE ContractId  =: contractId and Numero_Contrato_Univers__c =: nrContrato];
                
            //if (oppInsert.Numero_Contrato_Univers__c == null ){}
            
                oppInsert.Data_Emissao__c = dtEmissao;
                oppInsert.Numero_Contrato_Univers__c = nrContrato;
                oppInsert.Valor_Vendas__c = vlVenda;
                oppInsert.Valor_Devolucao__c = vlDevolucao;
                oppInsert.Quantidade_Total_Clientes__c = qtTotalClientes;
                oppInsert.Quantidade_Cliente_Ativos_Mes__c = qtClientesAtivosMes;
                
                upsert oppInsert ;
                
                return 'Faturamento';           
            
        } else if (flagMensal == true) {
                Contract contractInsert = [SELECT Id, Data_Emissao__c, Valor_Vendas__c, Valor_Devolucao__c, Quantidade_Total_Clientes__c,
                                         Quantidade_Clientes_Ativos_Mes__c  from Contract WHERE Id =: contractId];
                
                contractInsert.Data_Emissao__c = dtEmissao;
                contractInsert.Valor_Vendas__c = vlVenda;
                contractInsert.Valor_Devolucao__c = vlDevolucao;
                contractInsert.Quantidade_Total_Clientes__c = qtTotalClientes;
                contractInsert.Quantidade_Clientes_Ativos_Mes__c = qtClientesAtivosMes;
                
                upsert ContractInsert;
                
                /*List<Opportunity> listOpportunity = [select  ContractId, Id, Numero_Contrato_Univers__c, Data_Emissao__c, Valor_Vendas__c, 
                                                    Valor_Devolucao__c, Quantidade_Total_Clientes__c, Quantidade_Cliente_Ativos_Mes__c 
                                                    from Opportunity where ContractId =: contractId and  Numero_Contrato_Univers__c =: nrContrato ];
                
                                for(Opportunity opp : listOpportunity){
                                    Opportunity oppDeleteDiario = new Opportunity ();
                                        oppDeleteDiario.Data_Emissao__c = dtEmissao;
                                        oppDeleteDiario.Valor_Vendas__c = vlVenda;
                                        oppDeleteDiario.Valor_Devolucao__c = vlDevolucao;
                                        oppDeleteDiario.Quantidade_Total_Clientes__c = qtTotalClientes;
                                        oppDeleteDiario.Quantidade_Cliente_Ativos_Mes__c = qtClientesAtivosMes;
                                
                                delete oppDeleteDiario; 
                                return 'Deleted';
                }       */
                            
                Opportunity oppDelete = [SELECT Id,Data_Emissao__c, Numero_Contrato_Univers__c, Valor_Vendas__c, Valor_Devolucao__c, 
                                         Quantidade_Total_Clientes__c, Quantidade_Cliente_Ativos_Mes__c 
                                         from Opportunity WHERE ContractId  =: contractId and Numero_Contrato_Univers__c =: nrContrato];
                
                //oppDelete.Data_Emissao__c = dtEmissao;
                //oppDelete.Numero_Contrato_Univers__c = nrContrato;
                oppDelete.Valor_Vendas__c = null;
                oppDelete.Valor_Devolucao__c = null;
                oppDelete.Quantidade_Total_Clientes__c = null;
                oppDelete.Quantidade_Cliente_Ativos_Mes__c = null;
                
                delete oppDelete;                        
                
                return 'Faturamento Mensal  Updated';
        }    
    return null;
    }
}
Error: Inner types are not allowed to have inner types

This error is displayed in the Contract class.
I tried to separate the classes, but still the error persists.
Can anyone help me with this problem?

Thanks

 
Best Answer chosen by Pedro Salles
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pedro,

I trust you are doing very well.

Please refer to the below link with a similar discussion which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/230777/getting-null-for-inner-classes-in-complex-json-deserialization

https://salesforce.stackexchange.com/questions/223121/how-to-use-json-class-as-a-inner-class-instead-of-seprate-class


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Pedro,

I trust you are doing very well.

Please refer to the below link with a similar discussion which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/230777/getting-null-for-inner-classes-in-complex-json-deserialization

https://salesforce.stackexchange.com/questions/223121/how-to-use-json-class-as-a-inner-class-instead-of-seprate-class


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Pedro SallesPedro Salles
Hello Khan Anas .
Thanks for the help, it helped me a lot! Now another question has arisen: How to put this list in the parameter and use its variables?
For example:
 global static String updateFaturamento(List<Contract> Contratos )
    {
    RestRequest req = RestContext.request; 
    RestResponse res = RestContext.response;
        
        if (flagMensal == false){
                
                Opportunity oppInsert = [SELECT Id,Data_Emissao__c, Numero_Contrato_Univers__c, Valor_Vendas__c,                    Valor_Devolucao__c, Quantidade_Total_Clientes__c, Quantidade_Cliente_Ativos_Mes__c 
                                         from Opportunity WHERE ContractId  =: contractId and Numero_Contrato_Univers__c =: nrContrato];
                
            oppInsert.Numero_Contrato_Univers_c = Contratos.idSalesforce; // here I want to get the first variable from the "idSalesforce" list

I'm having a hard time doing this task
Thanks