I have a PHP site that calls a REST end point on Salesforce via a visualforce page. When trying to validate the JSON response I confirmed all required fields but am now getting this error:
{"success":false, "message":"System.NullPointerException: Attempt to de-reference a null object
This is coming from the following Apex Class:
public class WebDirectController {
public class ApiException extends Exception {}
public String response {get;set;}
private static final String PRIVATE_APPLICATION_KEY = '123';
public WebDirectController(){}
public Pagereference safeAction(){
Savepoint sp = Database.setSavepoint();
String businessUnit;
try {
Map<String, String> params = ApexPages.currentPage().getParameters();
for (String key : params.keySet()){
system.debug(key +' -> '+ params.get(key));
}
if (params.containsKey('Business_Unit__c') == false){
throw new ApiException('Unauthorized!');
}
businessUnit = params.get('Business_Unit__c');
if (params.containsKey('applicationKey') == false){
throw new ApiException('Unauthorized!');
}
if (params.get('applicationKey') != PRIVATE_APPLICATION_KEY){
throw new ApiException('Unauthorized!');
}
// throws ApiException
validateRequiredFields(params);
WebDirectUtility utility = new WebDirectUtility(businessUnit);
utility.duplicateCheck(params);
if (utility.duplicateAccountList.isEmpty() == false){
// if last opp is needs RX attach attachments to opp and flag opportunity
if (utility.duplicateAccountList.get(0).Opportunities.isEmpty() == false
&& utility.duplicateAccountList.get(0).Opportunities.get(0).Status__c == 'Needs Rx'
&& utility.hasAttachments(params) == true){
// save attachment.
utility.createAttachmentList(params);
utility.addAttachmentParentId(utility.duplicateAccountList.get(0).Opportunities.get(0).Id);
insert utility.attachmentList;
// set status to 'Attention- Call Center'
Opportunity opp = utility.duplicateAccountList.get(0).Opportunities.get(0);
opp.Status__c = 'Attention- Call Center';
update opp;
//utility.sendEmail(params, utility.duplicateAccountList.get(0), opp);
response = '{"success":true}';
return null;
}else{
//utility.sendEmail(params, utility.duplicateAccountList.get(0));
response = '{"success":true}';
return null;
}
}
utility.createAll(params);
insert utility.patientAccount;
utility.addAttachmentParentId(utility.patientAccount.Id);
//system.debug('attachmentList -> '+ utility.attachmentList);
insert utility.attachmentList;
utility.addOpportunityAccountId(utility.patientAccount.Id);
//system.debug('patientOpportunity -> '+ utility.patientOpportunity);
insert utility.patientOpportunity;
utility.addLineItemOpportunityId(utility.patientOpportunity.Id);
//system.debug('lineItemList -> '+ utility.lineItemList);
insert utility.lineItemList;
//send success email to kelly
Opportunity opp = [SELECT Opportunity_Url__c FROM Opportunity WHERE Id =:utility.patientOpportunity.Id];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject(businessUnit + ' Direct Success Email');
mail.setHtmlBody('<p>'+ businessUnit +' Direct created this opportunity: '+ opp.Opportunity_Url__c +'</p>');
mail.setToAddresses(new String[]{'pedrick#symplmed.com', 'childs#symplmed.com'});
Messaging.sendEmail(new Messaging.Email[] {mail});
response = '{"success":true}';
}catch(Exception e){
ErrorEmail.sendEmail(e);
system.debug(e);
system.debug(e.getMessage());
system.debug(e.getStackTraceString());
response = '{"success":false, "message":"'+ e.getMessage() +'"}';
Database.rollback(sp);
}
return null;
}
public void validateRequiredFields(Map<String, String> params){
Set<String> requiredParams = new Set<String>{'FirstName',
'LastName',
'PersonBirthdate',
'Phone',
'PersonEmail',
'BillingStreet',
'BillingCity',
'BillingState',
'BillingPostalCode'};
for (String rp :requiredParams){
if (params.get(rp) == '' || params.get(rp) == null){
throw new ApiException(rp +' is required!');
}
}
}
}
Not sure what I am missing but I am going on 2 weeks of trying to figure this out.
Thank You
I wanted to close the loop on this. By adding the stack track to the debug msg I was able to locate an error in the setup that resolved the Apex class issue, The product line item entry in the Utility class was pointing to the wrong pricebook.
Adding the Stack Trace to the debug msg was the trick.
Thank you
Login to SF as the user whose credentials you're using in the PHP application. Open the Developer Console (upper right corner, next to your username), execute the call from PHP. Ideally you'll see a debug log generated with more detailed error message, including the line number & stacktrace.
Alternatively - go to Setup -> Debug Logs and enable tracking of that integration user (this will give you files you can view & download; Developer Console has it's quirks, can be bloody slow but at least it'll parse the log for you so it's easier to read, you can set checkpoints in it...)
Hard to say what might be going wrong, you'll have more luck with stacktrace. My wild guess is that utility.duplicateAccountList is null and then if your code executes null.isEmpty() you'll obviously be slapped with an error ;)
P.S. Have you considered exposing your piece of Apex as a proper 'REST service', not a VF page?
https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_rest
https://trailhead.salesforce.com/en/modules/apex_integration_services/units/apex_integration_webservices
I would like to parse a string such as p1=6&p2=7&p3=8 into a NameValueCollection.
What is the most elegant way of doing this when you don't have access to the Page.Request object?
There's a built-in .NET utility for this: HttpUtility.ParseQueryString
// C#
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
' VB.NET
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
You may need to replace querystring with new Uri(fullUrl).Query.
HttpUtility.ParseQueryString will work as long as you are in a web app or don't mind including a dependency on System.Web. Another way to do this is:
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split('&');
foreach(string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
queryParameters.Add(key, val);
}
}
A lot of the answers are providing custom examples because of the accepted answer's dependency on System.Web. From the Microsoft.AspNet.WebApi.Client NuGet package there is a UriExtensions.ParseQueryString, method that can also be used:
var uri = new Uri("https://stackoverflow.com/a/22167748?p1=6&p2=7&p3=8");
NameValueCollection query = uri.ParseQueryString();
So if you want to avoid the System.Web dependency and don't want to roll your own, this is a good option.
I wanted to remove the dependency on System.Web so that I could parse the query string of a ClickOnce deployment, while having the prerequisites limited to the "Client-only Framework Subset".
I liked rp's answer. I added some additional logic.
public static NameValueCollection ParseQueryString(string s)
{
NameValueCollection nvc = new NameValueCollection();
// remove anything other than query string from url
if(s.Contains("?"))
{
s = s.Substring(s.IndexOf('?') + 1);
}
foreach (string vp in Regex.Split(s, "&"))
{
string[] singlePair = Regex.Split(vp, "=");
if (singlePair.Length == 2)
{
nvc.Add(singlePair[0], singlePair[1]);
}
else
{
// only one key with no value specified in query string
nvc.Add(singlePair[0], string.Empty);
}
}
return nvc;
}
To do this without System.Web, without writing it yourself, and without additional NuGet packages:
Add a reference to System.Net.Http.Formatting
Add using System.Net.Http;
Use this code:
new Uri(uri).ParseQueryString()
https://msdn.microsoft.com/en-us/library/system.net.http.uriextensions(v=vs.118).aspx
I needed a function that is a little more versatile than what was provided already when working with OLSC queries.
Values may contain multiple equal signs
Decode encoded characters in both name and value
Capable of running on Client Framework
Capable of running on Mobile Framework.
Here is my solution:
Public Shared Function ParseQueryString(ByVal uri As Uri) As System.Collections.Specialized.NameValueCollection
Dim result = New System.Collections.Specialized.NameValueCollection(4)
Dim query = uri.Query
If Not String.IsNullOrEmpty(query) Then
Dim pairs = query.Substring(1).Split("&"c)
For Each pair In pairs
Dim parts = pair.Split({"="c}, 2)
Dim name = System.Uri.UnescapeDataString(parts(0))
Dim value = If(parts.Length = 1, String.Empty,
System.Uri.UnescapeDataString(parts(1)))
result.Add(name, value)
Next
End If
Return result
End Function
It may not be a bad idea to tack <Extension()> on that too to add the capability to Uri itself.
If you don't want the System.Web dependency, just paste this source code from HttpUtility class.
I just whipped this together from the source code of Mono. It contains the HttpUtility and all it's dependencies (like IHtmlString, Helpers, HttpEncoder, HttpQSCollection).
Then use HttpUtility.ParseQueryString.
https://gist.github.com/bjorn-ali-goransson/b04a7c44808bb2de8cca3fc9a3762f9c
If you want to avoid the dependency on System.Web that is required to use HttpUtility.ParseQueryString, you could use the Uri extension method ParseQueryString found in System.Net.Http.
Make sure to add a reference (if you haven't already) to System.Net.Http in your project.
Note that you have to convert the response body to a valid Uri so that ParseQueryString (in System.Net.Http)works.
string body = "value1=randomvalue1&value2=randomValue2";
// "http://localhost/query?" is added to the string "body" in order to create a valid Uri.
string urlBody = "http://localhost/query?" + body;
NameValueCollection coll = new Uri(urlBody).ParseQueryString();
I just realized that Web API Client has a ParseQueryString extension method that works on a Uri and returns a HttpValueCollection:
var parameters = uri.ParseQueryString();
string foo = parameters["foo"];
private void button1_Click( object sender, EventArgs e )
{
string s = #"p1=6&p2=7&p3=8";
NameValueCollection nvc = new NameValueCollection();
foreach ( string vp in Regex.Split( s, "&" ) )
{
string[] singlePair = Regex.Split( vp, "=" );
if ( singlePair.Length == 2 )
{
nvc.Add( singlePair[ 0 ], singlePair[ 1 ] );
}
}
}
Just access Request.QueryString. AllKeys mentioned as another answer just gets you an array of keys.
HttpUtility.ParseQueryString(Request.Url.Query) return is HttpValueCollection (internal class). It inherits from NameValueCollection.
var qs = HttpUtility.ParseQueryString(Request.Url.Query);
qs.Remove("foo");
string url = "~/Default.aspx";
if (qs.Count > 0)
url = url + "?" + qs.ToString();
Response.Redirect(url);
Since everyone seems to be pasting his solution.. here's mine :-)
I needed this from within a class library without System.Web to fetch id parameters from stored hyperlinks.
Thought I'd share because I find this solution faster and better looking.
public static class Statics
public static Dictionary<string, string> QueryParse(string url)
{
Dictionary<string, string> qDict = new Dictionary<string, string>();
foreach (string qPair in url.Substring(url.IndexOf('?') + 1).Split('&'))
{
string[] qVal = qPair.Split('=');
qDict.Add(qVal[0], Uri.UnescapeDataString(qVal[1]));
}
return qDict;
}
public static string QueryGet(string url, string param)
{
var qDict = QueryParse(url);
return qDict[param];
}
}
Usage:
Statics.QueryGet(url, "id")
Hit up Request.QueryString.Keys for a NameValueCollection of all query string parameters.
To get all Querystring values try this:
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
Dim sb As New StringBuilder("<br />")
For Each s As String In qscoll.AllKeys
Response.Write(s & " - " & qscoll(s) & "<br />")
Next s
var q = Request.QueryString;
NameValueCollection qscoll = HttpUtility.ParseQueryString(q.ToString());
I translate to C# version of josh-brown in VB
private System.Collections.Specialized.NameValueCollection ParseQueryString(Uri uri)
{
var result = new System.Collections.Specialized.NameValueCollection(4);
var query = uri.Query;
if (!String.IsNullOrEmpty(query))
{
var pairs = query.Substring(1).Split("&".ToCharArray());
foreach (var pair in pairs)
{
var parts = pair.Split("=".ToCharArray(), 2);
var name = System.Uri.UnescapeDataString(parts[0]);
var value = (parts.Length == 1) ? String.Empty : System.Uri.UnescapeDataString(parts[1]);
result.Add(name, value);
}
}
return result;
}
let search = window.location.search;
console.log(search);
let qString = search.substring(1);
while(qString.indexOf("+") !== -1)
qString = qString.replace("+", "");
let qArray = qString.split("&");
let values = [];
for(let i = 0; i < qArray.length; i++){
let pos = qArray[i].search("=");
let keyVal = qArray[i].substring(0, pos);
let dataVal = qArray[i].substring(pos + 1);
dataVal = decodeURIComponent(dataVal);
values[keyVal] = dataVal;
}
This is my code, I think it's very useful:
public String GetQueryString(string ItemToRemoveOrInsert = null, string InsertValue = null )
{
System.Collections.Specialized.NameValueCollection filtered = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (ItemToRemoveOrInsert != null)
{
filtered.Remove(ItemToRemoveOrInsert);
if (!string.IsNullOrWhiteSpace(InsertValue))
{
filtered.Add(ItemToRemoveOrInsert, InsertValue);
}
}
string StrQr = string.Join("&", filtered.AllKeys.Select(key => key + "=" + filtered[key]).ToArray());
if (!string.IsNullOrWhiteSpace(StrQr)){
StrQr="?" + StrQr;
}
return StrQr;
}
I'm building an android app that connects to a remote MySQL database to fetch for a specified data.
I'm using a AsyncTask and JSON to read from the database, the AsyncTask is executed every 20 seconds.
The problem is that when I measure the traffic (upload and download ) for my app, it exceeds the normal values, means the app uses approximately 5 kB every 20 seconds.
Here is the code of the AsyncTask:
Any help will be appreciated.
class NewOrders extends AsyncTask<String,Integer,Integer>{
public Integer doInBackground(String...strings){
int res=0;
String today = todaysdf.format(new Date());
String ftime = fullsdf.format(new Date());
listOrders.add(new BasicNameValuePair("action", "myRequests"));
listOrders.add(new BasicNameValuePair("taxiid", idCab));
listOrders.add(new BasicNameValuePair("driverid", idDriver));
listOrders.add(new BasicNameValuePair("devid", idDevice));
listOrders.add(new BasicNameValuePair("date", today));
listOrders.add(new BasicNameValuePair("time", ftime));
try {
postOrders.setEntity(new UrlEncodedFormEntity(listOrders));
responseOrders = clientOrders.execute(postOrders);
entityOrders = responseOrders.getEntity();
resultOrders = EntityUtils.toString(entityOrders,"UTF-8");
jarrayOrders = new JSONArray(resultOrders);
if (jarrayOrders.length() > 0) {
res = 1;
}
else {
jarrayOrders = null;
}
} catch (Exception e) {e.printStackTrace();}
return res;
}
public void onPostExecute(Integer result){
if(result==1){
playNotification();
notifyNewOrder();
}
}
}
I have one problem..
I am making post request to php script. And I am getting back result as an xml. How can I make my qprogressbar working.
I have tried this:
v
oid MainWindow::init()
{
url = "http://127.0.0.1:8888/direkt_php_qt.php";
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateDataTransferProgress(qint64,qint64)));
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}
void MainWindow::updateDataTransferProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->progressBar->setMaximum(bytesTotal + (bytesTotal * 0.25));
ui->progressBar->setValue(bytesReceived);
ui->progressBar->show();
}
But it's not working. I am getting error:
Object::connect: No such signal QNetworkAccessManager::downloadProgress(qint64,qint64)
How can I make this work with manager variable or something like that.
EDIT 2:
This is for example function that is calling init()
void MainWindow::Citanje_korisnika()
{
init();
QUrl params;
params.addQueryItem("action","Citanje_korisnika");
QByteArray data;
data.append(params.toString());
data.remove(0,1);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/x-www-form-urlencoded"));
reply = manager->post(request, data);
}
I tried your code but I always get unexpectedly out from program. Can you change me my function from which I am calling post method so it can work with init()
donwloadProgress is a signal of QNetworkReply.
Try something like this:
url = "http://127.0.0.1:8888/direkt_php_qt.php";
manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(url));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(updateDataTransferProgress(qint64,qint64)));
I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.
This is what I want :
class user {
var lastname:String;
function user(in_ID:Number){
var ontvang:LoadVars = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
ontvang.onLoad = function(success:Boolean) {
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
};
}
}
I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars :
http://www.actionscript.org/forums/showthread.php3?t=144046
Any help would be appreciated ..
When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.
There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:
import mx.utils.Delegate;
class user {
var lastname:String;
var ontvang:LoadVars;
function user(in_ID:Number){
ontvang = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
ontvang.onLoad = Delegate.create(this, onLoad);
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
};
}
function onLoad(success:Boolean) : Void
{
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
}
}
Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.