How should I delete a field with the Profiles library - google-apps-script

Suppose you have a complete user entry with al details like department, company name, job title, some custom fields, etc.
How should we delete a field or leave it blank; for example, I if I try to leave blank company name to delete it, I get a "Cannot call method "setCompanyName" of null, so how should deletion of this information should be managed?

With single value fields like Department, CompanyName etc you can set the value to an empty string (not null) to remove the existing value. For example:
profile.setCompanyName('');
For custom fields, use .deleteCustomeField() and fields for which there can be multiple instances use the provided delete methods like .deletePhoneField(). Use them like this:
var phones = profile.getPhones();
for (var i in phones) {
phones[i].deletePhoneField();
}
The ProfilesApp Tests script has examples of all these in unit tests.

Related

SSIS combine 2 sets of columns into one based on given data

I have following ONE dataset with additional columns not mentioned for ease of reading,
Address1_PostCode Address2_PostCode
Address1_Line1 Address2_Line1
Address1_Line2 Address2_Line2
Address1_Country Address2_Country
Desired output is,
Address2_PostCode
Address2_Line1
Address2_Line2
Address2_Country
Here is what I am trying to do,
If any of Address1 field has data then overwrite Address2 fields, e.g. if Address1 only has postcode and Address2 has Country, the final result will have only postcode and country will be empty or null
If all fields of Address1 are empty then don't do anything
I already searched on my own and I can see there is functionalities like replace column to add new and replacenull function, but I am not able to understand them enough to achieve my goals
Add a Derived Column transformation that has a new column expression with a boolean result that will check whether all Address1 fields are empty/null or not.
Add another Derived Column transformation after that one (unless you want to repeat the validation expression on each column) that checks for this bool result and reassigns the proper field from either Address1 or Address2, on each field you need.
On this last step you can either add new columns to the flow or overwrite existing ones, just make sure that you are using the ones being checked.
I would suggest an alternate data model which allows you to keep your original data intact.
Add a multicast (this will allow the data to duplicate)
Have one flow handle the normal non-address flow
Add a new flow to handle person address data.
Add a script task (this will be used to normalize the addresses
Mark the key as input and all the address columns as input
Create a new output (call it Address) with PersonKey, AddressType, AddressLine1, AddressLine2, PostalCode, Country
Add this simple code.
AddressOutputBuffer.AddRow();
AddressOutputBuffer.PersonKey = Row.PersonKey;
AddressOutputBuffer.AddressType = "Address1";
AddressOutputBuffer.AddressLine1 = Row.Address1_Line1;
... (Add the rest in here)
AddressOutputBuffer.AddRow();
AddressOutputBuffer.PersonKey = Row.PersonKey;
AddressOutputBuffer.AddressType = "Address2";
AddressOutputBuffer.AddressLine1 = Row.Address2_Line1;
... (Add the rest in here)
Write this new Person Address info to a new table (you can build whatever logic you want to the queries you write or you can create a view to handle your specific logic.)
NOTE: You may need to null handle the code above
Ex:
AddressOutputBuffer.AddressLine1 = !Row.Address1_Line1_IsNull?Row.Address1_Line1:"";

Postgresql Update JSON Column Retaining Some KeyValues and Adding Additional KeyValue as null

I am trying to use a metadata JSONB column for a multi-tenant application. Every user for each tenant must have the same metadata, but the tenants have varying metadata fields.
In order to keep all user metadata in sync per tenant, when the tenant admin modifies the metadata fields I need to make sure all users have their metadata JSONB column updated with the following criteria:
If the metadata field/key already exists, the value needs to be retained
If the metadata field/key is new, the key needs to added with a null value
If there are any metadata fields/keys that are not included in the updated list, they should be deleted from the JSON object
For example, all the users for Tenant #1 have the following metadata assigned: { "EmployeeNo" : 123, "HireDate" : "2012-10-10", "Age" : 43 } and somewhere down the line the admin decides they don't care about Age, but they do want to start tracking ParkingSpace.
I need the new metadata record to retain the EmployeeNo and HireDate values, remove the Age key/value, and add the ParkingSpace key with a null value. { "EmployeeNo" : 123, "HireDate" : "2012-10-10", "ParkingSpace" : null }.
I would have thought that I could run an update query similar to the following where it returns a JSONB object where it selects the values if the key exists and a null if it doesn't:
UPDATE users SET metadata = metadata[keys: 'EmployeeNo', 'HireDate', 'ParkingSpace'] WHERE tenant_id = 1;
Obviously that won't work, but hopefully it indicates the issue?
Update: I might have misunderstood your question. Maybe you wanted something like that instead:
UPDATE users
SET metadata = (SELECT json_object_agg(n,metadata->>n) FROM unnest(ARRAY['EmployeeNo','HireDate','ParkingSpace']) AS t(n))
This solution involves creating a brand new jsonb object by extracting only the field that you want from the original metadata. The fields to be copied over are specified as an array that you can easily customize.
Original answer: I think this should do it:
UPDATE users
SET metadata = (metadata - 'Age') || '{"ParkingSpace": null}'::jsonb;
I am using the || operator which merges 2 jsonb objects into one and the - operator which removes a key/value pair.

UPDATING partial string from a field mysql

I have a table with columns like:
emails, country_code etc
Some of the rows contain emails like:
XXXXX#googlemail.com
I want to change about 10,000 records like this in a way that will affect only the "googlemail.com" part of the value and change all of them to "gmail.com".
UPDATE exmple_table SET emails = REPLACE(emails, '%googlemail.com','%gmail.com');
I tried to find and replace but that making me have to type all 10,000 addresses in the query, any solutions?
You can use 'like' operator to filter out the records which contain 'googlemail' and then perform the string replace on them, as shown below:
update table
set SET emails = REPLACE(emails, 'googlemail.com','gmail.com')
where emails like '%googlemail.com%'

Rails find_by with OR

I have a named scope set up in my rails application that is used to locate a record either by its ID (directly from the index view) or a UUID (from an email - basically only so that users can't enter in any ID and view a record)
scope :by_uuid, lambda { |id| where('id = ? OR uuid = ?', id, id) }
This is used in the show action, so the ID comes from the url, like
services/114
services/74c083c0-8c29-012f-1c87-005056b42f8a
This works great, until you get a UUID such as 74c083c0-8c29-012f-1c87-005056b42f8a
This, rails unfortunately converts to the int value and we get services/74 AS WELL AS the record with the correct UUID
Adding a .first to the scope will not help because the order could be different for each record, so that does not work.
Is there a way to prevent rails from converting the ID like this and taking the string literally? Obviously, there will not be a record with an ID that matches that, but if it goes willy-nilly with the integer values of the string passed to it, we could get anything back.
Using the dynamic finders, such as
Service.find_by_uuid
or
Service.find_by_id
work as intended, however we need to be able to retrieve a record using the UUID OR the ID in the same method (show).
Is there a way to do something like
Service.find_by_id_or_uuid
We fixed this issue with the following change to the scope:
scope :by_uuid, lambda { |id| where('binary id = ? OR uuid = ?', id, id) }
This ensures that the id string is taken by its binary value instead of being converted into an int. However, this will ONLY WORK WITH MYSQL BASED APPS

Setting the APP_USER as a value to be passed to target page in APEX

I'm making a student management system on apex. The short of it is a place where lecturers and students can log on. Lecturers create assignments, assign them, mark them, take attendance, record issues ...... all that, and students log on to view their attendance and results.
Now when a student clicks the "My Results" link it navigates to the same page that a lecturer sees, though the select list where a student is selected to view the results of is hidden. The select list displays the students name and returns the id for that student, which also happens to be the user name for a student to log in.
So i want to pass the value of the app-user when a student clicks the link so that only their results are shown.
I've tried to set
these items
:P10_SELECT_STUDENT
with these values
#APP_USER#
which works but no the message "no data found" is shown.
Just for testing i've set that select list to be displayed for a student, and when the page loads it loads with the null value at the top of the list which is
display value
select a student
return value
-1
I've gone and manually set the value passed to be the id of the test student. works a treat, the data loads for that student!!
So does anyone know why the #APP_USSER# value im sending isnt being set in the listbox
Thanks in advance
What you need is the session state of the APP_USER variable. In a query you would reference this with :APP_USER. When you need to pass on the value as a parameter for, for example, a link, you would use the substitution string notation &APP_USER. Much the same way you would refer any other variable/page item.
For example, setting up a button:
A good page to read up on Substitution strings: Application Builder Concepts
The hash-sign notation is commonly used for non-plsql-variables substitution, like the value of a column in report when passed through in a link,
Zac,
If the users haven't authenticate themselves then the :APP_USER parameter is null. If the user authenticated via a SSO, or via DB credentials or whatever you have in the application then the :APP_USER will get populated.
Here is what i understood :
In page 12 , P12_STUDENTS is a select list, and you want it to reflect the current student if he enters or the full list if he's a teacher right?
You don't need to pass :APP_USER via a link or a branch or whatever. It exists as a global Apex variable and is visible in page 12 via :APP_USER but again , its null if the user is not authenticated.
Your select list source should be like:
select display d, return r
from table
where (:APP_USER is NULL
OR (:APP_USER IS NOT NULL AND :APP_USER = student))
tell me if this helps?
regards,
Alex