Salesforce validations - salesforce

I'm new in Salesforce and I have to create an app for gym owner.
I made 3 objects, Member, Membership and Booking. My member object has data about gym members like address, date of birth etc.
Membership object has fields like MEMBER(lookup relationship with Member object), START DATA, END DATE and ACTIVE (checkbox to see if membership is stil active).
If membership of that member is still active, I have to prevent customer to create another membership for that member until current expires. How can I do that?

You could pull something like that with validation rule using VLOOKUP function (but then you need to always put something fairly unique & predictable in the Name field on your membership object). I'm thinking something along the lines of "if there exists membership 'Member #1234 active' then VLOOKUP should prevent creating another one like that".
A nasty user could then delete the membership, create new one and restore it from recycle bin :P
So I have better idea for you (still without using any code). Convert membership->member lookup to master-detail relation. This would let you create rollup summary fields, read up about them. Create a rollup field that would be COUNT() of active memberships for the guy. Make validation rule that throws tantrum when you insert new membership and the count is already > 0.

Related

Integrate Salesforce with Salesforce

I'm trying to get updated account records from a Salesforce organization pushTopic subscription and trying to add the changes in another Salesforce organization. If the second organization already has the particular account, it should be updated else it should be create as new account. For this scenario I need to check the second organization account with first organization account records. How can we do it since Salesforce doesn't have unique key other than record id?
Create a big object which will hold the first organization record Id with the equivalent new org record Id. Through this, you will get to know the records which are already inserted.
If the record Id is not present in the big object which means that you need to create a new record and create one more big object record with a new record Id and old record Id (for further checking).
You can use custom object as well but the optimal solution could be creating a big object.

Grab value from unrelated object where two fields match

I have two objects - "Account" and "Appointment". I'm trying to pull the value of the field "Status" from the "Appointment" object where "Account.Initial_Date" matches "Appointment.Date_Time". I initially tried making a new field in the "Account" object to return a text field and see if maybe it would return the first value:
Appointment__c.Status__c
Which results in the error:
"Field Appointment__c does not exist. Check spelling."
I was told that it's too difficult to link from "Appointment" to "Account" because there can be multiple appointments per account, which is why I'm trying to link based on the date fields. My next attempt was using VLOOKUP, but I read that this only works between custom objects, and I think I'm working with standard objects here... what kind of solution should I be looking for?
Adding the tag apex here in case this can only be achieved via a script of some sort - if that's the case, I'll make attempt via that.
I was told that it's too difficult to link from "Appointment" to "Account" because there can be multiple appointments per account
This is incorrect. That relationship appears to be exactly the same as that between Contact and Account - one Contact, many Accounts. It's a very common relationship pattern in Salesforce.
If an Appointment is logically related to an Account, it should have a relationship field referencing the Account object to which it is related.
However, having a one-to-many relationship does not mean you can trivially represent specific data points from the many side to the one side. The native tool to do so is the Roll-Up Summary Field, but it does not apply to your use case.
There's really three ways to implement your objective, which is essentially implementing a variant of a roll-up summary. VLOOKUP(), which works only in Validation Rules, does not apply here.
Write two Apex triggers (one on Account and one on Appointment) to react to all changes that would influence what value should appear in the Account__c.Status__c field.
Write equivalent Process and Flow declarative automation, which cannot get 100% of the way there because Process Builder and Flow cannot react to delete events.
Use the free and open source Declarative Lookup Rollup Summaries application to define a roll-up summary. DLRS can populate a field from the child object (Appointment) to the parent (Account) based on a sorting by another field (Date_Time__c).

How to do a nested query in SOQL? Salesforce

I have 2 custom objects in Salesforce.com
One is PersonAccount and one is Accounts.
Within the default "Account" object I have a field called user_id
PersonAccount acts as a junction table to link "Account" to Accounts
PersonAccount does a lookup in Person for user_id Lookup(Account)
How can I build a query to check something in Account to find all the matching items in Accounts?
Currently, Salesforce only permits a single level of nested queries. It cane be done like the following:
[SELECT ID, Name, Field1 from Object__c WHERE Id IN ( SELECT Id FROM Object2__c WHERE Field2 = 'SomeValue')]
However, with the junction object you don't actually need to use a nested query.
Unfortunately, your description isn't clear enough to understand your specific object set-up, so I am going to make some assumptions.
You have three objects, Accounts__c (your custom Accounts Objct), PersonAccount__c (your junction object), and Account (the default Account objects).
The PersonAccount__c object contains two lookup fields (for a true Junction, they should be Master-Detail). The first is to Accounts__c (we will call that lup_cust_accounts__c). The second is to Account (we will call that lup_account__c). [As an aside it is a really bad idea to have an Accounts and Account object. It is going to screw you up because Salesforce will automatically pluralize words and then you will be confused as to which is which.]
Salesforce allows dot relationship lookups in SOQL queries. So if you want to get the ID and Name from custom Accounts Objects when the associated Account object's Name is like "Test", you could do the following:
[SELECT lup_cust_accounts__r.Id, lup_cust_accounts__r.Name FROM PersonAccount__c WHERE lup_account__r.Name LIKE 'Test%'];
Notice the double underscore r instead of double underscore c? That is how you indicate a reference (lookup) rather than the specific field value.

