New answers tagged

1 vote

Validate records by date ranges in SOQL and apex en bulk

Finding overlapping dates is one of those things that you think should be easy, but is actually hard. I mean, sure, it's easy enough to tell if two date ranges overlap or not (just see if the start or ...
user avatar
  • 46.4k
1 vote

Create Apex trigger on an Object than execute a Platform event

You need to create a platform event record, not just reference the platform event type. Change the publish call something like: EventBus.publish(new TestAsset__e()); If your event doesn't contain any ...
user avatar
  • 26.6k
0 votes
Accepted

Bulkifying a trigger with a fiscal year/quarter specification

Even if we can ut first event query outide of for loop , you will still face issue in case of large data volumes if soql return more thn 50k records. with your current approach you will iterate ...
user avatar
2 votes
Accepted

Issue while inserting new Opportunity Product records on after insert trigger

When you add an OpportunityLineItem to an Opportunity, one of the most important fields to set is PricebookEntryId. The Product2Id field should be read-only, unless your API version is < v30.0, in ...
user avatar
  • 46.4k
0 votes
Accepted

Populating field automatically

Since the Related to (What) field on the Task object is a polymorphic field, first you need to check what kind of record the task record has been created for. and then you can assign the Campaign ...
user avatar
  • 61
0 votes
Accepted

Do triggers in salesforce have test class created by salesforce not by the developer?

There are two options for test class while deploying to prod. RunLocalTests: With this approach you will need to have org coverage minimum 75 during validation. Also for all triggers at least 1% code ...
user avatar
  • 1,028
0 votes

execute logic beforedelete event trigger

If you let beforeDelete pass through without throwing error, Salesforce will delete the record immediately and put into recycle bin. Since triggers cannot make callouts, you will have to notify ...
user avatar
  • 1,339
0 votes

Apex - Insert json value into custom field

