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
KuenKuen 

Simple Trigger Help

Hey all,

 

I have a question,

 

When before I tried to change the before insert/before update to after insert and after update, it would give me a readonly error. Am I doing something wrong to get it working? As a work around (kind of) I changed it to "before insert, before update" and it appears to work; however, whenever the account's custom field is set to something, it still changes the field to the name regardless of whether or not it is set.

 

Any hints or tips or suggestions to know what exactly is wrong here?

 

Thanks

 

Kuen

 

 

trigger zendeskAutoFill on Account (before insert, before update) {
for (Account a: trigger.new) {
             if (a.Zendesk__Zendesk_Organization__c != '') {
                    a.Zendesk__Zendesk_Organization__c = a.Name;
              }
         }

 

KuenKuen

Nevermind, I found a way to get around it and that's by using SQL statements and changing the database depending on whether or not it's empty in the database instead of the retrieved object.

nickname142857nickname142857

You should do it before insert or update because you want to change the field.

 

If you want to set the value only if it is not set, then should do:

             if (a.Zendesk__Zendesk_Organization__c == null || a.Zendesk__Zendesk_Organization__c == '') {
                    a.Zendesk__Zendesk_Organization__c = a.Name;
              }
KuenKuen

Thanks for the idea, but which way should be better and why, I am new to salesforce and wondering why one way would be better than another?

 

The way I have it working, which is basically using sql to select the distinct accounts involved, and then finding it null in the database and then changing it then. Or your way, which I just tested and works as expected?

 

Why should it be before an update/insert when I'm changing a field rather than after?

 

Thanks in advance,

 

Kuen