I am trying to read csv from Document object and pass it to a method in apex. The csv only contains account Ids. I am getting an error as
"Line: -1, Column: -1
System.StringException: Invalid id: 'csvid173648'"
Below is my code:
String[] filecontent = new String[]{};
Document doc = [Select body from Document where id = 'mydocumentId35661'];
String stringbody = doc.body.tostring();
list<String> filelines = stringbody.split('\n');
System.debug('filelines'+filelines);
Set<Id> accountIdSet = new Set<Id>();
for(Integer i=1; i<filelines.size();i++)
{
String[] cols = new String[]{};
cols = filelines[i].split(',');
System.debug('check cols'+cols);
accountIdSet.add('\''+ cols[0] + '\'');
}
Map<String,Account_Achievement_Assignment__c> partnerAchievementMap;
partnerAchievementMap = PartnerAchievementHandler.createPartnerAchievementForExistingAccount(accountIdSet,false);
I can see the ids are passed correctly in the for loop. Tried to convert string to Id still get the same error. What is the mistake here?
Related
Trying to convert a json string to key value pairs using Newtonsoft but no luck so far.
Response from the API:
var response = #"{'result':{'0199 - B344EE33':
{
'6400_00260100':{'1':[{'val':336688}]},
'6400_00462500':{'1':[{'val':null}]},
'6800_00832A00':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_008AA200':{'1':[{'low':0,'high':null,'val':0}]}
}}}";
Result I want is a new object of key value pairs:
{
"6400_00260100" : 336688,
"6400_00462500" : null,
"6800_00832A00" : 3000,
"6800_008AA200" : 0
}
In the response the result will always be the first and only prop. In the next level the code 0199 - B344EE33 can change but there will be only one prop in this level so we can always take the first one. Then in the last level we always need the val property.
What I have is the following but for getting the key value pairs in a clean way I got stuck:
var json = JObject.Parse(response);
var result = json["result"].First;
var path = result.Path;
UPDATE
var jObjectResult = new JObject();
var response = #"{'result':{'0199 - B344EE33':
{
'6800_10821E00':{'1':[{'val':'SMA Sunny Boy'}]},
'6800_00A21E00':{'1':[{'val':'3.0.0.2222'}]},
'6800_00823400':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_08822B00':{'1':[{'val':'SMA'}]},
'6800_08822000':{'1':[{'val':'Sunny Boy 3.0'}]}
}}}";
var json = JObject.Parse(response);
var json_serial = json["result"].First.Children<JObject>().ToList()[0];
foreach(var token in json_serial)
{
var tokenKey = token.Key;
var tokenVal = token.Value.SelectToken("$.1[0].val");
jObjectResult.Add(tokenKey, tokenVal);
}
You could use SelectTokens with the recursive descent operator .. to find all the val properties, then walk up the chain using .Parent repeatedly to get the corresponding key. Create new JProperties from this information and put them into a new JObject to get your result. Here is a "one-liner":
var result = new JObject(
JObject.Parse(response)
.SelectTokens("$..val")
.Select(jt => new JProperty(
((JProperty)jt.Parent.Parent.Parent.Parent.Parent.Parent).Name,
jt
))
);
Fiddle: https://dotnetfiddle.net/TbZ7LS
At the end with some pointers form #Brian Rogers I came with the following solution:
// Arrange
var response = #"{'result':{'0199 - B344EE33':
{
'6800_10821E00':{'1':[{'val':'SMA Sunny Boy'}]},
'6800_00A21E00':{'1':[{'val':'3.0.0.2222'}]},
'6800_00823400':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_08822B00':{'1':[{'val':'SMA'}]},
'6800_08822000':{'1':[{'val':'Sunny Boy 3.0'}]}
}}}";
// Act
var json = JObject.Parse(response);
var json_serial = (JProperty)json["result"].First();
var jObjectResult = new JObject(
json_serial.Value.Select(p =>
{
return new JProperty(
((JProperty)p).Name,
p.First.SelectToken("$.1[0].val")
);
}));
I try to return user informations json value at my web page' load method. My code looks like this.
DataSet dsX = ReadSql("select top 14 * from fuyeler where inStatus = 1 and inUye = 1");
if (dsX.Tables.Count > 0)
{
OYUNCU oyuncular = new OYUNCU();
for (int i = 0; i < dsX.Tables[0].Rows.Count; i++)
{
oyuncular.oyuncuID = Convert.ToInt32(dsX.Tables[0].Rows[i]["inID"]);
oyuncular.oyuncuMail = dsX.Tables[0].Rows[i]["stMail"].ToString();
oyuncular.oyuncuPass = dsX.Tables[0].Rows[i]["stPass"].ToString();
oyuncular.oyuncuToken = dsX.Tables[0].Rows[i]["stToken"].ToString();
}
string json = JsonConvert.SerializeObject(oyuncular);
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
}`
At the end it returns only 1 value of course. I have tried Arrays and lists but JsonConvert.SerializeObject(array or list) doesnt return as json value. It returns every values on 1 row. I want 1 row for 1 value. What should i do about it?
For this code below Json result looks like this,
{"oyuncuMail":"x#gmail.com","oyuncuPass":"x2008","oyuncuToken":"290620153513","oyuncuID":14}
This output is correct and should dispaly last record of the selected rows from your sql query because in your for loop you don't create new object, you assign values to the same object so it will take the values of the last row
you should do this if you want to get a list :
List<OYUNCU> listOyuncular = new List<OYUNCU>();
for (int i = 0; i < dsX.Tables[0].Rows.Count; i++)
{
var oyunclar = new OYUNCU();
oyuncular.oyuncuID = Convert.ToInt32(dsX.Tables[0].Rows[i]["inID"]);
oyuncular.oyuncuMail = dsX.Tables[0].Rows[i]["stMail"].ToString();
oyuncular.oyuncuPass = dsX.Tables[0].Rows[i]["stPass"].ToString();
oyuncular.oyuncuToken = dsX.Tables[0].Rows[i]["stToken"].ToString();
listOyuncular.Add(oyunclar);
}
string json = JsonConvert.SerializeObject(listOyuncular);
I have been receiving the following message when I try my codes.
'There was an error parsing the query. [ Token line number = 1,Token line offset = 56,Token in error = )] '
Does anybody here knows what's causing it?
This is my code:
var db=Database.Open("databaseName");
var username=WebSecurity.CurrentUserName.ToString();
var newFileName="";
WebImage img=null;
img = WebImage.GetImageFromRequest();
newFileName = Guid.NewGuid().ToString() + "_" +Path.GetFileName(img.FileName);
db.Execute("UPDATE Customer SET custBackground=#0 WHERE custUser=#1)", newFileName, username);
img allows null to be returned, but when I am trying my codes I ensure that img is not null.
Really appreciate any help I can get from all of you! Thanks!
as i'm getting the result of the following, if i'll only use the queryResult[i], the output will be:
object Object
object Object
object Object
object Object
object Object
object Object
as expected
(i have 6 columns in my table)
so what should come after the dot in order to get the names of the columns?
var newQuery = 'SHOW COLUMNS FROM `my_data`.my_table;';
socket.emit('new query', newQuery);
var html='';
socket.on('query results', function(queryResult){
for (i = 0; i < queryResult.length; i++){
html += '<section id="resultTitle">' + queryResult[i].///HERE/// + '</section>';
$('#searchResults').html(html);
}
});
I have that code:
private function handleFlashVarsXmlLoaded(event:Event) : void
{
var secondsplit:String = null;
var item:Array = null;
var string:* = XML(String(event.target.data));
var notsplited:* = string.vars_CDATA; //what is .vars_CDATA?
var splitted:* = notsplitted.split("&");
var datacontainer:Object = {};
var index:Number = 0;
item = secondsplit.split("=");
datacontainer[item[0]] = item[1];
this.parseFlashVars(datacontainer); // go next
return;
}
That function is loaded when URLLoader is loaded.
I think that this function parse a XML file to string(fe. param1=arg1¶m2=arg2), then split it by "&" and then by "=" and add data to datacontainer by
datacontainer["param1"] = "arg1"
But how should the XML file look like and what is string.vars_CDATA
I think, vars_CDATA is just a name of XML field, becourse variable named "string" is contains whole XML. So var "notsplited" contains a String-typed data of this field (I think so, becourse of the line "var splitted:* = notsplitted.split("&");", which splits String to Array).