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
UshankUshank 

Field value is coming as null in apex trigger ( trigger.new)

Hello,

 

I have below apex trigger. AccountId field is coming as null when I am trying to login as well which has read-only access to the field account name which is lookup field to Account standard object.

 

This value is coming file when I am trying to raise a case with profile having "MODIFY ALL DATA" access but with normal user having read-only access to the field in coming as null.

 

Triggers run on system mode why there is discrepancy with login. Please help how to get this value. ( for user where it is coming as null, this user can't edit account name field on page).

 

Please suggest how to check the values before insert, update.

 

 

Trigger

CaseContactUpdate onCase (beforeinsert, beforeupdate) {

 

RecordType rec = [Select r.Name, r.Id FromRecordType r where r.Name='Administrative Change'];

 

for (Case newCase : Trigger.new)

{

try

{

if(newCase.RecordTypeId == rec.Id) {

System.debug(

'ushank contactId'+newCase.ContactId);

System.debug(

'ushank accountId'+newCase.AccountId);

 

if(newCase.ContactId == null){

 

Contact c1 = [Select c.Id FromContact c where c.AccountId = :newCase.AccountId and c.Primary__c = true];

newCase.ContactId = c1.Id ;

}

if(newCase.ContactId != null && newCase.AccountId != null){

 

if ([Selectcount() FromContact c where c.AccountId = :newCase.AccountId andc.Id = :newCase.ContactId] == 0){

newCase.addError(

'Selected contact does not belong to customer, please select correct contact');

}

}

}

}

catch(Exception e)

{

newCase.addError(

'Not able to save record :::'+e);

System.debug(

'Exception : '+ e);

}

}

 

}

 

Regards

BryanHartBryanHart

I haven't tested this, but I believe that if your trigger uses "With Sharing", it'll run as the user who triggered the database operation