Apex Trigger (should be simple) - triggers

I am trying to write a trigger that will copy a field on a custom object from one field to another during the save or when things have changed. So the custom object is McLabs_Property__c, the field I want to update is the standard field Name, I would like to update this name with the custom field McLabs_Property_Name__c
trigger <name> on McLabs2__Property__c (before insert, before update)
{
for (McLabs2__Property__c a:trigger.new)
{
a.name__r = a.McLabs2_Property_Name__c;
}
}
Thank you very much in advance.

You could also consider using a field update workflow rule.

Related

How to update information in an existing node instead of creating a new one using Dgraph?

I am writing a Golang application using Dgraph for persisting objects. From the documentation, I can infer that a new UID and hence a new node is created everytime I mutate an object/run the code.
Is there a way to update the same node data instead for creating a new node?
I tried changing the UID to use "_:name" for the UID field but even this creates a new node everytime the application is run. I wish to be able to update the existing node if it is already present in the DB instead of creating a new node for it.
Unfortunately the docs aren't very beginner friendly yet :/
To modify / mutate existing data you have to run a set operation and supply a rdf-triple like <uid> <predicate> "value" / <objectYouWantToModify> <attributeYouWantToModify> "quotedStringValue". If it is not an attribute but an edge, the value has to be another <uid>.
The full mutation would be for example
{
set {
<0x2> <name> "modified-name" .
}
}
The . terminates the sequence and there is an optional fourth parameter you can use to also assign a label.
Check https://www.w3.org/TR/n-quads/ for further details.

Extbase Hooks - execute code upon record creation

I want to create a standard typo3 extension but when I create a record (or modify it) I want to calculate something (in my case I want to call the Google Map API to get coordinates from a given address).
SO I search for a hook or something. Any idea?
One of my project example, may helps you for hook in backend when record has been changed.
In your extension file ext_localconf.php
// Hook for cancellation
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:femanager/class.tx_femanager_tcemainprocdm.php:tx_femanager_tcemainprocdm';
hook file class.tx_femanager_tcemainprocdm.php where you can execute
your script
class tx_femanager_tcemainprocdm{
function processDatamap_postProcessFieldArray ($status, $table, $id, &$fieldArray, &$reference){
// $status also called action like delete
// $table - table name of excute backend action
// $id - record UID
// $fieldArray - fields of your table
if($table = 'your_extension_table_name'){
// your script
}
}
}
Maybe this answer is useful to you.
Register your class as a data handling hook in your extension. This one "is called AFTER all commands of the commandmap" were executed. Maybe you need to look for a more appropriate one.
Then in your registered Hook i.e. 'typo3conf/ext/your_ext/Classes/Hooks/AfterCreate.php' do your calculation. Hope this sets you on the right track.
In my special case there was no need to calculate the coordinates when the record got saved. So I just used the listAction in the controller, check if coordinates are there and if not call the Google API (and send an email if the Google API does not give a coordinate back).
In another case where the new record comes from a frontend plugin and I had to do something with this data I used the createAction in the Controller. (I am not sure if the createAction is also called when the record is created from the backend.)

Get field values in record.js

I override the record view, by creating in custom/modules/myModule/clients/base/views/record/record.js this file record.js. I want to get the value of a field for the current object and I use this.model.get('duration'), but I get nothing.The only field available is "id". How can I retrieve the values for others fileds?
When the record.js script is initially called, the model won't have fully loaded, so the only available field with be the id.
Your best bet is probably to override the _renderHtml function; by the the time the view is being rendered all the model details will have fully loaded:
_renderHtml: function() {
// custom code involving this.model.get('duration')
// call parent
app.view.View.prototype._renderHtml.call(this);
}
Note that you may find _renderHtml is called multiple times, sometines before the model is fully loaded. This is just a quirk of Sugar so it may be best to add a check in your code:
if (this.model.get('duration')) {
// custom code involving this.model.get('duration')
}
Dont forget that app.model.get('myfield') only delivers the right content (from this field) when your field is already displayed in detailview - else you will get "undefined"!
So you
Have to call the rest api (rest/v10/yourmodel/yourid) - than you
have all the values available
Display your fields (even you dont want to) to be able to use it in app.model.get('yourfield'), an alternative you could append your record.js (after rendering) with $('div [data-name="yourfield"]').hide();
I know this question is quite old already (but if someone else run into this he could find this useful).

Jira 6: How to assign issue by condition

I creating new workflow and I need to assign issues by condition.
For example:
During create issue if in the dropdown list I select "language_1" issue will be assigned to "translator_1" or if I select "langiage_2", issue will be assigned to "translator_2"
I tried to do this in workflow editor by creating post function, but this functions can't verify conditions. Does Jira have any other method to do it?
Use JIRA components to do this. Create a component named "language_1" with a component lead of your first user. When the issue is created set the component and leave the Assignee at automatic.
I used the Script runner plugin to do something similar by adding a post function to the required transaction. Code example:
from com.atlassian.jira import ComponentManager
customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
userUtil = ComponentManager.getInstance().getUserUtil()
# read field
language = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Language"))
if (language == "language_1") {
issue.setAssignee(userUtil.getUserObject("translator_1"))
} else if (language == "language_2") {
issue.setAssignee(userUtil.getUserObject("translator_2"))
}

Salesforce Apex Trigger "isAPI" Context Variable

Is there a way to determine if a trigger is being executed by an API call or through the Salesforce Web Interface?
I'd like to do something like this:
trigger Update_Last_Modified_By_API on My_Object__c (before update) {
for (My_Object__c o : Trigger.New) {
if (isAPI) {
o.Last_Modified_By_API__c = datetime.now();
}
}
}
(Currently using API version 25.0, though will soon be updating to 26.0)
There is currently no standard way to tell within the trigger what actually caused an update or insert to happen (API, standard page layout, VF page & controller, some other Apex code, etc.). Here's a full list of Trigger Context Variables.
To achieve this, I would suggest creating a custom checkbox field on the object, something like IsAPI__c (with a default value of false). Then all you would need to do is pass in true for that field with any API call, and then check the field in your trigger for each record in the batch (just make sure you remember to reset it to false when you're done so subsequent calls from the UI aren't treated as API calls).
trigger Update_Last_Modified_By_API on My_Object__c (before update) {
for ( My_Object__c o : Trigger.New ) {
if ( o.IsAPI__c ) {
o.Last_Modified_By_API__c = datetime.now();
}
o.IsAPI__c = false;
}
}
If you're just trying to determine whether a transaction was initiated via the UI or not, using the System.URL.getCurrentRequestUrl() method might give you an indication.