Mvc Getting List Based on Rights - sql-server

So I have a table named tblUsers (ID,Username, password) where all user details are available and another table tbluserregionrights (ID,UserID,Regioncode) [ID PK , UserID FK tblUsers.ID & Regioncode FK tblregions.id], where user permissions are mentioned based on RegionCode. Another table named tblSites where all sites details are mentioned (SiteID, SiteName , Regioncode)
In my controller, i am getting a list from tblsites and hardcoded the RegionCode
List<tblSite> list3 = db.tblSites.Where(x => x.Active == true && **x.RegionCode ==1**).ToList();
ViewBag.sitec = new SelectList(list3, "SiteCode", "SiteID");
Instead of the hardcoded region, I need to get the site list dropdown based on the user assigned Regioncode in tbluserregionrights.

Related

Who to access field of nested relationship in soql where first relation is simple and next is polymorphic

I want to access custom field of Owner (User) fields where Owner is polymorphic relation it can be either User or Group.
I tried following query:
SELECT Id, Email, Name, Lead.TYPEOF Owner WHEN User THEN custom__c FROM CampaignMember WHERE CampaignId='xxxxxx'
Relationship stack is:
Lead > Owner (User,Group) > custom__c.
SELECT field1__c,
field2__c
FROM Lead
WHERE Owner.type = 'User'
AND CampaignID = 'xxxxxx'
EDIT:
Leaving my original answer in case it helps someone else.
I believe what you are looking for is this:
SELECT Id, Email, Name,
TYPEOF Lead.Owner
WHEN User THEN custom__c
END
FROM CampaignMember
WHERE Field != 'value'

Salesforce Query to Check the Existing Contact with Account id

I already have contact by using Accountid, so before creating the new contact I want to verify the existing contact with email and id, can you please help me with this salesforce query to check if the Contact already exist with Contact Email and AccountId?
I already tried using the below query but it was throwing exceptions:
SELECT Id, Name , email
FROM Contact
WHERE email='XXX#Email.com'
AND Id IN (SELECT ContactId
FROM AccountContactRelation
WHERE AccountId = 'XXXX')
Got the below error:
INVALID_TYPE: and Id IN (SELECT ContactId FROM AccountContactRelation
WHERE AccountId
^
ERROR at Row:1:Column:121 sObject type 'AccountContactRelation' is not
supported. If you are attempting to use a custom object, be sure to
append the '__c' after the entity name. Please reference your WSDL or
the describe call for the appropriate names.
Can you please share the correct Salesforce Query ?
The below Query worked like charm :)
SELECT Id, Name , email
FROM Contact
WHERE email='XXXXXXXXX#XXX.com'
AND AccountId='YYYYY#yyy.com'

Adding Customer Specific Record in Master Table

