Questions tagged [after-trigger]

After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records.

Filter by
Sorted by
Tagged with
6
votes
0answers
3k views

ContentDocumentLink Apex after insert trigger error

Edit: I've built an after insert trigger on ContentDocumentLink. On cropredy's direction, I've bulkified my code and I think improved it generally. It "works" in that it accomplishes my business ...
5
votes
1answer
1k views

Is it possible to write Trigger for Before & After DML?

I'm wondering if I can write the trigger on Case, that works before and after DML in one file? I have two triggers on Case - first with method working on records before insert, and second for records ...
4
votes
1answer
118 views

Why won't Apex IF statement work in Production Unit Testing after working in sandbox?

this is driving me NUTS and any thoughts are MUCH appreciated after looking at this for far too long. Use case: users in certain profiles need to be automatically added/removed from a particular ...
4
votes
1answer
59 views

Need help in optimizing the below helper class

Logic : For any new record inserts for 'Properties' object, check if its Account_Ref_Ext_ID__c equals to 'Account' object's Chris_Account_Number__c. If it matches, assign Account id to properties ...
3
votes
3answers
358 views

How can I know if it's the last execution of an After Trigger?

Imagine one object with 20 fields, field1 to field20. Some of these fields I have Process Builder in place, some of them I have Worflow Updates, and some of them are 'bare field' (i.e. PB and WF does ...
3
votes
1answer
125 views

trigger working as expected from UI but not from data loader/workbench

I have written one trigger.It is working as expected when i am updating owner from UI. but it is not working when i am updating from DataLoader. Requirement :- in Lead description - add Lead owners ...
3
votes
1answer
76 views

How do I avoid a Duplicate-value during update in Trigger?

I'm changing the Clients__c value in Mopping__c if the StartDate__c, Status__c or Opportunity__c have changed in the needed Rent__c. Structure: if Mopping 1 is related to Rent 1 which is related to ...
2
votes
1answer
2k views

APEX Lock and Unlock a Record

I wanted to lock and unlock records based on a check box. I know that you can do this stuff via a validation rule but I think that is difficult to maintain if adding fields. I wrote an apex trigger ...
2
votes
2answers
54 views

trigger to update field on Account record whenever a new Contact, opportunity record is inserted or deleted

Number fields on Account : "No of Contacts" Write trigger to populate/update field on Account record whenever a new Contact record is inserted or deleted. -- whenever i am deleting last contact ...
2
votes
2answers
2k views

How to avoid this “Too many query rows: 50001”?

I have an afterInsert trigger for finding duplicates leads and if duplicate lead is inserted take its values and put it in a custom object record and lastly delete this new duplicate Lead record. It ...
2
votes
2answers
4k views

How do reference a related object within a Salesforce Trigger?

How do reference a related object within a Salesforce Trigger? I am creating a record for a related object and need to assign a value to one of the fields in the new related object (relatedOject1) ...
2
votes
1answer
1k views

Trigger After Insert Error : Record is read-only

still learning the apex ropes I've created a trigger which should create a contact after a custom object (School_bookings__c) record is created. My code is as follows: trigger CreateSchoolContact on ...
2
votes
1answer
43 views

Failure setting lookups in triggers when upserting multiple records of different types very quickly

I have an integration app that polls records of different types from an internal database every few seconds and upserts them into Salesforce. I have two pollers that run independently from one another ...
2
votes
3answers
104 views

Comparing @future vs non-future and beforeUpdate vs afterupdate when using DMLOptions to perform LeadAssignment in an Apex Trigger

When using Apex Database.DMLOptions for invoking standard Lead Assignment Rules, is it sane to use beforeUpdate instead of afterupdate? And is it sane to do this in non-future apex vs (as opposed to) ...
2
votes
2answers
69 views

trigger to update Custom field on Account record whenever a new opportunity record (Whose Title contains 'CEO') is inserted or deleted

Trigger to update Custom field(Count_Of_Title_in_Opp__c) on Account record whenever a new opportunity record (Whose Title contains 'CEO') is inserted or deleted Trigger trigger OppSizeOnAccount on ...
2
votes
1answer
117 views

Insert and return in test data factory

Why do we sometimes add a new record to an object and then just return the variable and sometimes we insert it too. I am confused, can somebody clarify me on this?
2
votes
1answer
3k views

How can we find out the deleted record in After delete trigger?

I am writing a trigger to handle after insert,after update and after delete events. I am using trigger.old for after delete.But, how to differentiate the deleted record from the rest of the records of ...
2
votes
2answers
139 views

Record is Read Only on After Insert Method

I wrote a method that works when it is before insert in my trigger handler. A problem I encountered is that the after insert method that already existed failed to run. I cannot figure out why it does ...
2
votes
1answer
72 views

Logic written in AfterInsert does not finish when beforeUpdate is called

We are creating OpportunityLineItem through Apex, now in isAfterInsert we are creating a Test object record whose Unique_Key is same as OpportunityLineItem unique_key. Now attaching the 'Test' to ...
2
votes
1answer
214 views

Check whether an sObject is read-only

Question - How can I determine that an sObject is read-only (due to being part of Trigger.New in an after trigger or otherwise). Explaination - I've been making a method to try and generically get ...
2
votes
1answer
503 views

Delete/Prevent trigger on Feed Item

The initial idea was to block all chatter feed from external batch jobs which run nightly. We had a trigger do it. trigger PreventPost on FeedItem (before insert) { if(UserInfo.getProfileId() == '...
2
votes
1answer
154 views

Getting 101 SOQL error when updating bulk records through data loader

I have a trigger which updates the total amount of child record values into parent record fields. It is working fine when I do the single records insertion. I have 50000 child records which are ...
2
votes
0answers
46 views

Problem with trigger time executions

I have 2 triggers after insert wich call one apex class each one. One of them call an apex class for make a query to get an Id from the first trigger execution. I need this ID to match two differents ...
2
votes
1answer
37 views

How to Capture Approval Comments in Case Object Custom field called comments ? Below code i am getting an error trigger depth exceeded?

trigger CaseApprover1 on Case (after update) { List<ProcessInstance> instances = [SELECT Id,TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps) ...
2
votes
0answers
91 views

DescribeFieldResult.getReferenceTo returns only one type in a specific scenario

I am seeing some strange behavior where DescribeFieldResult.getReferenceTo returns only one type for the Lead.OwnerId field in some cases when invoked in a trigger context. The code below displays ...
2
votes
0answers
520 views

AgentWork trigger is not firing before or after insert events

I have created an after/before insert trigger defined on AgentWork object. When a case is created from Email to Case, the record is automatically routed to an available Agent from a queue by Omni-...
2
votes
2answers
440 views

After insert trigger not reading lookup values

I have object customObjectA__c which has a lookup fields - lookupfield1__c I have an after insert trigger which, at some point, checks if that field is populated. The whole issue is, it's reading ...
1
vote
2answers
828 views

Why do we need to update related records in an after trigger but not before?

Why do we need to write the logic to update a related record in an after trigger but not before? Update triggers(Before/After) will have both the record ID and the related record's ID so why can I not ...
1
vote
1answer
57 views

How to force future method to wait until current thread finish?

I have an apex code which is called from an after trigger. The apex code calls a future method and sometimes the future method runs after the execution of the current thread and sometimes does not. I ...
1
vote
2answers
65 views

How to convert this trigger to apex Class

I've written this trigger to Update Total Numbers of Contact in related Account Custom Field... All Code is written in Apex Trigger However, I've been told that it's best practice to split this into ...
1
vote
2answers
56 views

Best way to process the records in After Update triggers - Sets or Lists

This may be a very primitive question but am failing to choose the best way and am trying to follow the best practices. We have many Salesforce Apps implemented, as a result there are many record ...
1
vote
2answers
337 views

Pass Trigger.old to constructor

I am not able to pass Trigger.old to constructor, Trigger trigger Test on Student_Details__c (after insert, after update,after delete) { studentupdatehandler studobj = new ...
1
vote
1answer
62 views

Debugging Apex Triggers

I am attempting the Apex Superbadge trigger and have bugs in my code. In the past, I have only written Apex controllers attached to a LWC. So, when I open a page/use the LWC, I can then see my debug ...
1
vote
2answers
70 views

Trying to update Values in Standard Object from metaData

Trigger is updated to Before UPdateI am using Trigger isBefore In System.debug(opp.get(metaData.get(0).Opportunity_Field_Name__c) it is showing correct Values but not Updating in Opportunity Object ...
1
vote
1answer
89 views

How write tigger Insert & update other records same object with update query

I have this trigger where I insert a record. On the insert I want to check on birthdate and set a checkbox on true for the record with the oldest birthdate related to same Household/Huishouden__c. ...
1
vote
1answer
329 views

Unit test keeps failing an “Insert” Campaign Member Status Apex Trigger (works in UI)

I am trying to write a Unit Test that passes an Apex trigger for inserting 2 custom CampaignMemberStatus records. The trigger is performing as expected in the Sandbox UI as an end user, however it ...
1
vote
1answer
1k views

Execution of AfterInsert caused by: System.FinalException: Record is read-only

I am creating a cross-objects trigger that whenever a new opportunity is opened, the trigger goes to check whether the company has sold the same product to the customer before; if so, the type of the ...
1
vote
2answers
62 views

Creating/updating new object based on a logic in a trigger

I have two object , abc__c and xyz__c. xyz__c has a field named key__c which should store the id of abc__c object. abc__c has a picklist field status__c which can have different value "In-progress" , ...
1
vote
2answers
431 views

Cross-Object SOQL Query

I have a set of custom fields on my Accounts object that depict weather an Account is approved for business with our company. The Approved Date is added via an Approval Process, and Account Status is ...
1
vote
1answer
121 views

how to know Orderitem trigger is triggerred by order trigger

some fields on orderitem can only be changed when the order is changed, so I need orderitem trigger to know if it's triggered by order trigger. how can I do this? Thanks
1
vote
2answers
48 views

SOQL Query Exception Non Selective Query in after insert Trigger

I am loading contacts records and when my after insert triggers runs I am getting the following error: System.QueryException: Non-selective query against large object type (more than 200000 rows)...
1
vote
1answer
39 views

To achieve same functionality as that of account teams in custom object

I want to achieve the same functionality as that of account team in project(Custom object)  using trigger . For this I have made a new custom object as projectteam and added lookup of user as well as ...
1
vote
1answer
52 views

After Update Trigger and wrong values

I have a Trigger and Trigger Helper. Opportunity is a parent for CompanyOrder__c. One Opportunity can have many CompanyOrder__c records with uncrossed Start- and End Dates. CompanyOrder__c is a ...
1
vote
3answers
77 views

Limitation with callouts

I want to listen to the change in permissions of salesforce records. For that, I am thinking to fire a callout from an afterUpdate method of a trigger. Whenever a record is updated through any means, ...
1
vote
2answers
108 views

Hari Krishnan based Trigger Framework executes twice on After Update

I have a trigger framework based on the work by Hari Krishnan: An architecture framework to handle triggers on the Force.com platform, however I have functionality on After Update in wich a trigger ...
1
vote
1answer
132 views

System.TypeException: Invalid integer: (Lead-0002)

I have created a round robin assignment of leads to users. I am getting the following error Lead_Trigger: execution of AfterUpdate caused by: System.TypeException: Invalid integer: (Lead-0002) ...
1
vote
1answer
595 views

How to perform DML operation in after insert, after update trigger

I am new in developing apex. Here is my trigger. When I try to update an account record, I get the error: " Error:Apex trigger pfUpdate caused an unexpected exception, contact your ...
1
vote
1answer
47 views

Change lookup relation parent on convert

I may have my terminology off as far as who the parent in the relationship is. However what I am trying to accomplish is take a custom object email_campagin__c attached to a lead and switch it over to ...
1
vote
1answer
161 views

Incompatible key type object for Map<id,account>

I am trying to write apex code (and I'm learning how) to roll up data from the on the Account from an object with a lookup relationship to the object. Fields from Account: ID, DealCount__c Fields ...
1
vote
1answer
42 views

how to finding the duplicates in the map of lists?

I have a requirement.for that I create a trigger. on that trigger I am using the Opportunity -Sobject and List of OpportunityContactRoles comes under the opportunity. Now I form the map as ...

1 2 3 4 5