Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How can string apex being truncated?

Asked by David Piper Mar 3, 2023 949 views 1 answer
Share

About this question

I am passing a string that holds the message to be sent to our endpoint from a trigger to a class and it seems if the string is over 270 characters it truncates the field. I have not been able to find any information relating to a size limit - but if I remove a few of the build statements, then the end of the string is correct.

Can anyone tell me why this happens and what I should do to correct it?

The debug values of the field buildMessage is as follows:

value in trigger:

Message before is :{"accountid": "9999999999cu5SXQAY", "title":"Kal", "physician_c": 5640, "market_rep_c":Barry", "email":"null", "phone":"null", "mobilephone":"null", "fax":"null", "mailingstreet":"null", "mailingcity":"null", "mailingpostalcode":"null", "mailingcountry":"
value in class
Message after is :{"accountid": "9999999999cu5SXQAY", "title":"Kal", "physician_c": 5640, "market_rep_c":Barry", "email":"null", "phone":"null", "mobilephone":"null", "fax":"null", "mailingstreet":"null", "mailingcity":"null", "mailingpostalcode":"null", "mailingcountry":"
The trigger code:
trigger ConOut on Contact (after update) {
  String buildMessage;
    for(Contact a:Trigger.new){
        buildMessage = '{';    
        buildMessage = buildMessage + '"accountid": "' + a.accountid + '"'; 
        buildMessage = buildMessage + ', "title":' + '"' + a.title + '"';
        buildMessage = buildMessage + '"physician_c":' + a.physician__c;
        buildMessage = buildMessage + ', "market_rep_c":' + a.market_rep__c + '"';   
        buildMessage = buildMessage + ', "email":' + '"' + a.email + '"';        
        buildMessage = buildMessage + ', "phone":' + '"' + a.phone + '"';  
        buildMessage = buildMessage + ', "mobilephone":' + '"' + a.mobilephone + '"';   
        buildMessage = buildMessage + ', "fax":' + '"' + a.fax + '"';   
        buildMessage = buildMessage + ', "mailingstreet":' + '"' + a.mailingstreet + '"';      
        buildMessage = buildMessage + ', "mailingcity":' + '"' + a.mailingcity + '"'; 
        buildMessage = buildMessage + ', "mailingpostalcode":' + '"' + a.mailingpostalcode + '"';
        buildMessage = buildMessage + ', "mailingcountry":' + '"' + a.mailingcountry + '"';         
        buildMessage = buildMessage + '}';
        system.debug('Message before is :' + buildMessage);
        OurCallout4.putContact(buildMessage);  
    }
}

The class code:

public class OurCallout4 {
    public static String generatedToken {set; get;}
    static String errorMessage {set; get;}
    static String TimelineResponse {set; get;}
    
    @future(callout = true)
    
    public static void putContact(string buildMessage) {    
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('hardcoded_client_id' + ':' + 'hardcoded_client_key')));
        req.setHeader('content-type', 'application/x-www-form-urlencoded');
        req.setEndpoint('https://ourdomain.com/token');
        req.setBody('grant_type=client_credentials');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        JSONParser objParse = JSON.createParser(res.getBody());
        while (objParse.nextToken() != null) 
        {
           if (objParse.getCurrentToken() == JSONToken.FIELD_NAME && objParse.getText() == 'access_token')
           {                               
              objParse.nextToken();
              generatedToken = objParse.getText();
           }
        }
            if(generatedToken == null ) 
            {
                TimelineResponse = 'Error while generating token, so unable to get messages. Check your debug log.';
                return;
            }
            HttpRequest objReq = new HttpRequest();
            objReq.setEndpoint('https://ourdomain.com/ourendpoint');
            objReq.setMethod('POST');
            objReq.setHeader('Authorization', 'Bearer ' + generatedToken);
            
            objReq.setBody(buildMessage);
        system.debug('Message after is :' + buildMessage);
            Http objHttp = new Http();
            HTTPResponse objRes = objHttp.send(objReq);
            TimelineResponse = objRes.getBody();
            if(String.isBlank(TimelineResponse))
            {
               TimelineResponse = objRes.toString();
            }
    }
}

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.