Grab value from unrelated object where two fields match - salesforce

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).

Related

Salesforce retrieve object id using custom field on another object

We're implementing a coupon program. The coupons are unique codes and are related to an existing customer and stored in a custom field on the Account.
When a Lead is created due to being referred using one of the unique coupons, the unique coupon is saved in a custom field on the Lead. I need to access the associated Account Id of the unique coupon.
I could do this by creating a trigger on lead insert and then query accounts looking for unique coupon. My concern with this approach is having a trigger and query on every lead created; seems this would not be good pratice - using so much resources for a rare situation
Is there another (better) approach; lookup?
Thanks
If you build the trigger correctly there should be no concern about resources. But it really depends on what you're trying to do with the Account data, I don't know the architecture though so you will need to give more details

Salesforce Lookup Object Relationships

I have a number of objects that contains references to other objects in Salesforce. An example is that a Shipping is the parent object related to ShippingItem by the shippingId. In order to save ShippingItems I need to get the ShippingId of the Shipping object. Also, the ShippingItems are related to ShippingMethod object which contains the shipping methods, UPS, FEDEX etc. This is a picklist type that allows users to pick the shipping method. In order to create/save the ShippingItem. I need to obtain all the related fields, i.e. ShippingMethod i.e UPS, ShippingId to which it is related and other similar information. How can I obtain these fields in order to save the ShippingItems? When I inspect the Objects, I am unable to determine the fields to query in order to obtain the related information.
Please help
If I understood this correctly, ShippingItem has a lookup to Shipping and a lookup to ShippingMethod. Are you providing a UI where people create ShippingItems? If yes then ShippingID and ShippingMethodID should be populated on the shipping record when you get the record back in controller.
If your question is how to provide a list of ShippingMethod records on the UI for users to select, you can query the object records separately and create a picklist for the user to choose.
Adding a little more information or code snippet will help you get better answers.

Designing a database with similar, but different Models

I have a system whereby you can create documents. You select the document type to create and a form is displayed. Data is then added to the form, and the document can be generated. In Laravel things are done via Models. I am creating a new Model for each document but I don't think this is the best way. An example of my database :
So at the heart of it are projects. I create a new project; I can now create documents for this project. When I select project brief from a select box, a form is displayed whereby I can input :
Project roles
Project Data
Deliverables
Budget
It's three text fields and a standard input field. If I select reporting doc from the select menu, I have to input the data for this document (which is a couple of normal inputs, a couple of text fields, and a date). Although they are both documents, they expect different data (which is why I have created a Model for each document).
The problems: As seen in the diagram, I want to allow supporting documents to be uploaded alongside a document which is generated. I have a doc_upload table for this. So a document can have one or more doc_uploads.
Going back to the MVC structure, in my DocUpload model I can't say that DocUpload belongs to both ProjectBriefDoc and ProjectReportingDoc because it can only belong to one Model. So not only am I going to create a new model for every single document, I will have to create a new Upload model for each document as well. As more documents are added, I can see this becoming a nightmare to manage.
I am after a more generic Model which can handle different types of documents. My question relates to the different types of data I need to capture for each document, and how I can fit this into my design.
I have a design that can work, but I think it is a bad idea. I am looking for advice to improve this design, taking into account that each document requires different input, and each document will need to allow for file uploads.
You don't need to have a table/Model for each document type you'll create.
A more flexible approach would be to have a project_documents table, where you'll have a project_id and some data related to it, and then a doc_uploads related to the project_documents table.
This way a project can have as many documents your business will ever need and each document can have as many files as it needs.
You could try something like that:
If you still want to keep both tables, your doc_upload table in your example can have two foreign keys and two belongsTo() Laravel Model declarations without conflicts (it's not a marriage, it's an open relationship).
Or you could use Polymorphic Relations to do the same thing, but it's an anti-pattern of Database Design (because it'll not ensure data integrity on the database level).
For a good reference about Database Design, google for "Bill Karwin" and "SQL Antipatterns".
This guy has a very good Slideshare presentation and a book written about this topic - he used to be an active SO user as well.
ok.
I have a suggestion..you don't have to have such a tight coupling on the doc_upload references. You can treat this actually as a stand alone table in your model that is not pegged to a single entity.. You can still use the ORM to CRUD your way through and manage this table..
What I would do is keep the doc_upload table and use it for all up_load references for all documents no matter what table model the document resides in and have the following fields in the doc_upload table
documenttype (which can be the object name the target document object)
documentid_fk (this is now the generic key to a single row in the appropriate document type table(s)
So given a document in a given table.. (you can derive the documenttype based on the model object) and you know the id of the document itself because you just pulled it from the db context.. should be able to pull all related documents in the doc_upload table that match those two values.
You may be able to use reflection in your model to know what Entity (doc type ) you are in.. and the key is just the key.. so you should be able.
You will still have to create a new model Entity for each flavor of project document you wish to have.. but that may not be too difficult if the rate of change is small..
You should be able to write a minimum amount of code to e pull all related uploaded documents into your app..
You may use inheritance by zero-or-one relation in data model design.
IMO having an abstract entity(table) called project-document containing shared properties of all documents, will serve you.
project-brief and project-report and other types of documents will be children of project-document table, having a zero-or-one relation. primary key of project-document will be foreign key and primary key of the children.
Now having one-to-many relation between project-document and doc-upload will solve the problem.
I also suggest adding a unique constraint {project_id, doc_type} inside project-document for cardinal check (if necessary)
As other answers are sort of alluding to, you probably don't want to have a different Model for different documents, but rather a single Model for "document" with different views on it for your different processes. Laravel seems to have a good "templating" system for implementing views:
http://laravel.com/docs/5.1/blade
http://daylerees.com/codebright-blade/

How to interpret this database assignment?

I am working on an assignment concerning a simple database.
The instructions are given as:
Create a small database for "products",
give each product 3 or so attributes in a related table,
and then provide URL's for updating all aspects of those objects.
Create
Read
Update
Delete
List
Search
(For Products:)
Add Attribute
Remove Attribute
I am confused as to whether the Attributes are supposed to fixed categories that are the same for all products, and that deleting the attribute simply clears the cell, OR if it means that attributes are intended to be dynamically added, and that each product should be capable of having different categories of attributes.
In the second case, deleting an attribute would get rid of the entire category.
Your assignment is unclear and you should ask your instructor.
The straighforward interpretation of
Create a small database for "products", give each product 3 or so
attributes in a related table, and then provide URL's for updating all
aspects of those objects.
is that there are some "products" (ie product types rather than individual objects) each of which has its own current set of attributes and its own table that records the current "objects" of that type and the attribute values of each one.
You more or less have the choice of recording a table for each product type P with:
(object,A) rows like "object object is a P product whose attribute A is A and attribute ..." with Add Attribute implemented by DDL
(object,attribute,value) rows like "object object is a P product whose attribute attribute is value" with Add Attribute implemented by DML
(The latter approach is called "EAV" and leads to obscure queries and foregoing of most of the supportive functionality of a DBMS. I only mention it because given the vaguenss of the question maybe it's wanted as a solution nevertheless.)
Re "attributes are intended to be dynamically added": Either of these allows each database state to have its own set of attributes for a product. Remember that there is both DDL and DML. Also, you seem to allow that "attribute" might mean "attribute value" but the former choice seems much more likely.
Re "deleting an attribute would get rid of the entire category": The assignment asks you to record "all aspects" of objects that are given product types, not of products. So it doesn't matter whether you ever "get rid of the entire category" (of a product).
(Clearly give and justify your interpretation of the assignment. If there is more than one interpretation that you think likely, do that for each.)

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