0

I have a trigger that sends a certain opportunity to an unspecified stage, so I have a json with the objects, but I intend to send everything in a single string, so I joined json, when I run the code it gives me the error "$message": "JSON improperly formed". Anyone know how I can join Json.

trigger TESTE on Opportunity (after insert, after update) {

for(Opportunity opp : trigger.new) {
    if(opp.StageName == 'Approved'){

        Contact contact = [SELECT  FirstName, LastName, Title, Id ,
                           Xplor__Gri_Status__c,Xplor__Gri_Full_Consent_Given_to_GDPR__c, Xplor__Gri_Soft_Consent_Given_to_GDPR__c, Xplor__Gri_Preferred_Language__c,
                           Xplor__Gri_Gender__c, Xplor__Gri_Secretary_Email_2__c, Xplor__Gri_Secretary_Name_1__c,Xplor__Gri_Secretary_Name_2__c,
                           Xplor__Gri_Secretary_Phone_1__c,Xplor__Gri_Secretary_Phone_2__c, Xplor__Gri_Secretary_Email_1__c, CurrencyIsoCode, Xplor__Teste__c,
                           MailingCity, MailingState, MailingCountry, MailingPostalCode, MailingStreet
                           FROM Contact 
                           WHERE id =: opp.Xplor__Contact__c]; 

        System.debug('contact'+contact);

        Account account = [SELECT Name, id, Xplor__Gri_Brazilian_Withholding_taxes__c, Xplor__Gri_Company_s_Legal_Name__c,Xplor__Gri_Tax_Regime__c,
                           Xplor__Gri_Tax_Rule__c, Xplor__Gri_Id_X3__c, Xplor__Gri_Payment_Notes__c, Xplor__GRI_other_email__c, Xplor__Gri_Mobile_Phone__c,
                           Xplor__Gri_Local_Currency__c, Xplor__Gri_Inscricao_Municipal__c, Xplor__Gri_Inscricao_Estadual__c,Xplor__Gri_Direct_Line__c,
                           Xplor__GRI_CPF__c, Xplor__Gri_CNPJ__c, Xplor__GRI_Business_Phone__c, BillingStreet, BillingCity, BillingState, BillingPostalCode, Xplor__Gri_Email__c
                           FROM Account
                           WHERE Id =: opp.AccountId];

        System.debug('account'+account);

        //Opportunity
        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartObject(); 
        gen.writeObjectField('XTOIMP', Opp.Xplor__Gri_State__c); //state necessary for X3
        if(Opp.Xplor__Gri_Club__c != null){

            gen.writeObjectField('YCLUB', Opp.Xplor__Gri_Club__c);
        }
        if(Opp.Xplor__Teste__c != null){
            gen.writeObjectField('YIDCONT', Opp.Xplor__Teste__c); ///*********************************Atençao -   AccountId ********************************
        }
        if(Opp.Id!= null){
            gen.writeObjectField('YIDOP', Opp.Id);
        }
        if(Opp.Xplor__Gri_Financial_Notes__c != null){
            gen.writeObjectField('YPAYNOTE', Opp.Xplor__Gri_Financial_Notes__c);
        }
        //if(Opp.Xplor__Gri_EventID__c!= null){
        // gen.writeObjectField('YEVENTID', Opp.Xplor__Gri_EventID__c);
        // }
        if(Opp.CloseDate != null){
            gen.writeObjectField('ORDDAT', Opp.CloseDate); 
        }
        if(Opp.CurrencyIsoCode != null){
            gen.writeObjectField('SOHCUR', Opp.CurrencyIsoCode);
        }
        if(Opp.Xplor__Gri_Government_Entity__c != null){
            gen.writeObjectField('YENTPUBLI', Opp.Xplor__Gri_Government_Entity__c); 
        }
         /*if(Opp.Xplor__Gri_Owner_Name__c != null){
            gen.writeObjectField('REP', Opp.Xplor__Gri_Owner_Name__c); 
        }*/
        gen.writeEndObject();
        String jsonSOpportunity = gen.getAsString();
        System.debug('jsonSOpportunity'+jsonSOpportunity);


            //Contacts
        gen = JSON.createGenerator(true);
        gen.writeStartObject();
        if(contact.Id != null){
            gen.writeObjectField('CCNCRM', contact.Id);  
        }
       /* if(contact.Xplor__Teste__c != null){
            gen.writeObjectField('YIDCONT', contact.Xplor__Teste__c);  ///*********************************Atençao********************************
        }*/
        if(contact.LastName != null){
            gen.writeObjectField('CNTLNA', contact.LastName);
        }
        if(contact.FirstName != null){
            gen.writeObjectField('CNTFNA', contact.FirstName);
        }
        if(contact.CurrencyIsoCode != null){
            gen.writeObjectField('CREUSR', contact.CurrencyIsoCode); ///*********************************Atençao********************************
        }
        /*if(contact.MailingCity != null){
            gen.writeObjectField('YSECEMAIL1', contact.MailingCity);
        } */
        if(contact.MailingState != null){
            gen.writeObjectField('SAT', contact.MailingState);
        }
        if(contact.MailingCountry != null){
            gen.writeObjectField('ACRY', contact.MailingCountry);
        }
         if(contact.MailingPostalCode != null){
            gen.writeObjectField('POSCOD', contact.MailingPostalCode);
        }
        if(contact.MailingStreet != null){
            gen.writeObjectField('BPAADDLIG0', contact.MailingStreet);
        }
        if(contact.Xplor__Gri_Secretary_Email_1__c != null){
            gen.writeObjectField('YSECEMAIL1', contact.Xplor__Gri_Secretary_Email_1__c);
        }
        if(contact.Xplor__Gri_Secretary_Email_2__c != null){
            gen.writeObjectField('YSECEMAIL2', contact.Xplor__Gri_Secretary_Email_2__c);
        }
        if(contact.Xplor__Gri_Secretary_Name_1__c != null){
            gen.writeObjectField('YSECNAME1', contact.Xplor__Gri_Secretary_Name_1__c);
        }
        if(contact.Xplor__Gri_Secretary_Name_2__c != null){
            gen.writeObjectField('YSECNAME2', contact.Xplor__Gri_Secretary_Name_2__c);
        }
        if(contact.Xplor__Gri_Secretary_Phone_1__c != null){
            gen.writeObjectField('YSECPHONE1', contact.Xplor__Gri_Secretary_Phone_1__c);
        }
        if(contact.Xplor__Gri_Secretary_Phone_2__c != null){
            gen.writeObjectField('YSECPHONE2', contact.Xplor__Gri_Secretary_Phone_2__c);
        }
        if(contact.Xplor__Gri_Full_Consent_Given_to_GDPR__c != null){
            gen.writeObjectField('YFULLCONS', contact.Xplor__Gri_Full_Consent_Given_to_GDPR__c);
        }
        if(contact.Xplor__Gri_Soft_Consent_Given_to_GDPR__c != null){
            gen.writeObjectField('YSOFTCONS', contact.Xplor__Gri_Soft_Consent_Given_to_GDPR__c);
        }
        if(contact.Xplor__Gri_Preferred_Language__c != null){
            gen.writeObjectField('CNTLAN', contact.Xplor__Gri_Preferred_Language__c);
        }
        if(contact.Xplor__Gri_Gender__c != null){
            gen.writeObjectField('YGENDER', contact.Xplor__Gri_Gender__c);
        }
        //if(contact.Xplor__Gri_Status__c != null){
        // gen.writeObjectField('YSTA', contact.Xplor__Gri_Status__c);   /******************************************Só Falta este**************************
        // }
        gen.writeEndObject();
        String jsonSContact = gen.getAsString();
        System.debug(jsonSContact);


        ClassTriggerOpportunityController.testeOpportunity(jsonSOpportunity, jsonSContact);


    }

}
}

