Create Class object through class name in Salesforce - salesforce

I am new to Salesforce and want to code one requirement, I have an api name in string variable through which I want to create an object of that class.
For Eg. Object name is Account which is stored in string variable.
String var = 'Account'
Now I want to create object of 'Account'. In java it is possible by Class.forName('Account') but similar approach is not working in Salesforce Apex.
Can anyone please help me out in this.

Have a look at Type class. The documentation for it isn't terribly intuitive, perhaps you'll benefit more from the article that introduced it: https://developer.salesforce.com/blogs/developer-relations/2012/05/dynamic-apex-class-instantiation-in-summer-12.html
I'm using this in managed package code which inserts Chatter posts (feed items) only if the org has Chatter enabled:
sObject fItem = (sObject)System.Type.forName('FeedItem').newInstance();
fItem.put('ParentId', UserInfo.getUserId());
fItem.put('Body', 'Bla bla bla');
insert fItem;
(If I'd hardcode the FeedItem class & insert it directly it'd mark my package as "requires Chatter to run").
Alternative would be to build a JSON representation of your object (String with not only the type but also some field values). You could have a look at https://salesforce.stackexchange.com/questions/171926/how-to-deserialize-json-to-sobject
At the very least now you know what keywords & examples to search for :)
Edit:
Based on your code snippet - try something like this:
String typeName = 'List<Account>';
String content = '[{"attributes":{"type":"Account"},"Name":"Some account"},{"attributes":{"type":"Account"},"Name":"Another account"}]';
Type t = Type.forName(typeName);
List<sObject> parsed = (List<sObject>) JSON.deserialize(content, t);
System.debug(parsed);
System.debug(parsed[1].get('Name'));
// And if you need to write code "if it's accounts then do something special with them":
if(t == List<Account>.class){
List<Account> accs = (List<Account>) parsed;
accs[1].BillingCountry = 'USA';
}

Related

How to get a document from mongodb by using its string id and string collection name in SpringData

I know that generally, we need to do something similar to this for getting a document back from mongodb in spring data:
Define a class and annotate it with #Document:
#Document ("persons")
public class Person
Use MongoTemplete:
mongoOps.findById(p.getId(), Person.class);
The problem is that in runtime I don't know the class type of the document, I just have its string collection name and its string Id. How is it possible to retrieve the document using SpringData? Something like this:
db.myCollectionName.findOne({_id: myId})
The result object type is not a concern, it can be even an object, I just want to map it to a jackson JsonNode.
A possible workaround for this you can use the aggregate function of mongooperation like this
AggregationResults<Object> aggResults = mongoOps.aggregate(newAggregation(match(Criteria.where("_id").is(myId)) ,
myCollectionName, Object.class);
return aggResults.getUniqueMappedResult();

getting Value of a field by its Name in apex salesforce

in my visualforce page i have some campaign object first user select an object then there is a multi picklist. in this picklist there is Label for all the fields user selects some fields then i have to show the value of these fields in the selected campaign object
for showing multiple picklist my apex function is
public List<SelectOption> getOptionalFields(){
Map <String, Schema.SObjectField> fieldMap= Campaign.sObjectType.getDescribe().fields.getMap();
List<SelectOption> fieldsName =new List<SelectOption>();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
fieldsName.add(new SelectOption(dfield.getName(),dfield.getLabel()));
}
but i have no idea how to show value for the the field
for exmple i have object instance like
Campaign c;
now i have to get value of any field whose Name is in string form.how to get corresponding value for that field.one solution is just write like
say
String fieldName;
and use multiple if
if(fieldName=='Name')
c.Name=
if(fieldName=='Id')
c.Id=
is there any other convenient method??please explain!!
You need to read about "dynamic apex". Every "concrete" sObject (like Account, Contact, custom objects) can be cast down to generic sObject (or you can use the methods directly).
Object o = c.get(fieldName);
String returnValue = String.valueOf(o);
There are some useful examples on dynamic get and set methods on Salesforce-dedicated site: https://salesforce.stackexchange.com/questions/8325/retrieving-value-using-dynamic-soql https://salesforce.stackexchange.com/questions/4193/update-a-records-using-generic-fields (second question is a bit more advanced)
You'll still need to somehow decide when to return it as String, when as number, when as date... Just experiment with it and either do some simple mapping or use describe methods to learn the actual field type...

Apex Compile Error: sObject type 'Book__c' is not supported

I am new in apex programming. I am using site.com.
I am creating New apex class using this code --
#isTest
private class HelloWorldTestClass {
static testMethod void validateHelloWorld() {
Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);
System.debug('Price before inserting new book: ' + b.Price__c);
// Insert book
insert b;
// Retrieve the new book
b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
System.debug('Price after trigger fired: ' + b.Price__c);
// Test that the trigger correctly updated the price
System.assertEquals(90, b.Price__c);
}
}
but it gives me this error -Error: Compile Error: sObject type 'Book_c' is not supported. If you are attempting to use a custom object, be sure to append the '_c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 13 column 12.
help me out..
Have you actually created such object in your Salesforce instance? There are some standard objects (like User) and you most likely also have the objects related to the CRM product (like Account, Contact, Opportunity) but Book__c will be a custom object created by you or another System Administrator in your organization.
Check Setup (upper right corner in web interface) -> Create -> Objects. There's a youtube tutorial or you can always click "Help for this page" link.
To expose this object on the Site (which means anybody in the world will be able to view and insert Books) you'll have to follow few more steps. But as it's a test class that fails to compile I think you haven't reached this problem yet. More info about permissions: http://login.salesforce.com/help/doc/en/siteforce_data_access_perms.htm

