2

I am just learning about EncodingUtil.urlEncode(targetString,'UTF-8'); and encoding schemes. targetString will print out something that looks like this:

String targetString =
                'ckm_campaign_id=999' +
                '&ckm_key=euu87nn' +
                '&ckm_subid=Company Name&' +
                'ckm_subid_2=Partner&' +
                'ckm_subid_3=API&' +
                'first_name=' + l.FirstName +
                '&last_name=' + l.LastName +
                '&email_address=' + l.Email +
                '&phone=' + l.Phone +
                '&personal_credit_score=' + l.Est_Credit_Score__c;

system.debugs --> ckm_campaign_id%3D999%26ckm_key%3Deuu87nn%26ckm_subid%3DCompany+Name%26ckm_subid_2%3DPartner%26ckm_subid_3%377gggs%26first_name%3Dtest%26last_name%3DNF1%26email_address%3Dtest1%40gmail.com%26phone%3D7776665432%26personal_test%3D680+-+719+%28Great%29

what I want is for it to look like this with just the plus signs:

ckm_campaign_id=999&ckm_key=euu87nn&ckm_subid=Company+Name&ckm_subid_2=Partner&first_name=test&last_name=test&email_address=test%40gmail.com&phone=1234567890&personal_test=680+-+719+(Great)

I am pulling the data from the Lead object so the only way I know of how to do this is I would have to print out the values, hard code in the plus signs, and then insert them back in. I feel like this is inefficient and could create a lot of mistakes.

Is there an APEX method or way of encoding with just the + signs?

1 Answer 1

6

Consider using PageReference instead:

Lead l = new Lead(Est_Credit_Score__c='999', Phone='(999) 555-1212', FirstName='Brian', 
                  LastName='Fear', email='[email protected]');
PageReference ref = new PageReference('');
Map<String, String> params = ref.getParameters();
params.put('ckm_campaign_id', '999');
params.put('ckm_key', 'euu87nn');
params.put('ckm_subid', 'Company Name');
params.put('ckm_subid_2', 'Partner');
params.put('ckm_subid_3', 'API');
params.put('first_name', l.FirstName);
params.put('last_name', l.LastName);
params.put('email_address', l.Email);
params.put('phone', l.Phone);
params.put('personal_credit_score', ''+l.Est_Credit_Score__c);
System.debug(
    ref.getUrl().removeStart('?')
);

Output:

ckm_campaign_id=999&ckm_key=euu87nn&ckm_subid=Company+Name& 
ckm_subid_2=Partner&ckm_subid_3=API&email_address=not-my-real-email%40example.com&
first_name=Brian&last_name=Fear&personal_credit_score=999&phone=%28999%29+555-1212

(Note: Line breaks added for legibility, will not appear in the actual output.)

You must log in to answer this question.

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