I am trying to change the metadata of fields of a object in salesforce using Apex. For example I am trying to make all required field non-required. I was able to retrieve all the required fields using the schema class and using methods like isNillable(). I wanted to ask if there is any way I can modify the metadata.
I have searched a lot regarding this but could not find any helpful results.
Schema.DescribeSObjectResult a_desc = objects.get(Name_of_of_object_whose_fields_are_to_be_retrieved).getDescribe();
Map<String, Schema.SObjectField> a_fields = a_desc.fields.getMap();
Set<string> x=a_fields.keySet();
//I am making a map of fieldname and bool(field required or not)
Map<String,boolean> result=new Map<String,boolean>();
for(String p:x)
result.put(p,a_fields.get(p).getDescribe().isCreateable() && !a_fields.get(p).getDescribe().isNillable() && !a_fields.get(p).getDescribe().isDefaultedOnCreate());
//what I want is to modify isNillable and other attributes and make these changes to the fields.
You can't make all required fields non-required because many of them are required at the database level and cannot be modified.
For example, the Name field (on any object that has a Name field) is always required. You cannot change this property. Likewise, Master-Detail relationship fields are always required, on standard and child objects.
To change the metadata of custom fields that are modifiable, you would have to use the Metadata API. It's not available in Apex, unless you use a wrapper like apex-mdapi. As a warning, modifying your org's metadata in a broad-based way via Apex is dangerous. You can cause damage to your org and its function in this way very easily. I strongly encourage you not to attempt to do this. Required fields are required for a reason.
Related
I'd like to add custom field types like 'email' or other strings that could use regex validation.
Also, numeric fields with validation like < or > or formatting like money.
I was thinking about storing a mapping of schema uuid + field name + field type, and have the UI query a new rest api to get the validation and formatting criteria when editing a node?
Does that sound like a good approach? or is there a better one?
I'm new to mesh, so I'm still learning how to approach customizations.
Thanks!
I think this relates to issue 112. At the moment it is not possible to add custom validation or custom field types. In the future more specific constraints can be added. Additionally we also plan to add a "control" property to schema fields. This field can be used to store any JSON. This JSON control property value can in turn be used in the UI to add custom form behavior or control how front-ends display/handle the value.
I have content type award with few fields (user, position, lesson).
I have create a rule to create new node in rule actions.
When i select node type in rule it's show only two fieds : title and author.
How i get all other fields in rules action or condition .
Thanks
You have to create additional actions to set data values for the additional fields you want filled in.
It looks like (part of) what you're trying to do, is to add a Rules Action like "Set a data value" for the fields you mentioned (like lesson, etc).
But before you will be able to create a Rules Action like "Set a data value" for your field(s), you have to make sure to add a Rules Condition Entity has field (related to the field for which you want to set a value). And make sure to add that Entity has field condition BEFORE other Rules Conditions in which you might want to refer to this field. Depending on what exactly you want to do in your custom rule, an alternative might be to use content is of type.
That's also what is mentioned in the Rules UI, e.g. when you're adding a "data comparison" condition: somewhere it says:
The data selector helps you drill down into the data available to Rules. To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type').
For a video tutorial that illustrates the importance of this Entity has field condition, refer to Data types and data selection, especially what is shown between about 13:30 and 17:30 in it.
I'm trying to get over a limitation in Salesforce where Lead objects can't have related lists that convert with the Lead over to Opportunity, Contact and Account. I have set up 4 objects of type Lookup Relationship, and created a dummy record in each.
I want to use Custom Settings to store the id of each of these dummy records, so that when the Lead converts, any custom objects can also convert to objects with Master/Detail relationships on the respective standard objects.
My trigger on Lead(after update) tries to create a Map of the Custom Settings:
Map cs = AcctId__c.getAll();
AcctId__c is the Custom Setting api name. Compile time is giving me the above message.
Now, I copied this code directly from the Salesforce documentation. What am I forgetting?
I believe that you must include the actual map definition <String,AcctId__c> after the word Map.
Check out this page.
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_maps.htm
I'm having a pretty hard time trying to create a write-once field in a Django model. Ideally I'd want it to work like a final variable, although I can settle for simply preventing it from being edited through the admin.
I know there is a solution for read-only fields, but it also affects the add form, and I don't want the field to be read-only there.
Use get_readonly_fields(), and return a tuple with the write-once field name if obj exists, or an empty tuple if obj is None.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields
I want to ensure an object is unique, and to throw an error when a user tries to save it (e.g. via the admin) if not? By unique, I mean that some of the object's attributes might hold the same values as those of other objects, but they can't ALL be identical to another object's values.
If I'm not mistaken, I can do this like so:
class Animal(models.Model):
common_name = models.CharField(max_length=150)
latin_name = models.CharField(max_length=150)
class Meta:
unique_together = ("common_name", "latin_name")
But then each time I refactor the model (e.g. to add a new field, or to change the name of an existing field), I also have to edit the list of fields in the parenthesis assigned to unique_together. With a simple model, that's OK, but with a substantial one, it becomes a real hassle during refactoring.
How can I avoid having to repeat typing out the list of field names in the unique_together parenthesis? Is there some way to pass the list of the model's fields to a variable and to assign that variable to unique_together instead?
Refactoring models is a rather expensive thing to do:
You will need to change all code using your models since field names correspond to object properties
You will have to change your database manually since Django cannot do this for you (at least the version I used the last time when I worked with Django couldn't)
Therefore I think updating the list of unique field names in the model meta class is the least issue you should worry about.
EDIT: If you really want to do this and all of your fields must be "unique together", then the guy at freenode is right and you'll have to write a custom metaclass. This is quite complicated and errorprone, plus it might render your code incompatible to future releases of Django.
Django's ORM "magic" is controlled by the metaclass ModelBase (django.db.models.base.ModelBase) of the generic base class Model. This class is responsible to take your class definition with all fields and Meta information and construct the class you will be using in your code later.
Here is a recipe on how you could achieve your goal:
Subclass ModelBase to use your own metaclass.
Override the method __new__(cls, name, bases, dict)
Inspect dict to gather the Meta member (dict["Meta"]) as well as all field members
Set meta.unique_together based on the names of the fields you gathered.
Call the super implementation (ModelBase.__new__)
Use the custom metaclass for all your unique models using the magic member __metaclass__ = MyMetaclass (or derive an abstract base class extending Model and overriding the metaclass)