function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
brozinickrbrozinickr 

List has more than 1 row for assignment sobject error

Hello, I have this trigger that stops people from deleting an opportunity of a certain type.  Originally only system admins were allowed to delete these opportunities, but now we need another profile to be able to do this as well.

 

I am getting a list has 1 or more rows for assignments error on the line that is underlined in red.  I am not sure how to make this  trigger accommodate for both profiles though.

 

trigger CannotDeleteAcctMgmtRenewal on Opportunity (before insert, before delete) {

    Profile p = [SELECT Id from Profile where Name IN ('System Administrator', 'Sales Op Admin')];
        if(System.Trigger.IsDelete)
        {       
            for (Opportunity opps : trigger.old)
                if (UserInfo.getProfileId() != p.Id && (Opps.Type == 'Acct. Mgmt. Renewal'||Opps.Type == 'Acct. Mgmt. Supersede'||Opps.Type == 'Acct. Mgmt. Late Renewal'||Opps.Type == 'Acct. Mgmt. TER'||Opps.Type == 'New Ad Sales'))
                {
                 opps.addError('You do not have the permissions delete opportunities.  Please contact a System Administrator or your designated Ad Operations Specialist.');
                }
         
        }
        
        if(System.Trigger.IsInsert)
        {
            for (Opportunity Opps : trigger.new)
                if (UserInfo.getProfileId() != p.Id && (Opps.Type == 'Acct. Mgmt. Renewal'||Opps.Type == 'Acct. Mgmt. Supersede'||Opps.Type == 'Acct. Mgmt. Late Renewal'||Opps.Type == 'Acct. Mgmt. TER'))
                {
                Opps.addError('Error:  You do not have the permissions create Acct. Mgmt. Renewal opportunities.  Please contact a System Administrator or your designated Ad Operations Specialist.');
                }
        }
       
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You're trying to find two records (and succeeding), but you didn't store it in a list. Instead, you probably want this:

 

Set<Id> profileIds = new Map<Id, Profile>([SELECT id FROM Profile WHERE Name IN ('System Administrator','Sales Op Admin')]).keySet();

...

if(!profileIds.contains(UserInfo.getprofileid()) ...

 

All Answers

sfdcfoxsfdcfox

You're trying to find two records (and succeeding), but you didn't store it in a list. Instead, you probably want this:

 

Set<Id> profileIds = new Map<Id, Profile>([SELECT id FROM Profile WHERE Name IN ('System Administrator','Sales Op Admin')]).keySet();

...

if(!profileIds.contains(UserInfo.getprofileid()) ...

 

This was selected as the best answer
brozinickrbrozinickr

Thank you for your help, sfdcfox. :)