JCR, JackRabbit : Making XPath for search for a Year of a Date and for subnodes properties values

Grettings!.
I am having too much troubling with the following question... i will try to be as clear as possibly.
Currently i have a Jackrabbit JCR implementation running in our web application. All things works fine, buts just a little (big) problem appears when trying to do some specific search.
For a brief synopsis of what kind of data is stored, we have 3 a node class called "Entry", that extends another node class named "BaseEntry" and that extends another called "BaseNode".
The Entry class represents a Node in our JCR system, and has a set of properties (mapped as attributes in the corresponding class), an also inherits the properties mapped in their superclasses as well.
I copy and paste, the important part of the class definition and the properties of interest...
#Node(jcrType = "entry", extend = BaseEntry.class)
public class Entry extends BaseEntry {
... // nothing really important here
}
#Node(jcrType = "baseEntry", extend = BaseNode.class, isAbstract = true)
public abstract class BaseEntry extends BaseNode {
#Collection (jcrType = "attachment",
collectionConverter = NTCollectionConverterImpl.class)
protected List<Attachment> attachments = new ArrayList<Attachment>();
...
}
#Node(jcrType = "baseNode", isAbstract = true)
public abstract class BaseNode {
#Field(jcrName = "name", id = true)
protected String name;
#Field(jcrName = "creationDate")
protected Date creationDate;
...
}
1) How i can make a predicate for select only those nodes (entries) that have a specific year in the property creationDate ignoring the rest. The attribute is of type Date (in the class) and i guess the property is stored in a xs:DateTime format i guess... i really do not know very well... how it really match a Date in the JCR underlying system.
So far i get to this...
there must something like this //element(*, entry)[getYear(#creationDate) == <year>]
must be an integer, string, ... i really don't kwow.
2) How i can make a predicate for select only those nodes (entries) that contains attachments that have name a certain name.
Again the class Attachment, the important part...
#Node(jcrType = "attachment", discriminator = true)
public class Attachment extends BaseNode implements Comparable<Attachment> {
...
}
So far i get to this.. that is working.. but there must be a better way:
//element(*, entry) [jcr:contains(./*,'<nameOfInterest>')]
That all friends, i really apologies for the absent of information that i reader may require to understand better the background of the matter, i guess this is what i can do. I am pretty new to Jackrabbit and JCR, and i have to get the hands (dirty) on it, without knowing very well what i am doing.. and obliviously it began to be very complicated...
Well hope any charity soul can answer this, and help, at least a little :D.
Thanks for advance.
Greetings.
VĂ­ctor.
I'm not an expert, but I try to answer anyway:
Question 1
//element(*, entry)[getYear(#creationDate) == <year>]
I think you could use:
//element(*, entry)[
#creationDate >= '2001-01-01T00:00:00.0'
and #creationDate < '2002-01-01T00:00:00.0']
Question 2
Select only those nodes (entries) that contains attachments that have name a certain name.
I only know the SQL-2 query, using equality on the node name. I'm not sure if this is what you are looking for:
select * from [nt:base] where name() = '<nameOfInterest>'

