Salesforce: Accessing Documents in Document sObject from Apex - salesforce

I'm trying to access a csv document in my Document sObject via apex (in order to parse the csv). I know how to parse CSV one I get the data (there are many articles on that part), but I'm not sure how to grab the document from the documents sObject in the first place!
How do I reference the CSV document (in sObject Document) from Apex?
I did see that a URL is returned from the following query:
SELECT body From Document WHERE name = 'myCSV'
That returns something like:
/services/data/v32.0/sobjects/Document/00000UptFER/Body
Do I need to point the parser to that address?
Thank you!

tostring() is the solution.
Document d = [SELECT id, body
FROM Document
WHERE Name='myCSV' LIMIT 1];
string strBody = d.body.tostring();

Related

How to query more than 200 field; Salesforce SOQL Fields

I am facing an issue using Salesforce API. While querying I am getting the following exception: "The SOQL FIELDS function must have a LIMIT of at most 200". Now, I understand SF expects a max of 200 only. So, I wanted to ask how can I query when the results are more than 200?
I can only use REST API to query, but if there is another option, then please let me know and I will try to add it in my code.
Thanks in Advance
You could chunk it, SELECT FIELDS(ALL) FROM Account ORDER BY Id LIMIT 200. Read the id of last record and in next query add WHERE Id> '001...'. but that's not very effective.
Look into "describe" calls, waste 1 call to learn names of all fields you need and explicitly list them in the query instead of relying on FIELDS(ALL). You can compose SOQL up to 20k characters long and with "bulk API" queries you could fetch up to 10k records in each API call so "investing" 1 call for describes would quickly pay off.
You could even cache the describe's result in your application and fetch fresh only if something interesting changed, there's rest API header for that: https://developer.salesforce.com/docs/atlas.en-us.232.0.api_rest.meta/api_rest/sobject_describe_with_ifmodified_header.htm
Try this it is Helpful:
// Get the Map of Schema of Account SObject
Map<String, Schema.SObjectField> fieldMap = Account.sObjectType.getDescribe().fields.getMap();
// Get all of the fields on the object
Set<String> setFieldNames = fieldMap.keySet();
list<String> lstFieldNames = new List<String>(setFieldNames);
// Dynamic Query String.
List<Account> lstAccounts = Database.query('SELECT ' + String.join(lstFieldNames, ',') + ' FROM Account');
system.debug('lstAccounts'+lstAccounts);

How to get salesforce Activity id

I have a salesforce query that extracting users time report
SELECT ID,Logged_Date__c ,CreatedBy.Email, CreatedBy.id, CreatedBy.Name, Time_Spent_Hours__c, Activity__c, CaseId__r.CaseNumber, CaseId__r.Account.id, CaseId__r.Account.Name , Utilized__c
FROM Time_and_Placement_Tracking__c
The Activity__c returns with the activity text.
I was trying to use Activity__c.Id, Activity__r etc. but all returns with error.
Is there a way to get the Activity id?
Verify these
You need to get to the object definition and see the field info. You can use workbench or any other API tool if you are familiar with and get the object and field def's.
Check the data type for Activity__c field. It should be a lookup/master relation. If it is not, find the field which ties to Activity object.
Open the field to get the API name and use that in the query with a '__r' extension.

OrientDB - find "orphaned" binary records

