How to use the validation method? - arrays

Hi i made a portal for institute and now need to use validation method so username cannot be repeated at the time of registration. So how to use array list in validate method.

You need to check all records of table if you use array list for check repeated username instead of this use
"select * from table_name where username='pass the string that user enter for user name'"
You can get only one row if that user name exist so it will reduce your code.
If it's not repeated then returning row will be zero so you can easily check this by simple if and else condition.

Related

Trigger to restrict duplicate record for a particular type

I have a custom object consent and preferences which is child to account.
Requirement is to restrict duplicate record based on channel field.
foe example if i have created a consent of channel email it should throw error when i try to create second record with same email as channel.
The below is the code i have written,but it is letting me create only one record .for the second record irrespective of the channel its throwing me the error:
Trigger code:
set<string> newChannelSet = new set<string>();
set<string> dbChannelSet = new set<string>();
for(PE_ConsentPreferences__c newCon : trigger.new){
newChannelSet.add(newCon.PE_Channel__c);
}
for(PE_ConsentPreferences__c dbcon : [select id, PE_Channel__c from PE_ConsentPreferences__c where PE_Channel__c IN: newChannelSet]){
dbChannelSet.add(dbcon.PE_Channel__c);
}
for(PE_ConsentPreferences__c newConsent : trigger.new){
if(dbChannelSet.contains(newConsent.PE_Channel__c))
newConsent.addError('You are inserting Duplicate record');
}
Your trigger blocks you because you didn't filter by Account in the query. So it'll let you add 1 record of each channel type and that's all.
I recommend not doing it with code. It is going to get crazier than you think really fast.
You need to stop inserts. To do that you need to compare against values already in the database (fine) but also you should protect against mass loading with Data Loader for example. So you need to compare against other records in trigger.new. You can kind of simplify it if you move logic from before insert to after insert, you can then query everything from DB... But it's weak, it's a validation that should prevent save, it logically belongs in before. It'll waste account id, maybe some autonumbers... Not elegant.
On update you should handle update of Channel but also of Account Id (reparenting to another record!). Otherwise I'll create consent with acc1 and move it to acc2.
What about undelete scenario? I create 1 consent, delete it, create identical one and restore 1st one from Recycle Bin. If you didn't cover after undelete - boom, headshot.
Instead go with pure config route (or simple trigger), let the database handle that for you.
Make a helper text field, mark it unique.
Write a workflow / process builder / simple trigger (before insert, before update) that writes to this field combination of Account__c + ' ' + PE_Channel__c. Condition could be ISNEW() || ISCHANGED(Account__c) || ISCHANGED(PE_Channel__c)
Optionally prepare data fix to update existing records.
Job done, you can't break it now. And if you ever need to allow more combinations (3rd field) it's easy for admin to extend it. As long as you keep under 255 chars total.
Or (even better) there are duplicate matching rules ;) give them a go before you do anything custom? Maybe check https://trailhead.salesforce.com/en/content/learn/modules/sales_admin_duplicate_management out.

How to store all Azure AD user group members in an array using a logic app

I am working on a logic app that will create users in third party applications based on AAD group membership. To avoid issues when the group has more than 999 users I have implemented paging. I first get the first 50 users, and a NextLink that I call to get the next 50. This loop runs fine.
Snippet of logic app
When no more nextlink is found, the loop exits. During the loop iterations, I need to store the user information (first name, lastname, UPN etc) in an array so i can process everyone after running through the loop. I have tried running the Union expression as follows:
union(variables('AllUserInfoArray'),body('HTTP_-_Request_My_Group_Name_group_members')['value'])
But this does not add the user data to the AllUserInfoArray, it creates a new array (Compose->Outputs). How do I add all userdata into the AllUserInfoArray array so I can loop through all users once all user info has been gathered?
According to the description of your problem, your concern is the "union" in "Compose" action creates a new array which contains both of the collections(array) but why not append the array from http request to the array "AllUserInfoArray". But why not create an action after the "Compose" to set the variable "AllUserInfoArray" with output value of the "Compose". And then we can do the union again to modify "AllUserInfoArray" in the next loop.

MS Access 2003 - Auto fill form-field based on previous form field.