Grails iterating through database tables

As I'm a bit new to Grails, I'm wondering how I can iterate through the current data i have saved in the database to check if the information already exists.
For instance, lets say I have a domain class for Books and I create an action that automatically adds more books but I want to check if the book.title already exists so I don't add it again.
Side note
I'm just using the default database (whatever is used when the project is set to production mode)
Edit
I'll post my domains so it is a bit easier to understand. Instead of book.title, I changed it to where book belongs to author. So I only want the author added once but able to add many books for it. The issue happens in an action i created in the controller.
Author Domain:
class Author {
static hasMany = [books:Book]
String authorName
String notes
String age
String toString() { authorName }
static constraints = {
authorName()
notes(maxSize:500)
age()
}
}
Book Domain:
class Book {
static belongsTo = Author
String toString() { bookNumber }
Author bookAuthor
String title
String numberOfPages
static constraints = {
bookAuthor()
title()
numberOfPages()
}
}
Book Controller (this is where I'm having issues):
class BookController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
//took out index, create, list, etc. to focus on the once that I'm concerned about
//this action will simple read a text file and add books and authors
def gather = {
def parseData = new parseClient() //parses text file line by line and puts it into a list
def dataHolder = parseData.information //dataHolder will hold data from text file
int linesOfData = dataHolder.size() //get size to iterate and add authors & books
linesOfData.times {
def _author = dataHolder.author[it] //get author - Author
def _age = dataHolder.age[it] //get age - Author
def _title = dataHolder.title[it] //get title - Book
def _pages = dataHolder.pages[it] //get pages - Book
def authorInstance //create new Author to add
authorInstance = new Author() //for some reason I have to create and save AuthorName (can't have other fields) before I can add my Book correctly
authorInstance.setAuthorName(_author)
authorInstance.save()
def bookInstance
bookInstance = new Book() //create a new Book to add
bookInstance.setBookAuthor(authorInstance)
bookInstance.setTitle(_title)
bookInstance.setNumberOfPages(_pages)
bookInstance.save() //has to have access to the authorInstance to add correctly which is why i was wondering how to access the database to grab it if it existed
authorInstance.setAge(_age) //add whatever data is left for Author
authorInstance.save() //save again because cant save it with this information before I add authorInstance to the Book
}
}
}
Text File Content:
//You'll notice that author _Scott Davis_ is in here twice.
//I don't want to add two instances of Scott Davis but need to access it to add the book
//the unique constraint makes the value come up as not null but can't be added
Scott Davis : Groovy Recipes
Bashar Abdul Jawad : Groovy and Grails Recipes
Fergal Dearle : Groovy for Domain-Specific Languages
Scott Davis : GIS for Web Developers: Adding 'Where' to Your Web Applications
So I'm basically looking for a way to add that information and haven't found a way that seems to work without running into random problems.
Hope this clears my question up a bit, as the original question was a bit broad
In this case, you can put a unique constraint on title and grails will do that for you.
You can iterate thru the data, but you probably don't want to load the db if you can avoid it. So you could write a custom query to select the number of books for the title, for example.
Edit: for your updates
You don't need to use setters in your controller. Grails adds setters/getters for you dynamically at runtime. If you want to put some logic in your setters, then you can define your own and use them in that case
Have you looked at the grails documentation? http://grails.org/doc/latest/
you have a static constraints block, but you haven't defined how you want each property to be constrained. for unique title it would be
title(unique:true)
if you want to get a list of author names, you can do
List names = Author.executeQuery('select authorName from Author')

Resources