Class

public class ClassTriggerOpportunityController {


@Future(callout=true)
public static void testeOpportunity(String jsonSOpportunity, String jsonSContact){//, String jsonSAccount, String jsonSContact){

    System.debug('jsonSOpportunity'+jsonSOpportunity);
    System.debug('jsonSContact'+jsonSContact);
    String Json = (jsonSOpportunity + jsonSContact);
    Json = Json.replace('}{\n', '');

    System.debug('Json'+Json);

    HttpRequest req = new HttpRequest();
    //set endpoint
    String endpoint = '**************************************.$create';
    req.setEndpoint(endpoint);
    //set method
    req.setMethod('POST');
    //set content-type
    req.setHeader('Content-Type','application/json');
    //set authorization
    req.setHeader('Authorization', 'Basic ZHNjOmRhbmllbGFjZXJxdWVpcmE=');
    //set body
   // req.setbody(jsonSOpportunity);
    req.setbody(Json);
    //req.setbody(csv);
    Http http = new Http();
    //send request
    HTTPResponse response = http.send(req);
    System.debug(response);
    System.debug(response.getBody());

}
}

This is the part I use for joining json

  String Json = (jsonSOpportunity + jsonSContact);
        Json = Json.replace('}{\n', '');

enter image description here

enter image description here

1
  • I think it would be easier for us to determine whats wrong if you post your example json – user1974566 Jan 7 '19 at 10:33
2

You will typically end up with less code and less buggy code if you create your own Apex class that has the field names and types you want, set the values there and then push that through JSON.serialize:

public MyClass {

    public String CCNCRM;
    public String CNTLNA;
    ...

    public MyClass(Contact c) {
        CCNCRM = c.Id;
        CNTLNA = c.LastName;
        ...
    }
}


MyClass mc = new MyClass(contact);
String s = JSON.serialize(mc);

This separates creating the data structure from the details of building JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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