I have the following table designed already.
Now the new requirement is i need to show some Category for all the
tenants. Also each tenant should able to add their new Category into
the Master Category. So they can see all master + their specific
categories
Tenant Table
TenantId
Name
Group Table
GroupId
Name
Category Table
CategoryId
Name
TenantXCategory
TenantId
CategoryId
What changes i can do in above tables to achieve it? I tried this below
Modified Category table to below.
Category Table
CategoryId
Name
TenantId NULL // This indicates tenant specific category
Add a unique key for TenantId and Name
Then queried
SELECT *
FROM Category where TenantId = 1
UNION
SELECT *
FROM Category where TenantId IS NULL
But the problem is if two tenant only want to see a particular
Category, I need to add a new row with other TenantId in Category
table. This mean i am creating duplicate entry in a lookup table. Any
suggestion to achieve the new requirement?
So, a tenant A may see:
(1) master categories,
(2) categories that belong to tenant A,
(3) categories which belong to another tenant and which the other tenant explicitly allowed tenant A to see
Your present schema seems to satisfy the requirements. In particular, the first two rules can indeed be implemented using a nullable TenantId column in the Category table, where NULL would stand for a master category and a non-NULL value would reference the creator/owner of the category and thus signify a tenant-specific category. (I might rename the column to something like OwnerTenantId for better clarity, but that might be just me.)
To retrieve only master categories or those that belong to the specified tenant, you could use the query you've posted in your question or this one (which will probably yield an identical execution plan to your query's):
SELECT
CategoryId,
CategoryName,
CASE
WHEN TenantId = #TenantId THEN 'Mine'
WHEN TenantId IS NULL THEN 'Master'
END AS Ownership
FROM Category
WHERE TenantId = #TenantId
OR TenantId IS NULL
;
To implement the third rule, you could use your TenantXCategory table to store categories available to tenants in addition to those that are accessible using the first two rules. That is, if tenant M permits tenant N to see some of tenant M's categories, the categories' Ids would be inserted into TenantXCategory along with tenant N's Id.
So, to query all categories available to a particular tenant, you could do something along the lines of the following:
SELECT
c.CategoryId,
c.CategoryName,
CASE
WHEN c.TenantId = #TenantId THEN 'Mine'
WHEN c.TenantId IS NULL THEN 'Master'
WHEN tc.CategoryId IS NOT NULL THEN 'Someone else''s'
END AS Ownership
FROM Category c
LEFT JOIN TenantXCategory tc
ON tc.CategoryId = c.CategoryId AND tc.TenandId = #TenantId
WHERE c.TenantId = #TenantId
OR c.TenantId IS NULL
OR tc.CategoryId IS NOT NULL
;
If you're going to be adding additional categories, you should probably have an identifier to distinguish between master and user-defined categories.
CREATE TABLE CategoryType (
[CategoryTypeID] int primary key identity,
[Description]
)
CREATE TABLE Category (
[CategoryID] int primary key identity,
[CategoryName] nvarchar(max)
[CategoryTypeID] int
)
/* CategoryTypes
1 Master
2 User-defined */
Show categories for a specific tenant
SELECT *
FROM TenantXCategory txc
JOIN Tenant t
ON t.TenantID = txc.TenantID
JOIN Category c
ON c.CategoryID = txc.CateoryID
WHERE t.TenantName = N'user1909604'
-- AND c.CategoryTypeID = 1 -- Only show master categories
-- AND c.CategoryTypeID = 2 -- Only show user-defined categories
-- AND c.CategoryID in (1, 2) -- Only show specific categories
To add a category for a user, store the category in your cross reference table. If the category doesn't exist, you'll have to add it to the category table first.
INSERT TenantXCategory (TenantID, CategoryID)
SELECT #TenantID, #CategoryID
--
Unless you're storing who created the category (for auditing purposes), this should work. If not, I misunderstood your question and you should clarify what you're trying to do.

SalesForce: Query objects a user has read permission for

How can I Query all Accounts a given user has read permission for?
I tried the following but, returned the error "semi join sub selects can only query id fields, cannot use: 'RecordId'"
User u = new User();
Account[] account = [SELECT Name FROM Account a
WHERE Id IN
(
SELECT RecordId
FROM UserRecordAccess
WHERE RecordId = :a.Id
AND UserId = :u
AND HasReadAccess = true
)
];
The code is being executed as part of a scheduled batch job run as system so use of "with sharing" is not applicable.
Thanks
The salesforce documentation says that RecordID is a picklist field which seems odd. If it is a picklist field that might explain why it does not work in a subquery.
You could try building a set of RecordIDs first and then using this to filter the Account query.
Set<ID> sRecordIDs = [SELECT RecordID FROM UserRecordAccess WHERE UserId = :u AND HasReadAccess = True];
Account[] accs =[SELECT ID,Name FROM Account WHERE Id in :sRecordIDs];
This might fall over governor limits if the number of records in UserRecordAccess is high.
If you are only looking to do this for Account you could try this:
User u = new User();
list<Account> accs = [select Id, Name from Account where Id in (select AccountId from AccountShare where UserOrGroupId = :u.Id) or OwnerId = :u.Id];
That should give you all of the account records that the user has access to or owns. In the general case each sObject has its own 'Share' sObject that represents visibility against it, unless it is degenerate in some way (i.e. it is the detail in a master-detail).

Automatically set one to many id when adding items

I have two tables with the following (simplified) structure
person_availabilities (id, person_id)
persons (id)
that are related (a person can have many availabilities)
I have used the bake feature to create the current pages.
What I wish to do is go to a person's profile and click on "New Person Availability". The page will then go to the add person availabilities page and allow the user to input the data there.
The user should not have to find the person in a drop down list, which is the default way to select the identity of the person whose availability I'm setting.
When you go to a person's profile and then navigate to add page, try to pass the id of that person through url to that add page and then try following:
URL may be:
http://example.com/person_availabilities/add/2 // here 2 is the person id
In person_availabilities controller's add function try something like this:
function add($person_id = null) { // $person_id is the passed id. e.g $person_id = 2
$this->set('person_id', $person_id); // set the id to view
}
In view file the select box should automatically show the person whose availability you're using if the select field name is person_id i.e.
$this->Form->input('person_id');
If not then try selected property to that select box:
$this->Form->input('FIELD_NAME', array('selected' => $person_id));

Categories

Resources