I am currently attempting to design a database that requires a number of users inputting data via forms.
one of these tables is a 'user' table. Amongst the information in the table is
userid (int),
username (text),
first name (text),
last name (text)
In the even that I'm filling out a form and supply the the username in the username field is it possible if the username already exists to pull the first name and last name from the user table and auto-populate those form fields? If so can you point me in the right direction please?
Directly via access functionality or via vba? If not possible in 2003 is this possible in 2007?
Ok now for the auto fill (yes i did find one example), This will fill the username after you filled the userid (Both should be comboboxes in this case called Usernamecombo and useridcombo)
First make the query with a SQL similar to this:
SELECT [User].username FROM User WHERE ((([User].userid) Like '*' & [Forms]![Yourform]![useridcombo] & '*'));
Lets call this query "qry_username".
Then go to designview of the form and to the properties of the useridcombo, in the event/afterupdate property you make a event procedure (VBA) :
Private Sub useridcombo_AfterUpdate()
[Forms]![yourform]![Usernamecombo].Value = DFirst("username", "qry_username")
Forms("yourform").[Usernamecombo].Requery 'this last line is optional
End sub
Other fields can be added to the VBA pretty simply(dont forget the query)
Private Sub useridcombo_AfterUpdate()
[Forms]![yourform]![Usernamecombo].Value = DFirst("username", "qry_username")
[Forms]![yourform]![Firstnamecombo].Value = DFirst("Firstname", "qry_username")
[Forms]![yourform]![Lastnamecombo].Value = DFirst("Lastname", "qry_username")
Forms("yourform").[Usernamecombo].Requery 'this last line is optional
End sub
I have a similar form and i use to make these field as Comboboxes.
Then set the property row source as a query.
Set the criteria Where like this
WHERE ((([Users].Username) Like '*' & [Forms]![YourForm]![Username] & '*'));
This will allow the user to choose the name as fast as possible
But it will not fill it automatically because my users can have the same username as others.

Account name must be unique

What I am looking to do is Make it the "Account" name field require a unique name.
Basically If one of my reps tries to create an account, and that account all ready exists it tells them no that account all ready exists.
Salesforce tells me this funicality is not build into sales force. Any help or dirrection would we wonderfull.
Make a new text field, call it Name__c. Mark it as unique, length... probably 80, same as Name field length.
Create new Workflow rule with condition ISNEW() || ISCHANGED(Name) || ISBLANK(Name__c) and the action should be a field update that simply has Name in the formula that determines new value.
Remember to activate the workflow and to fill in the newly created field because it will be blank for all your existing accounts!
Your call if you want to show the field on page layouts (it's quite "technical" so could be hidden). If you do - it's a good idea to make it readonly!
You can use this validation:
AND(CONTAINS(VLOOKUP( $ObjectType.Account.Fields.Name , $ObjectType.Account.Fields.Name, Name), Name), OR(ISNEW(), ISCHANGED(Name)) )
Salesforce offers duplication management for this purpose.
You just set up Matching Rules and Duplicate Rules for your Account object in Setup > Administration Setup > Data.com Administration > Duplicate Management.
Link: https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_prevention_map_of_tasks.htm&language=en_US
You could write a trigger to prevent duplicates. It'd be a "before insert" trigger that queried for existing accounts with the same name. If an Account name already exists, you'd call addError() on the new Account record, preventing the insert from continuing.
Have you searched the AppExchange for solutions? Might want to check out something like DupeCatcher
You could always make a custom field to contain the account name (something like "Business Name") and then ensure that's required and unique.
You'd need to do some basic Data Loader gymnastics to move your account names to the new field, and come up with a strategy for populating the existing Name field for accounts.
AND(VLOOKUP($ObjectType.Object_Name.Fields.Name, $ObjectType.Object_Name.Fields.Name, Name) = Name, OR(ISNEW(), ISCHANGED(Name)))

Arrays in Rails

After much googling and console testing I need some help with arrays in rails. In a method I do a search in the db for all rows matching a certain requirement and put them in a variable. Next I want to call each on that array and loop through it. My problem is that sometimes only one row is matched in the initial search and .each causes a nomethoderror.
I called class on both situations, where there are multiple rows and only one row. When there are multiple rows the variable I dump them into is of the class array. If there is only one row, it is the class of the model.
How can I have an each loop that won't break when there's only one instance of an object in my search? I could hack something together with lots of conditional code, but I feel like I'm not seeing something really simple here.
Thanks!
Requested Code Below
#user = User.new(params[:user])
if #user.save
#scan the invites dbtable and if user email is present, add the new uid to the table
#talentInvites = TalentInvitation.find_by_email(#user.email)
unless #talentInvites.nil?
#talentInvites.each do |tiv|
tiv.update_attribute(:user_id, #user.id)
end
end
....more code...
Use find_all_by_email, it will always return an array, even empty.
#user = User.new(params[:user])
if #user.save
#scan the invites dbtable and if user email is present, add the new uid to the table
#talentInvites = TalentInvitation.find_all_by_email(#user.email)
unless #talentInvites.empty?
#talentInvites.each do |tiv|
tiv.update_attribute(:user_id, #user.id)
end
end

Resources