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
SFDC New learnerSFDC New learner 

how to restrict the Salesforce Platform User to update record using trigger

Hi Everyone,

I am trying to reproduce the scenario as below link. Created a user as Salesforce Platform licence, trigger and when I logged in as Salesforce Platform User licence and created contact and updated not able to get the error.

https://www.sundoginteractive.com/blog/triggers-updating-restricted-records

Can anyone suggest me on this?

Thanks,
Sirisha
@ M  Coder@ M Coder
Hello sirisha, 
can you post your trigger code such that we can replicate it . 
br
Mcoder
SFDC New learnerSFDC New learner
Hi M Coder,

Below is the code which will update opportunity flag when user updates any field on Contact Object.

trigger CheckContact on Contact (after update) {
    
    List<ID> acctids = new List<ID>();
    List<Opportunity> oppflag = new List<Opportunity>();
    
    for(Contact con:Trigger.New){
        acctids.add(con.accountId);
    }
    
    if(acctids.size()>0){
        List<Opportunity> opplist = [select id,accountid from Opportunity where Accountid In:acctids];
        for(Opportunity opp: opplist){
            opp.checkflag=true;
            oppflag .add(opp);
            
        }
        update oppflag ;
    }

}

Thanks,
Sirisha
@ M  Coder@ M Coder
Hello Sirisha , 
point 1: first checkflag is not a standard filed , it might be a custom one
point2: all ways include fieilds in soql if we use it like below

=============================================================
Using Maps : Trigger always should be buikified
trigger UpdateOppFTPRecipients on Contact (after update) {
    List<ID> acctids = new List<ID>();
    List<Opportunity> opplist = new List<Opportunity>();
    for(Contact con:Trigger.New){
        acctids.add(con.accountId);
    }
    Map<Id,Opportunity> OppMap = new Map<Id,Opportunity>([select id,accountid,FTP_Recipients__c from Opportunity where Accountid In:acctids]);
    if(OppMap.size() > 0 ) 
     {
      for(Opportunity opp: OppMap.values())
     {
        if(OppMap.get(opp.id).Accountid!=null)
         {
         opp.FTP_Recipients__c = true;
         opplist.add(opp);
         }
      } 
    }      
    update  opplist;
 }
==========================================================================
Using list : 
trigger UpdateOppFTPRecipients on Contact (after update) {
    List<ID> acctids = new List<ID>();
    List<Opportunity> oppflag = new List<Opportunity>();
    for(Contact con:Trigger.New){
        acctids.add(con.accountId);
    }
    if(acctids.size()>0){
        List<Opportunity> opplist = [select id,accountid,FTP_Recipients__c from Opportunity where Accountid In:acctids];
        for(Opportunity opp: opplist){
            opp.FTP_Recipients__c=true;
            oppflag.add(opp);
        }
        update oppflag ;
    }
}

Please mark it as a better solution.
please reach out to me at salesforceseekar@gmail.com if you need help , Happy learning.

br
Mcoder


 
SFDC New learnerSFDC New learner
Hi Mcoder, Thanks for the tips. I appreciate it. But my question is how can I replicate the error. As per my understanding from the link: 1. I have created a contact, Opportunity as an Sys Admin. 2. Logged In as Salesforce Platform User. 3. Opportunity has Sharing settings as OWD as Private and assigned to only one user as Sys Admin. 4. Now, when I create a contact and update the flag on contact object it should get all the opportunities and update the flag on Opportunity. 5. Now when I login as Salesforce Platform User and update the contact flag or any field should throw the "Insufficient privilleges error" (Also, Salesforce Platform User is not having Opportunities). Am I missing something? Please advise. Thanks, Sirisha