This works with one caveat for(Account acc : account) { Account accJsonCondensed = new Account( Id = acc.Id, Name = acc.Name, ... ); Rating__c rating = new Rating__c( ...
user avatar
  • 61.4k
1 vote

trigger to update parent record field from child

From the simple requirement of incrementing the case count I would use the ternary operator, which the short form of if then else + variable assignment. By this you have the a one liner. See below ...
user avatar
3 votes

trigger to update parent record field from child

Your code has a multitude of flaws, noOfCases counting nothing, as well as an inefficient query, plus not including things like the account changing. You can just do this: public static void ...
user avatar
  • 429k
2 votes
Accepted

trigger to update parent record field from child

The issue is in the below line . acc.Closed_Cases__c+=noOfCases; If the value of Closed_Cases__c field is null then you are adding 1 to the null which gives null pointer exception. Instead of that ...
user avatar
0 votes
Accepted

Method does not exist or incorrect signature: void contains(String) from the type List<Lead>

You are checking string in contains whereas type of your list if Lead. Update code as below. trigger LeadsTrigger on Lead (before insert) { Set<String> leadNamesSet = new Set<...
user avatar
  • 1,028
1 vote
Accepted

How to retain the create date of a deleted Lead record after merging

In the after-delete trigger on Leads, you'll have a populated value in the MasterRecordId field, which you can use to determine how you'd like to update your custom field. trigger ...
user avatar
  • 429k
0 votes

Apex Trigger to Update Feed Comment

Here's an option Turn off Chatter Feed tracking for this Opportunity_Approved__c field in your Feed Tracking settings. That will eliminate the message you are currently getting Add a Flow that ...
user avatar
  • 61.4k
2 votes
Accepted

How to track time spent in each stage of a custom detail object

Sam! You can try to enable the Track Field History feature in your custom object: Then, to track the time spent in each status picklist value create two fields, a process/flow to update them whenever ...
user avatar
0 votes

Tagging Lead with most recent pre-mql campaign (help with trigger)

If you want a trigger to work on deletion of a record, the first thing is that you need to add either before delete or after delete to the list of events that your trigger fires for. After that, you ...
user avatar
  • 46.4k
0 votes

How to track time spent in each stage of a custom detail object

You could add a date/time field to the object to track the date/time that the record entered the current stage When stage changes, use an after save flow to insert a child object record. On the child ...
user avatar
  • 4,203
0 votes

Trigger related projects leads

You have to populate the SFDCProjectId/CreatedId using a batch process. Create another input for updating the Leads with the associated SFDCProjectId and CreatedId to build the relationship. If you ...
user avatar
0 votes

After response from future callout how we can update the field value in record, can you please assist

Try Requery with AccountIds and update. Refer: (The issue reported is NOT the same; but check "updateSERecord" method for the code pattern) https://developer.salesforce.com/forums/?id=...
user avatar
2 votes

Getting error in trigger recursion for example if I create account and than update it. It will give me the concatenation of both insert one and update

Since you want to update a field of the record(s) being passed to the trigger, just do that; there's no need to perform a DML operation. These additional updates will simply get rolled in with the ...
user avatar
  • 26.6k
0 votes

Getting error in trigger recursion for example if I create account and than update it. It will give me the concatenation of both insert one and update

The reason why this happens is pretty straightforward. The trigger does DML on those records too whose Phone fields were not even updated. The fix is pretty straightforward as well once you know why ...
user avatar
  • 1,242
5 votes
Accepted

how to invoke a trigger manually in salesforce

No. If you need to run logic contained in triggers in a non-trigger context, factor the code into a separate service class and call it from a trigger handler. The service class can then be consumed in ...
user avatar
  • 85.9k
0 votes

Swap 2 "Unique" values in a single transaction

I am still not 100% sure, but I suspect there is a a different save process on User object causing each record to be saved one at a time. Hopefully someone can reference an article to back this up. ...
user avatar
0 votes

How can I show the Result with the trigger that I created?

You are querying Account as a single sObject (Account acc = [SELECT Id FROM Account]; ) instead of a list of sObjects, which can lead to either a List has more than 1 row for assignment to SObject or ...
user avatar
3 votes

Variable does not exist: Trigger

You are calling operationType() as a method by including the parentheses, but it is an attribute. Incorrect switch on Trigger.operationType() Correct switch on Trigger.operationType
user avatar
  • 142k
1 vote

Not able to fetch the newly created child record id from the newly created parent record in Apex Trigger

It is impossible for a newly-created parent record to have any child records until its own save process completes. This means you cannot access any child records that will be created in the parent ...
user avatar
  • 85.9k
0 votes

Trigger - Create new record and update lookup field with the related record

Looks like you are using fflib here ... (if not, my solution is only a guide) There are two parts to this issue Updating the Task.Bonus__c field to the newly-created Bonus__c Avoiding recursion in ...
user avatar
  • 61.4k
0 votes

Trigger getting Coverage on Insert but not Update

Ok, I was able to figure it out with @TestSetup, but I'm not sure why it is the case...probably something to do with @TestSetup not doing updates and inserts in the same context? Here is the solution ...
user avatar
  • 1,228
0 votes

Trigger After update doesn't show custom exception message

I've found it necessary to call setMessage to get the text output at the client-side: String m = 'Required document types were not found'; Exception e = new VerifyFileAttachmentTypeException(m); e....
user avatar
  • 129k
0 votes

How to start a flow from a trigger

You can simply call the flow as below: Flow.Interview.myFlow startFlow= new Flow.Interview.myFlow(new Map<String, Object>());
user avatar
0 votes

Write a Trigger For changing subscription start date to the 1st of every month and end date to last date of the month on term object

If you are trying to update the start and end date fields of the OrderApi__Renewal__c record which is getting inserted, there is no need to create a new record of the same object. You just have to ...
user avatar
  • 531
1 vote
Accepted

Null checking before Update DML

Yes you will have the value. You do not need to have update statement for trigger.new in before triggers. The value will automatically update in the records. You can validate the values using System....
user avatar
  • 531
2 votes

Why Places field on account object is not getting updated in the UI after making callout from trigger using future method?

Because you're not performing DML in your @future method. @future methods (and all ways of executing code asynchronously) are run in an entirely separate transaction from the transaction that your ...
user avatar
  • 46.4k
1 vote

Why Places field on account object is not getting updated in the UI after making callout from trigger using future method?

From the code you pasted it seems like you're never updating the values even though you are assigning them correctly. You should be able to set the values by updating Accounts outside the for(Account ...
user avatar
1 vote
Accepted

How to query related Contact from Account by Id in "after insert, before update" trigger?

There are several issues with this code (you should never1 have queries inside of a loop), but the fundamental problem you're running into is one of how relationships work. The immediate error that ...
user avatar
  • 46.4k
0 votes

How to query related Contact from Account by Id in "after insert, before update" trigger?

This block of code successfully returns the correct Contact because both the Account and Contact share a custom field, Lead_Number__c. The AccountId on the Contact object is not yet assigned when the ...
user avatar
  • 5
2 votes

How to check and uncheck a checkbox when inserting and removing a document in a related list

Before Delete triggers do not work for ContentDocumentLink. Use triggers on ContentDocument instead. Please follow below links: https://ideas.salesforce.com/s/idea/a0B8W00000GdWdyUAF/enable-trigger-...
user avatar
  • 531
1 vote

Moving data from one object to another using Apex

You can create a single List to Upsert the records by matching the External Id. Please refer updating a records using external Id in Apex In LWC Javascript, call the Apex method imperatively on HTML ...
user avatar
  • 531
0 votes
Accepted

How to write a test class for this below code can anyone help me

The basic test class should be inserting a Case and updating the case such that the Status field is Closed and Deleting the case to cover 100%. It can be something as below. @isTest public ...
user avatar
0 votes

How to write a test class for this below code can anyone help me

As the platform does not supply mocking for email sends, you have to use code like this to assert what has been sent in your test cases: public class CaseEmailHandler { // Test cases can assert ...
user avatar
  • 129k
0 votes

How to write a test class for this below code can anyone help me

Something like below should work. @isTest public class CaseEmailHandlerTest { @testSetup public static void setup(){ contact con = new contact(); con.LastName = 'Test'; ...
user avatar
1 vote

How can I detect that a record was created by a trigger without creating custom fields?

Use a static variable. // Static variable class public class SystemFlags { public static Boolean tasksCreatedFromTrigger = false; } ... // Lead trigger logic SystemFlags.tasksCreatedFromTrigger = ...
user avatar
  • 429k
0 votes

Check Duplicate Records and sObject row does not allow errors

You're trying to modify the record you queried, not the record in the trigger. You can't add an error to a record that's not in Trigger.old or Trigger.new. Your code also isn't bulkified; multiple ...
user avatar
  • 429k
0 votes

For Loop and DML Inside

Since the combination of CPU and CPU Speed is unique, you may consider to create a formula field which function as a composite key, eg CPU_with_Speed__c To bring your Pricing Data query out of loop, ...
user avatar
0 votes

Callout using Apex Trigger when custom field updated

I would suggest you the following Trigger all starting conditions ideally from a Record Triggered Flow -Whenever apply on flow , call to an specific apex method, by calling a method with @...
user avatar
0 votes

Opportunity Trigger not displaying correct value on Parent Account

To get the highest Amount among opportunities associated to any account, you can create a roll up summary field on Account Object. This roll up summary field will show highest amount of opportunities ...
user avatar
0 votes
Accepted

Opportunity Trigger not displaying correct value on Parent Account

I suspect you have two issues: if(NewOppr.Account.Highest_Opportunity_Amount__c != Null){ near the end of your code. This is causing you to only set the account field if it already has a value. If ...
user avatar
  • 46.4k

Top 50 recent answers are included