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
ch ranjeethch ranjeeth 

I need the trigger to be run in user mode. I have created a class with sharing and in trigger created a instance but its not respecting the user permissions....how to slove this?

global with sharing class recordcreation
{
  global void method1()
  {
 
    oldmapnewmap__c o=new oldmapnewmap__c();
    o.name='ranjith';
    insert o;
  }
}
Trigger:

trigger recordcreation on Account (before insert)
{
  recordcreation rc=new recordcreation();
   rc.method1();
}

The user dont have permissions on oldmapnewmap__c object but its inserting records... OWD is Private... Need help on this?
Best Answer chosen by ch ranjeeth
Arunkumar RArunkumar R
Hi Ranjeeth,

As per Bob suggesstion , you need to alter your code like below,

global with sharing class recordcreation
{
  global void method1()
  {
    oldmapnewmap__c o=new oldmapnewmap__c();
    o.name='ranjith';
    insert o;
}
  }
}

Trigger:

trigger recordcreation on Account (before insert)
{
   if (Schema.sObjectType.oldmapnewmap__c.isCreateable()) 
   {
      recordcreation rc=new recordcreation();
      rc.method1();
   }
}

Please note some the below suggesstion,

1. With sharing keyword only for Record Sharing not it will act as User mode.

2. If you are using the below class in webservice make use of global, else make it public.

Thanks.

All Answers

bob_buzzardbob_buzzard
You have to manage this yourself I'm afraid - Apex runs in system mode and doesn't respect the context users permissions (although it does respect most license limitations).  How to approach this is documented at:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_perms_enforcing.htm
Arunkumar RArunkumar R
Hi Ranjeeth,

As per Bob suggesstion , you need to alter your code like below,

global with sharing class recordcreation
{
  global void method1()
  {
    oldmapnewmap__c o=new oldmapnewmap__c();
    o.name='ranjith';
    insert o;
}
  }
}

Trigger:

trigger recordcreation on Account (before insert)
{
   if (Schema.sObjectType.oldmapnewmap__c.isCreateable()) 
   {
      recordcreation rc=new recordcreation();
      rc.method1();
   }
}

Please note some the below suggesstion,

1. With sharing keyword only for Record Sharing not it will act as User mode.

2. If you are using the below class in webservice make use of global, else make it public.

Thanks.
This was selected as the best answer