How to get all shared accounts list in Salesforce?

I am very new to sales-force, In sales-force the owner can manually share the account with another user. I need a list that shows the manual sharing of all accounts.Would it be possible by using apex class or trigger or any other way?
Examine the AccountShare table, especially something like that:
SELECT Id, AccountAccessLevel, AccountId, UserOrGroupId
FROM AccountShare
WHERE RowCause IN ('Manual', 'TerritoryManual')
LIMIT 100
You can read up more about the values here, there's also a nice example of Apex used to share records based on rules that can't be expressed in normal sharing rules.
UserOrGroupId is tricky one -> if it will start with "005" that's an userid and your search is done. Otherwise you'll have to look it up in Group table. This will contain real groups (called "Public Groups" in the setup) as well as system-generated groups that correspond to Queues, Roles, Roles+Subordinates, Territories etc. So you might have a bit of recursive world of pain here trying to learn who actually is in this group.
I admit I was lucky in the past, level of nesting I saw in the Group table was never > 2 levels deep.

Create multiselect lookup in salesforce using apex

I want to create a multi-select Contact Lookup.
What i want :
When user clicks on a lookup then he should be able to select multiple contacts from that.
What i have done:
I have created an object and a field inside that object using both
"Lookup" and
"MasterDetail Relationship" and
"Junction Object"
When i try to use this Field for any input text/Field then it always provides an option to select only one value from lookup but i want to have an option to select multiple.
Even in the Junction object i have created 2 master-detail relationships still lookup allows only one value to be selected.Moreover it makes the field mandatory which i don't want.
Links that i followed:
http://success.salesforce.com/questionDetail?qId=a1X30000000Hl5dEAC
https://ap1.salesforce.com/help/doc/user_ed.jsp?loc=help&section=help&hash=topic-title&target=relationships_manytomany.htm
Can anybody suggest me how to do this.
Its same as we use Email CC/BCC under Send Email option for any Lead.
Even you use a junction object a lookup is just that, it references (looks up to) one other record: when you create a record on the junction object you still have to set each lookup individually and you're still creating only one record.
Master Detail relationships are essentially lookups on steroids, one object becomes the child of the other and will be deleted if the parent object is deleted, they're not going to provide an interface to lookup to many records at once.
If you're not a developer then your best bet is to either just create on junction object record at a time, or look into using dataloader. You could prepare your data in Excel or similar and then upload all the records into Salesforce in one go.
If you are a developer, or have developers at your disposal, then what we've done in the past is create a Visualforce page to do the job. So if, for example, you wanted to link a bunch of contacts up to an Account, we'd have a single account lookup field on the page, then some search fields relating to fields on the contact. Using a SOQL query you can then find all contacts matching the search parameters and display them in a list, where you may want to provide checkboxes to allow the user to select the contacts they want. Then it's just a case of looping through the selected contacts, setting their Account field to be the chosen account.
There are areas in Salesforce (such as the send Email functionality you mentioned) where it's clear to see that bespoke work has been done to fulfil a specific task — another instance of what you want is in the area where you can manage campaign members. This is the model I've copied in the past when implementing a Visualforce page as described.
Good luck!
For adding multiple junction objects at one time, the only solution we have found is a custom Visualforce page, as described by LaceySnr.
For a slightly different problem, where we need to assign many of object B to object A, We have trained our users to do this with a view on object B. We are assigning Billing Accounts (B) to Payment Offices (A). The view on Billing Account has check boxes on the left side. The user checks the Billing Accounts to be assigned, then double-clicks on the Payment Office field on any of the checked rows. A pop-up asks if you want to update only the single row or all checked rows. By selecting 'all checked rows', the update is done to all of them.
The view is created by the user, who enters the selection criteria (name, address, state, etc.). All user-created views are visible only to them.

Resources