I have a question, please tell me how can I set by "Flag" and "Rules" module that an entered user automatically, could adds the other users.
Thanks!
# Thank you! Shabana Blackborder
Basically, if you mean you would like to flag other users like in a 'friendship' type flag, when a new user is created, all you need to do is:
Create a flag which flags users, called 'flag users'
Create a rule where the event could be 'After saving a new user account'
a. Create an action to that rule where 'flag user' is the action
a. Then, you select the 'flag users' flag which you created earlier
b. The user to flag: you can use the data selector to select whom you would like to flag or alternatively, use the direct input to specifically add a userid.
c. User on whose behalf to flag: site:current-user or whatever you choose
d. Click save.
That's all you need. So now, every time a new user is saved, the rule will automatically fire to flag the users of your preference.
Related
Is there any way to properly save a user's role history without the need for a database and/or a JSON file on Discord.js? I would like to create a function that logs the roles a user has at the beginning of the command and gives them back after a specific function is triggered.
There is an easy way to save a user's Role history on Discord without the need for a Database and/or any JSON file, you must add a few lines to your code in order to do this.
You first need to make sure your Discord bot has "Privileged Gateway Intents" enabled within the Discord Developer Portal panel, this will highly improve the accuracy of the bot's cache fetching tasks. Next, create a constant for the user mention or user ID, just like the following:
const saveRolesFor = (message.mentions.members.first() || message.guild.members.cache.get(args[0]));
With these 2 arguments, you will be able to state the id of the user or mention the user instead.
Then you must save the roles for the specified user inside another constant variable, just like the following:
const roleHistory = saveRolesFor.roles.cache.map(r => r.id);
The reason why mapping the roles is the best thing to do is due to the roles cache containing other elements, aside from the actual user roles, mapping them would simply include the actual roles of the user, preventing potential issues.
Now you must state the new role you'd like the user to have, just like the following:
let roleToAdd = message.guild.roles.cache.find(r => r.name === 'Role');
Time to remove all of the previous roles and add the roleToAdd to the user, you should do it as:
await(saveRolesFor.roles.set([roleToAdd.id]));
You will want to set the user's roles, as this function automatically removes any other role the user had, and add the single role you have stated, you no longer require the .remove and .add functions.
After these functions have been performed, you can be creative and add the rolehistory back after certain functions are completed.
A very simple example of this would be to add the roles back to the user once a message is sent, for example:
message.channel.send(`The roles for ${saveRolesFor} have been added back!`)
.then(saveRolesFor.roles.set(roleHistory));
All of the user's previous roles will be added successfully, and the roleToAdd will be removed, as you .set the roleHistory of the user, meaning you added those specific roles only. The .then function is calling the roleHistory set after the message is sent to the channel, you could modify it to properly fit into what you attempt to obtain out of your code.
This won't be the best way to add the roles back, as it will take less than a second, but the functions shown above will be highly useful at the time of creating a similar command.
Hope it was helpful!
Background
I'm trying to prevent editing of a record based on one of it's fields having a non-blank value. I'd like to do it with permissions and sharing rules, but I'm not sure what the best way to set it up is. I'm trying to avoid triggers and adding extra fields if possible.
At my work, we're using an object to represent a Job being worked on in the field. A lot of different users need access to edit a Job in progress and so it's set with public read/write sharing rules. Once a job is completed though, it's processed by billing and has an Invoice # field filled in. Once that's set, only billing users should be able to edit the record.
What I've Tried
I first tried a validation rule that fires when Invoice # is filled and the user's profile is not the billing team. This worked initially, but caused triggers to fail when a billing user tried to edit afterwards allows the Invoice # to be set and saved, but triggers that fire afterwards then trip and fail on the validation rule.
Next and currently, I'm playing with sharing rules to accomplish this, which seems like the right path to go down. However, I also want to prevent editing of child records in a related list under the Job. It doesn't seem like I can use cross-object fields or any formula fields in a sharing rule, so I'm not sure if this is possible without using triggers.
What I Could Try
I know I can use sharing rules with a trigger to set a sort of Editable or Locked checkbox field on the record and children when the Invoice # field is set, but I really don't like the idea of that much overhead for something seemingly simple.
Question
What's the cleanest way to allow users to edit a record until a certain field is non-null, after which only a subset of users can edit? If possible, how can I extend that to child objects in a related list?
I would use an on before update trigger to accomplish this.
Something like this:
if (Trigger.isUpdate && trigger.isBefore && UserInfo.getUserRoleId() != billingRoleId) {
for (Job__c theJobRecord : Trigger.new) {
if (String.isNotBlank(theJobRecord.Invoice_Number__c)) {
theJobRecord.Name.addError('You do not have permissions to modify this record!');
}
}
}
If you want to do this inside a Validation rule check if the job record invoice number is blank or use the global merge field $UserRole and check the name of the role else fail the Validation rule.
Is there an easy way to save a recordset, i mean multiple records, but only the "new ones"?
I have a table users and a users form in the view. First field you must enter is passport number, so if user already exists in database the rest of the form will be auto completed and disabled to prevent changes but if passport dont exist then you have to enter all data. As anyone can change those existing users data controls from the browser even if they are disabled, i want to make sure only new records are saved in database. First of all i thought i could find again in database and delete existing users from recordset before save, but i wonder if there is a more elegant approach.
Ty in advance.
I write this here, cause comments are too short:
Thank you for your answer, André. I'm sorry if i didnt explain perfectly, but the form is done by disabling all controls except passport and if passport dont exist (i check it on passport focusout) then the rest of controls are set to enabled. I mean, that is already done. The question was only about the saving.
The validation method you talk about, well i'm actually validating all the controls in the form and i must disable the 'unique' rule so a user can link an existing user to the current bill, otherwise it will fail validation on submit and it wont allow the user to proceed (i did this because it happened to me when testing). The actual setting is much more complicated: the form belongs to a model (bills) which is associated with 4 other models and 2 of those relationships are many to many, bills_users and bills_clients, where users are the persons who do the job and clients pay for it, but I was trying to make the question easier. Anyway, what I am looking for is, in fact, some kind of saving setting which I can't find. In documentation I found "When converting belongsToMany data, you can disable the new entity creation, by using the onlyIds option. When enabled, this option restricts belongsToMany marshalling to only use the _ids key and ignore all other data." The first half of the sentence was promising, but the explanation says different, and I actually tried it without success.
First:
Is there an easy way to save a recordset, i mean multiple records, but only the "new ones"?
Yes there is you can validate it in model, something like this:
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique('passportNumber'));
return $rules;
}
This will prevent to save a duplicate passport number register, but you can different.
I have a table users and a users form in the view. First field you must enter is passport number, so if user already exists in database the rest of the form will be auto completed and disabled to prevent changes but if passport dont exist then you have to enter all data.
There is two different ways to do this:
First you have your form, you develop an ajax function when you fill the first field (passport number) this ajax function do a request to your controller to search for a passport with that number if find something get data and fill others fields and turn them just readable, else just nothing and let user fill the fields by himself
second add a step-by-step where you first do a form to try find by pass number, user fill this only field with a number then submit, on submit this search for a record, if find fill the entire next step fields, else the next step will be the rest of form with the fields to be filled.
This may help you too: https://book.cakephp.org/3.0/en/orm/validation.html
Tell me how you decided to do :)
In system I am developing, user can access particular page which is used to make DML operation on custom object. I want to fulfill following business case :
User A opens a Page made changes on page but did not click Save.
At the same time User B opens a page made changes and click Save. User B data got saved in record.
Now user A came and click Save. But as data is outdated I want to prompt user that "you are over righting the latest update. Do you want to continue."
Is there any faster out-of-box way available to achieve it ?
Assuming this is in a VisualForce page, you could check if the value has changed inside your Save method.
Query for the LastModifiedDate of all records you may change, and save that in your controller. This should be done in your controller's constructor and/or anywhere you re-load the record(s).
In the save method, first query the record(s) again to check if the LastModifiedDate has been changed. If it has been changed, prompt the user.
Assuming you are trying to insert a record into an object.
You can write a validation rule on that object. Check if that record is already inserted or not and show an error message.
Regards,
Naveen
autorabit
We have been trying for a while to solve a Drupal Rules problem.
The situation is;
On every cron run we would like to check for Users who have a certain Role.
We will then send an email to those Users.
But we only want this email to go once.
We are thinking therefore that we will create a new custom/hidden User field called 'email sent'. Then the rule will;
EVENT: On every cron run
CONDITION: Check for Users who have the role AND have a null value in that field
ACTIONS: Send an email to the
users AND set the value of the user 'email sent field to 1.
We think this is possible but we can't see how you can set up a rule to do this.
Any help really appreciated
I think that you can resolve your problem via a custom module where create an action rule that set this fields. In situation like yours I follow this method. Search hook_rules_action_info().
Or if you use profile2 module you can see if there is a rule that can be set this value. I think that there is because Profile2 support Rules.
M.