+ Start a Discussion
phantom1982phantom1982 

Constructing a URL with Parameters

Hi,

I have to implement a salesforce to SMS function where user will be able to send an sms from salesforce to client. I am facing a bit of challenge in constructing the URL with parameters in it, that is sent to the SMS service. The SMS text is formatted in a sense that it ends with the Sender Name, Mobile and Company Name e.g:

 

_____________________

This is the test SMS Text

 

Sender Name

Mobile Phone

Company

____________________

 

 

The problem is in the characters used for formatting the text \n and \r.  Below is the code:

 

public PageReference sendSMS(){
		
		
		User u = [select FirstName, LastName, MobilePhone from User where Username =: UserInfo.getUserName()];
		
		SMS__c sms = [select SMS_Text__c, Account__c from SMS__c
		where id = :ApexPages.currentPage().getParameters().get('id')];
		
		Account acc = [Select Phone from Account where Id = :sms.Account__c];
		 
		String smstext = sms.SMS_Text__c+'\n\n'+u.FirstName+' '+u.LastName+'\n'+u.MobilePhone+'\nCyber Consultants';
		//sms.SMS_Text__c = smstext;
		//insert sms;
		String myurl = 'http://mytestcompany.com/sendurlcomma.asp?user=200335466&pwd=jn23e52&senderid=ABC&mobileno='+acc.Phone+'&msgtext='+smstext+'&priority=High&CountryCode=+971';

		
		PageReference mypage = new PageReference(myurl);
	    mypage.setRedirect(true);
	    return myPage;
	}

 Pleas if anyone can lead me to the right direction in generating the proper URL.

 

tom_patrostom_patros

Are you receiving any errors when you save or run this code? What issues are you seeing?

aruna11.3081223889641084E12aruna11.3081223889641084E12

Hi, Please explain in brief, what error you are getting.

 

As a general tip, you can  try \\n in stead of \n. Give a try.

Also, instead of forming url string.


 

phantom1982phantom1982

Problem is when I redirect to the constructed URL page, the SMS service somehow doesn't recognize it and gives error. Following is the final URL in browser:

 

http://smsservice.com/sendurlcomma.asp?CountryCode=+971&mobileno=971553123456&msgtxt=sdfdsds%0A%0Anull+Admin%0A971558123456%0ACyber+%26+Consult&priority=High&pwd=jnse53&senderid=ABC&user=20047451

 

Notice that the above URL has the formatted sms text. i.e.

 

sdfdsds

 

Admin

971558123456

Cyber Consult

 

Now the error is not on VF/Salesforce part, error is on the sms service side that it doesn't recognize the formatting characters in the URL i.e %0A and %26

 

Is there a way to encode the URL so that its recognizable?

 

I have already tried UTF-8 encoding but it doesn't work..

 

Pasting my code again with commented encoding code.

 

public with sharing class SendSMSController {

	public SendSMSController() {

    }
    
    public SendSMSController(ApexPages.StandardController controller) {
    }
	
	
	public PageReference sendSMS(){
		
		
		User u = [select FirstName, LastName, MobilePhone from User where Username =: UserInfo.getUserName()];
		
		SMS__c sms = [select SMS_Text__c, Account__c from SMS__c
		where id = :ApexPages.currentPage().getParameters().get('id')];
		
		Account acc = [Select Phone from Account where Id = :sms.Account__c];
		 
		String smstext = sms.SMS_Text__c+'\n\n'+u.FirstName+' '+u.LastName+'\n'+u.MobilePhone+'\nCyber & Consult';
		//sms.SMS_Text__c = smstext;
		//insert sms;
		String myurl = 'http://smsservice.com/sendurlcomma.asp?user=20047451&pwd=jnse53&senderid=ABC&priority=High&CountryCode=+971';

		PageReference mypage = new PageReference(myurl);
		//PageReference mypage = new PageReference(myurl);
		mypage.getParameters().put('mobileno', u.MobilePhone);
		mypage.getParameters().put('msgtxt', smstext);
		//String myurl1 = mypage.getUrl();
		//myurl1 = EncodingUtil.urlEncode(myurl1, 'UTF-8');
		
		
		
	    mypage.setRedirect(true);
	    return myPage;
	}
}

 

 

 

phantom1982phantom1982

Any one??