I have some images stored in the default cluster in my OrientDB database. I stored them by implementing the code given by the documentation in the case of the use of multiple ORecordByte (for large content): http://orientdb.com/docs/2.1/Binary-Data.html
So, I have two types of object in my default cluster. Binary datas and ODocument whose field 'data' lists to the different record of binary datas.
Some of the ODocument records' RID are used in some other classes. But, the other records are orphanized and I would like to be able to retrieve them.
My idea was to use
select from cluster:default where #rid not in (select myField from MyClass)
But the problem is that I retrieve the other binary datas and I just want the record with the field 'data'.
In addition, I prefer to have a prettier request because I don't think the "not in" clause is really something that should be encouraged. Is there something like a JOIN which return records that are not joined to anything?
Can you help me please?
To resolve my problem, I did like that. However, I don't know if it is the right way (the more optimized one) to do it:
I used the following SQL request:
SELECT rid FROM (FIND REFERENCES (SELECT FROM CLUSTER:default)) WHERE referredBy = []
In Java, I execute it with the use of the couple OCommandSQL/OCommandRequest and I retrieve an OrientDynaElementIterable. I just iterate on this last one to retrieve an OrientVertex, contained in another OrientVertex, from where I retrieve the RID of the orpan.
Now, here is some code if it can help someone, assuming that you have an OrientGraphNoTx or an OrientGraph for the 'graph' variable :)
String cmd = "SELECT rid FROM (FIND REFERENCES (SELECT FROM CLUSTER:default)) WHERE referredBy = []";
List<String> orphanedRid = new ArrayList<String>();
OCommandRequest request = graph.command(new OCommandSQL(cmd));
OrientDynaElementIterable objects = request.execute();
Iterator<Object> iterator = objects.iterator();
while (iterator.hasNext()) {
OrientVertex obj = (OrientVertex) iterator.next();
OrientVertex orphan = obj.getProperty("rid");
orphanedRid.add(orphan.getIdentity().toString());
}

Using Salesforce Apex to find a record and pull data from a field

In salesforce we have two objects. The first is a Component pricing object (Comp_Pricing__c). This has records with component part numbers and pricing in their respective fields, Part_Number & Pkg_Price__c. We also have an object in which we put together proposals with quotes. In this object we use an apex class and trigger to run our calculations to determine quantities of parts needed. We would like to have the apex class, based on a variable (apPart), search through the records, find the corresponding part number and then pull back the price for use in further apex calculations. I believe I will need to run a query on the records but have no idea how to do this. Can I get some help?
list cpFTList = [SELECT Pkg_Price__c FROM Component_Pricing__c WHERE Component_Pricing__c.Part_Number__c = :PRT_Pr_Ft]; Pr_Ft = cpFTList[0].Pkg_Price__c;
This is the final query that works.
Thanks Chiz for the help.

What is a dynamic apex in salesforce?

I am in the learning stages of Salesforce Apex. I have read the topic of Dynamic Apex and was not able to understand the concept. Can someone explain it how to deal with it and in which scenarios it is best to use?
Thanks in advance.
Use case 1:
You are developing a page that reads the salesforce object meta data to display the object records to the user. You want to use the describe global methods but you dont know how to combine standard SOQL with the generic SObject type.
Standard SOQL eg
Person__c [] persons = [SELECT Id, Name, Age__c, Height__c FROM Person__c];
But the describe global metadata methods returns a SObject types.
Solution:
Use the describe global methods to get a list of objects, then further get all the fields on that object. Build a SELECT statement in a local string variable with all the fields then execute the query with Database.query().
string objectfullname = 'scenario__c';
Schema.SObjectType targetType = Schema.getGlobalDescribe().get('scenario__c');
if (targetType == null) {
system.debug('Type not found: '+objectFullname);
throw new TypeNotFoundException(objectFullName);
}
Schema.DescribeSObjectResult typedescription = targetType.getDescribe();
Map<String, schema.Sobjectfield> resultMap = typedescription.Fields.getMap();
string query = 'SELECT ' + string.join(new List<string>(resultMap.keySet()), ',') + ' FROM '+ objectfullname + ' LIMIT 100';
sobject [] records = Database.query(query);
Use Case 2
You want to loosely couple your code with custom objects in a beta managed packaged so that the managed packaged can be uninstalled and upgraded.
Solution
When you use the Database.query() method, your code is not compiled against the custom object so it can be re-installed without any need for commenting out code to remove the dependency.
Use Case 3
You have a trigger that copies records to another custom object after insert according to an dynamic field mapping schema. You can't code it in the standard way [SELECT ...] because you only know what object you are inserting to at run time.
Solution
Again, use describe global methods & Database.query to get the records and type information then you can insert into the target object like normal DML.
sobject newRecord = ...
for (integer i = 0; i < fieldCount; i++) {
newRecord.put(fields[i],values[i]);
}
insert newRecord;
If you are doing bulk inserts, like always, make sure that you dont put DML (insert, update) statements in a loop.

Resources