New answers tagged

0 votes

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

How to track time spent in each stage of a custom detail object Is it possible to share that loop decision criteria and update node elements criteria?
  • 1
1 vote

I couldn't manage to run the trigger test successfully

As long as you are not using the @seeAllData=true option on your test class/method, You can use the Test.setCreatedDate method on your newly created test opportunity record. Hope it helps.
  • 500
0 votes

Can we determine if the Salesforce instance is production org or a Sandbox org?

I do think there's a method that allows this without a SOQL query and without running the risk that URLs change because of domain name changes. Salesforce has a System.Domain class that has a method ...
0 votes
Accepted

Chaining Queueable Apex with Callouts Run Into Loop

if (userIds != null && (!System.isFuture() || !System.isQueueable())) { Is incorrect. If you're in a future call, you won't be in a queueable call, and if you're in a queueable call, you won'...
  • 445k
1 vote

Trigger to count of related contacts on Account based on checkbox field

The main problem you're running into here in your handler is that acc.contacts.size() doesn't distinguish between your two Contact fields, it's simply the count of all of the Contacts related to the ...
  • 50.6k
1 vote
Accepted

Before Insert Trigger field not writeable

Trigger variables don't contain fields from parent records. You need to gather lookup values (newobj.Account__c) and put them into Set, either 2a SOQL current values of Account.Number_of_obj__c and ...
  • 4,212
0 votes

Trigger to count of related contacts on Account based on checkbox field

If you need to do this way, you must change code as below: for(Account acc : [SELECT Id, Total_X_Contacts__c, Total_Y_Contacts__c, (SELECT Id, Team_X__c, Team_Y__c FROM Contacts) FROM Account WHERE Id ...
  • 11
1 vote

How to programmatically disable a Process Builder

You can create a Boolean checkbox field on custom setting.Then update your Process Builders so that the very first entry criterion is the custom setting being true, and if it is not, stop. Then from ...
7 votes

WHY do triggers execute in chunks of 200 records, if still synchronous and more likely to hit limits?

The original 1.0 API also only supported a single record at the time for a DML operation. There was a batch mode, but it was a separate call that just performed the DML one record a time until it was ...
  • 445k
0 votes

I want to add no of count of task on lead

To get the exact count you can try writing a trigger for all the three cases. After insert, after update and after delete. So that if a task is created under a lead or updated/deleted under lead it ...
0 votes

Can we add the debug statement without a method?

There are two kinds of executable blocks in Apex. There are static blocks and instance blocks. Static blocks can be anonymous blocks and static methods. Instance blocks come in two basic flavors: ...
  • 445k
0 votes

Hi all, I am writing a trigger handler that needs to update the owner of a record to a specific user, but I am getting a DMLexception error

I solved the problem by update the line below: List<Account> accountRecords = [SELECT Id, Name, OwnerId FROM Account WHERE OwnerId = :deactivatedUsersIds LIMIT 1]; //REMOVE LIMIT AFTER TESTING ...
  • 13
0 votes

Hi all, I am writing a trigger handler that needs to update the owner of a record to a specific user, but I am getting a DMLexception error

The issue here is because in the trigger declaration you have used up before update and after update, but when we go down in the trigger, we have not specified when it would run for isBefore or ...
6 votes
Accepted

How to subtract 1 day from DateTime value in apex?

As per the other answer, the various add* methods support negative values for subtractions. However, if you want the time for your StartDateTime field to remain the same, even when subtracting the day ...
  • 28.7k
2 votes

How to subtract 1 day from DateTime value in apex?

AddDays() method also supports negative values. If you want to subtract days you can use it as below. Field.addDays(-value)
0 votes

Can we add the debug statement without a method?

You should have at least constructor in your apex class to write the system.debug statement as below. public class sample1234 { integer h=10; public sample1234(){ system.debug(h); } } ...
1 vote

CPU Time limit exception inside trigger

Your code has a lot of logic bugs in it. resultMapMPAT should be a plain old Set, Trigger.new can't be used in an after delete trigger, you should never truncate 18-character ID values like that (it's ...
  • 445k
2 votes
Accepted

APEX: How to get Field Label Name using Field API?

Your logic is correct. You just need to pass objectname in OBJAPI and field api name in field. Something like this. Map<String,Schema.SObjectField> mfields = Schema.Account.SObjectType....
2 votes

Problem statement : Prevent Deletion of Loan Application if Applicant exists. Loan Application -> parent obj and Applicant -> child. Is trigger right?

You don't need a trigger for this. You can simply specify the option to prevent deletion of the parent object on the field itself. On the field properties, simply check: Don't allow deletion of the ...
  • 445k
2 votes

Problem statement : Prevent Deletion of Loan Application if Applicant exists. Loan Application -> parent obj and Applicant -> child. Is trigger right?

Assuming you're ignoring the actual field / object api names. The idea here seems correct. There is one issue with your code with line, If( ApplicantList.contains(loanApp.id) { ApplicationList here ...
  • 1,294
0 votes

Is my solution right logically?

Technically, you don't need to care about the old status of the account, you can query just the current status. Since we're deleting opportunities when the account meets the criteria, we can write ...
  • 445k
3 votes
Accepted

trigger practice

List<Opportunity> listAccountIds=[SELECT stageName, createdDate, accountId FROM Opportunity WHERE AccountId !=null]; You're essentially querying every record in the database. Instead, use an ...
  • 445k
3 votes

Is my solution right logically?

Use AccountId instead of Id. List<Opportunity> opportunitiesToRemove = [SELECT id FROM Opportunity WHERE AccountId IN = :accountIds]; And also some additional comments from my side: You can ...
  • 1,347
0 votes

Comparing trigger.newmap.keyset() vs trigger.new in SOQL query

Yes, you can always use a List<sObject> as a filter assuming that the type of the sObject matches the lookup field's sObject type, and assuming that you want to use the Id field.
  • 445k
7 votes
Accepted

Any Reason NOT to Use FOR UPDATE?

General If you have FOR UPDATE on your query, you cause the record(s) (and potentially related records too) to be locked at the time of the query execution in the transaction against the Salesforce ...
  • 28.7k
3 votes
Accepted

I am trying to write a trigger that transfers accounts records owned by a particular user when that user is deactivated

The problem is in the 2nd Step, where you are trying to get accounts owned by deactivated user, but instead retrieving accounts owned by the user who is deactivating (UsefInfo.getUserId()). Also you ...
1 vote

Apex Trigger count the number of updated records

You should be using maps for (Opportunity o : Trigger.new) { if (Trigger.oldMap.get(o.Id).Name != o.Name) { // name changed count++; } } Maps are way more efficient than doing for loops ...
  • 63.8k
1 vote

Record triggered flow after save get child records

The reason for your issue is the following: The recordsToInsert variable contains an Opportunity and 0..n Child Records. The insert operation will insert the Opportunity first and then initiate the ...
  • 63.8k
7 votes

Any Reason NOT to Use FOR UPDATE?

A lock is always necessary to update a record, but a transaction might already have a lock on the record you want to update, so you don't necessarily need FOR UPDATE in every query. Performing a DML ...
  • 445k
2 votes
Accepted

Apex: How to Put Map of strings inside a List

You have used putAll. putAll either accepts a List of Map entries or a map of entries itself. It should be something like this: List<Map<String, String>> ss = new List<Map<String, ...
0 votes

How to write a validation rule that throws an error when a field value was already inserted?

Your question is a little ambiguous. Do you mean one of the following? 1. You want to check other fields on the record for the same value as the CreatedById. This is pretty straightforward. Your ...
-1 votes

how to compare field values of map in a for loop

We just need to change the map structure and the if condition a bit Map to contain the values to compare 'if condition' to check the values fetched from map The context of whether both contact and ...
  • 90
0 votes

Update field on related object

You just need to change your query to look for the LIST of ids, rather than a specific id as you have in your query: Change: AND AssetId = :prdAsPO To: AND AssetId IN :prdAsPO
1 vote

System.DmlException: Insert failed. CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Welcome to Salesforce Stack Exchange (SFSE)! Firstly: You do not seem to be doing any check to see if a related record of the other object already exists before you insert it. Is that a possibility ...
  • 2,317
0 votes

Test Coverage for trigger on ActionCadeneSteptracker CDC

@isTest(SeeAllData=true) public class TestUpdateCustomOnStepCompletionTrigger { @isTest static void testActionCadenceEvent() { Test.enableChangeDataCapture(); list<...
2 votes

“Best Practice” way to get value from child record saved to parent record

I've always done this with Declarative Lookup Rollup Summaries, rather than trying to write all my own triggers. It's faster, easier, and less error-prone. I'd drop down to handwritten triggers only ...
  • 88.2k
0 votes
Accepted

Entitlement status change not firing update trigger

Trigger would not work in this case, it would only fire if you make any update manually or by some other custom actions. You need to write apex scheduler which will check for expire status and clear ...
  • 2,402
0 votes

issues updating the IsExternallyVisible flag on email message

after keeping IsExternallyVisible as false still external user is able to see the emails in portal but not able to open it . its giving access issue . instead of this cant we hide the emails from ...
0 votes

Error while deleting record on update using trigger

Trigger Trigger AccountTrigger on Account (Before Delete){ if(Trigger.isDelete){ AccountClassDemo1.beforeMethod(Trigger.Old) } } Apex AccountClassDemo1 public class AccountClassDemo1{ ...

Top 50 recent answers are included