How to identify a new PFRelation on AfterSave using Parse - parse-platform

Is it possible to identify what has changed on a PFRelation column on the AfterSave method of a table? I want to identify an User who liked an Item. The Item table has a UserLikes PFRelation which has all users that liked that Item. I want to trigger execute a notification whenever a new User likes an Item, but only for that new User, not the ones that liked it previously.

I'm not sure if that is possible. However, I would instead create a cloud code function for the notification, and then trigger that in the afterSave block for the like. If the like was successfully saved, notify the owner of the liked item.
If that doesn't work with your current data model, take a look at how Parse solves liking in the Anypic tutorial app.

Related

Simple private messaging system. How to mark as read

I'm building an app in Laravel, and using the eloquent ORM.
i want to create a simple private messaging system within my application. Nothing mad complex. It will basicaly be like email. It has a recepient, title, content and timestamps.
I want the ability to show if it's read/unread. Would having a column as a boolean called 'read' which has a default value of 0 work. When the user clicks on it, the read attirbute changes to 1, and the message is marked as read.
How would I update the attribute, when the message is opened.
Laravelish way, is to use a column named read_at, and set it to NULL if unread, and the current timestamp when read.
Migration part of read_at may look like:
$table->timestamp('read_at')->nullable()->default(null);
Note: do not forget to add read_at to $dates array so working with read_at is easy - carbon way.
If you are not using front-end framework like Angular or react, make ajax request to your controller function. In controller function update the db table using ORM or Query Builder. Using Ajax you can do it without affecting to the user view.
if you are making simple messaging system, store a flag with read. When the user clicks on a message, take that message's id and update that records read with 1 and unread 0 using an update action.

Laravel audit log for model events using generic way

I am using Laravel 5.2.
I want to create audit log entries for each and every model in my project. Whenever any model gets created, updated or deleted; the audit log for that record should be logged along with old and new values.
For example if I update user's first name from John to George, the audit table entry should be logged something like First name updated from John to George.
I know I can fire created, updated and deleted events and using individual listners I can log this. But I want to create it more generic way.
I don't want to define boot method with created, updated and deleted events in each and every model as I have more than 75 models existing in my project. I don't like duplication of that method in each and every model. I want to make it more generic way, like use something like trait, using that in model should automatically identify which event is fired.
Again, the listener will also listen the event but not for any specific model but generic. I don't want any tight coupling. It should decide which model is updated and make entry of log according to that.
Can any one guide me how to achieve this?

Laravel Cashier - adding a column that must be updated on subscription create, resume or cancel

I want to provide my users with the ability to lock in their rate for X number of years. However, this is not possible with Stripe API or Cashier. That is why I will have to do it on my application end. To do this I will need a subscribed_at column. I know I can get it straight from Stripe customer's subscription (field current_period_start) but it takes too much time to retrieve it and I will need this functionality quite a lot. So, in my subscriptions table, where I have all of Cashier's needed fields (stripe_id, stripe_active, ..., subscription_ends_at) I want to add this subscribed_at column. Now, I want to automatically update this value anytime a subscription is create, resume, or cancel. On cancel I need to set the value to NULL.
I don't want (and it's not a good practice) to modify Cashier's code. (In StripeGateway.php there is a method updateLocalStripeData() which fires exactly for these three options but as I said I don't want to modify it).
I cannot override the StripeGateway.php method updateLocalStripeData() because my Subscription Model does not extend it.
So far the only solution that worked but which I don't really like is to use Model events saving and there retrieve the subscription's current_period_start field and insert it into my Subscription Model's subscribed_at field.
What other ways to do this?

Determine new record in PreWriteRecord event handler and check value of joined field

There is custom field "Lock Flag" in Account BC, namely in S_ORG_EXT_X table. This field is made available in Opportunity BC using join to above table. The join specification is as follows: Opportunity.Account Id = Account.Id. Account Id is always populated when creating new opportunity. The requirement is that for newly created records in Opportunity BC if "Lock Flag" is equal to 'Y', then we should not allow to create the record and we should show custom error message.
My initial proposal was to use a Runtime Event that is calling Data Validation Manager business service where validation rule is evaluated and error message shown. Assuming that we have to decide whether to write record or not, the logic should be placed in PreWriteRecord event handler as long as WriteRecord have row already commited to database.
The main problem was how to determine if it is new record or updated one. We have WriteRecordNew and WriteRecordUpdated runtime events but they are fired after record is actually written so it doesn't prevent user from saving record. My next approach was to use eScript: write custom code in BusComp_PreWriteRecord server script and call BC's method IsNewRecordPending to determine if it is new record, then check the flag and show error message if needed.
But unfortunately I am faced with another problem. That joined field "Lock Flag" is not populated for newly created opportunity records. Remember we are talking about BC Opportunity and field is placed in S_ORG_EXT_X table. When we create new opportunity we pick account that it belongs to. So it reproduceable: OpportunityBC.GetFieldValue("Lock Flag") returns null for newly created record and returns correct value for the records that was saved previously. For newly created opportunities we have to re-query BC to see "Lock Flag" populated. I have found several documents including Oracle's recomendation to use PreDefaultValue property if we want to display joined field value immediately after record creation. The most suitable expression that I've found was Parent: BCName.FieldName but it is not the case, because active BO is Opportunity and Opportunity BC is the primary one.
Thanks for your patience if you read up to here and finally come my questions:
Is there any way to handle PreWrite event and determine if it is new record or not, without using eScript and BC.IsNewRecordPending method?
How to get value of joined field for newly created record especially in PreWriteRecord event handler?
It is Siebel 8.1
UPDATE: I have found an answer for the first part of my question. Now it seems so simple to me that I am wondering how I haven't done it initially. Here is the solution.
Create Runtime Event triggered on PreWriteRecord. Specify call to Data Validation Manager business service.
In DVM create a ruleset and a rule where condition is
NOT(BCHasRows("Opportunity", "Opportunity", "[Id]='"+[Id]+"'", "AllView"))
That's it. We are searching for record wth the same Row Id. If it is new record there should't be anything in database yet (remember that we are in PreWriteRecord handler) and function returns FALSE. If we are updating some row then we get TRUE. Reversing result with NOT we make DVM raise an error for new records.
As for second part of my question credits goes to #RanjithR who proposed to use PickMap to populate joined field (see below). I have checked that method and it works fine at least when you have appropriate PickMap.
We Siebel developers have used scripting to correctly determine if record is new. One non scripting way you could try is to use RuntimeEvents to set a profileattribute during the BusComp NewRecord event, then check that in the PreWrite event to see if the record is new. However, there is always a chance that user might undo a record, those scenarios are tricky.
Another option, try invokine the BC Method:IsNewRecordPending from RunTime event. I havent tried this.
For the second part of the query, I think you could easily solve your problem using a PickMap.
On Opportunity BC, when your pick Account, just add one more pickmap to pick the Locked flag from Account and set it to the corresponding field on Opportunity BC. When the user picks the Account, he will also pick the lock flag, and your script will work in PreWriteRecord.
May I suggest another solution, again, I haven't tried it.
When new records are created, the field ModificationNumber will be set to 0. Every time you modify it, the ModificationNumber will increment by 1.
Set a DataValidationManager ruleset, trigger it from PreSetFieldValue event of Account field on Opportunity BC. Check for the LockFlag = Y AND (ModificationNumber IS NULL OR ModificationNumber = 0)) and throw error. DVM should throw error when new records are created.
Again, best practices say don't use the ModNumbers. You could set a ProfileAttribute to signal NewRecord, then use that attribute in the DVM. But please remember to clear the value of ProfileAttribute in WriteRecord and UndoRecord.
Let us know how it went !

MS CRM Save + Copy as new (Custom Entity)

I have a custom entity in Microsoft CRM (4.0). The user has to input records however usually they have a batch of 20+ records that are almost the same apart from 2 or 3 fields which need changing. I know I need to write some custom code to enable this functionally. However can anyone recommend any methods to do this.
Ideally there should be a button that will save and create a copy as a new entity.
My Current way of thinking is to pass all the details as part of the URL and use javascript to strip them out on the page load event. Any ideas welcome.
Thanks
Luke
I found the answer here:
http://mscrm4ever.blogspot.com/2008/06/cloning-entity-using-javascript.html
I've used it and it appears to work well.
Since there are numerous fields, but only certain fields values are different, then i am thinking to set the default value to all the fields, so that users just need to alter those values when needed.
In my approach, i will hook a javascript function on load of the form data entry screen and use XmlHttp approach/Ajax approach to hook to the custom web service to pull/retrieve the default values of each fields. Or you can set those values at the javascript function itself, but the drawback of this, it's difficult to customize later. So i will choose the approach to hook to the custom web service and retrieve those value from some application parameter entity.
Your idea of providing a "clone" button is also a great idea, which means that it will duplicate all the attributes of the previous record, into a new record, so that it will save time for data entry person to customize the different value
EDIT
Since you would enter records in batch mode, how about customizing .ASPX screen to enter records. By customizing through .ASPX screen, you can use a tab , so that users can browse through tabs, to customize the value/attribute of each record.
There will be a "save" button as well as "clone" button to clone some common attribute or value.
I would create a custom web service that would accept the entity type and the ID of the record I'm cloning. Your "Save and Clone" button would call the service, and the service would handle the details of retrieving the current record and deciding which fields to set on the new record. The service creates the record, and sends the Guid of the record back to your button, which then opens up the newly created record.
This way, you avoid the messiness of setting/getting values in JavaScript and tying which fields to set/retrieve directly to your OnLoads, as well as avoiding the possibility of query string that's too long.
The service could easily be sufficiently generalized so that all you'd have to do is add your button to any entity, and it would work, assuming you'd set up your service to handle that particular entity.
One possible downside is that since the clone record button would actually create the record, the user would be forced to delete the cloned record if they decided they didn't want to clone the record after all.

Resources