Skip to main content
16 votes
Accepted

Inserting test record with record type

You need to set the Id not the name. First fetch the Id Id stExam = Schema.SObjectType.Student_Records__c.getRecordTypeInfosByName().get('Student Exam Records').getRecordTypeId(); and then assign it ...
Tushar Sharma's user avatar
9 votes
Accepted

Inserting Accounts and Contacts Simultaneously

If you have an External Id field on Account that you can specify, you can set the relationship via the Name Pointing Field (Account). String externalId = '<some_value>'; Account ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

Why is my flag not working on maximum trigger depth exceeded

You're seeing recursion because your recursion guard is in the wrong place. When this gets executed: AankoopDetailTriggerHandler.isBeforeInsert(trigger.new); if(...
David Reed's user avatar
  • 93.2k
6 votes

Insert 2 objects (parent and child) at a time (over 200 records)

Your Case.Id values are null until your perform the insert with insert newCases. You should be performing two DML operations, as you do here, and only a single SOQL query. Right now, you're ...
David Reed's user avatar
  • 93.2k
6 votes
Accepted

How to find required fields for an sObject?

Please note that fields can be made required via other means which are difficult or impossible to detect. Validation rules, triggers, and Process Builder can all introduce errors when a field is not ...
Adrian Larson's user avatar
  • 151k
6 votes

Is my after insert trigger efficient and safe to port to production?

No, you have not properly bulkified your code. Bulkification 101's most important lesson is to aggregate filter values, query before your loop and put records into a Map, and update values after your ...
Adrian Larson's user avatar
  • 151k
5 votes
Accepted

INVALID_FIELD, Foreign key external ID not found - Inserting Related Records via External Id

Take a look at Creating Parent and Child Records in a Single Statement Using Foreign Keys (emphasis mine): You can create related records that are up to 10 levels deep. Also, the related records ...
Adrian Larson's user avatar
  • 151k
5 votes

Lead before update runs on insert

A typical reason for this is that you have Lead Assignment rules. Once those execute, a new owner is assigned and hence the Lead object is updated. You can see this in the debug log if you enable ...
cropredy's user avatar
  • 72.1k
5 votes
Accepted

Trigger:Too many soql query error

You don't need a Query here. You can easily do this in Before insert and Before update event. trigger UpdateAssetImageurl on SymphonyIPM__Patent__c (before insert,before update) { for(...
Tushar Sharma's user avatar
5 votes
Accepted

|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object on my Apex Trigger

Others have covered how to address the Null Pointer Exception (NPE for short). I'd like to cover how to determine the cause of an NPE. When debugging NPEs, I like to keep in mind the famous saying of ...
Derek F's user avatar
  • 62.7k
5 votes

Assign a record as a look up object before insertion

It is not possible to insert parent and child records with the statement when the records have the same SObjectType. You will need to insert any parents, then insert or update any children to populate ...
Adrian Larson's user avatar
  • 151k
5 votes
Accepted

List.clear() not emptying my List

The 10,000 DML row limit is per transaction, not per operation. You can't update more than 10,000 rows from start to finish. If you need to do so, you'll need to use Batchable or Queueable methods to ...
sfdcfox's user avatar
  • 497k
5 votes

Before insert callout to apex class, apex class not updating fields in trigger

Your trigger and your @future method are executing in different transaction contexts. The lifespan of a static variable is a transaction. As a result, your trigger Apex cannot access the values of any ...
David Reed's user avatar
  • 93.2k
4 votes

Trying to get Code Coverage on my apex Class

Just a note on how to cover the catch block, my standard approach is to use a lookup Id for the incorrect sObjectType. For example, if you set busIntId to a User Id, then you should get a DmlException....
Adrian Larson's user avatar
  • 151k
4 votes
Accepted

Uncommitted work in tests / Preparing for callouts

Simply searching that error message would yield some highly useful information: Sometimes a developer needs to create a record and then update it with information provided by a Web Service. However,...
Adrian Larson's user avatar
  • 151k
4 votes
Accepted

Inserting Records using a lightning component

A conceptual approach could be store the data you need as custom data-attributes in the <li> collect the data you need into a JavaScript object (I use jquery for DOM-operations like this - ...
Uwe Heim's user avatar
  • 28.4k
4 votes

|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object on my Apex Trigger

If you cannot guarantee that a value is not null, you typically add a null check: MyObject__c record = myMap.get(someKey); if (record != null) { record.MyField__c = someValue; }
Adrian Larson's user avatar
  • 151k
4 votes
Accepted

Error: Too many DML rows: 10001

This is a per-transaction limit. You'll need to break your code apart by using Batchable, Queueable or some other technique to stay under the 10,000 DML rows per transaction limit. There's way to ...
sfdcfox's user avatar
  • 497k
4 votes

Getting Unexpected Token error when inserting records from execute anonymous

You haven't provided much in the way of details here, but I'd suspect that your issue is with the date you're trying to set to Date_of_Visit__c I know that SOQL queries can handle date literals, but ...
Derek F's user avatar
  • 62.7k
4 votes

Write a trigger on Contact and fill its Mailing Address with its Account's Shipping Address

The ShippingAddress field is a compound field, and thus read-only. Use the other fields (Street, City, State, PostalCode, Country) instead. Also, in general, you can use a Map for more efficiency: ...
sfdcfox's user avatar
  • 497k
4 votes
Accepted

Insert records of user Selected Object without knowing object first

If you provide an attributes property, you can deserialize into the generic sObject type, and use that. Here's what that looks like: sObject a = (sObject)JSON.deserialize('{"attributes":{&...
sfdcfox's user avatar
  • 497k
4 votes

Data insertion failed with salesforce inspector

This error means that your user does not have access to the record type you have specified. With Id 0121t0000001qxAAAQ. You can either grant access to this in the user's Profile or via a Permission ...
fred's user avatar
  • 3,414
3 votes
Accepted

Avoid Duplicate Id in a List

You should consider using the Map class instead of a List. In fact, if you instantiate some maps like this: Map<Id, Bundle__c> bndlList = new Map<Id, Bundle__c>(); The filter on ...
Martin Lezer's user avatar
  • 7,295
3 votes

APEX fields on inserted sObject

The DescribeFieldResult class has methods that supply the default values but there are bugs. But for a few fields of types not subject to those bugs you could assign the default value using something ...
Keith C's user avatar
  • 136k
3 votes

DML not allowed on User

Use the Database.upsert static method instead of the upsert DML keyword: //failure (DML not allowed on User) upsert user ExternalId__c; //success Database.upsert(new User(), User.ExternalId__c, true)...
Matt and Neil's user avatar
3 votes
Accepted

Trying to get Code Coverage on my apex Class

Your unit test appears to be the standard form for testing a trigger. You're not trying to test a trigger, so this type of pattern is inappropriate. Instead, you want to use a "test a class" form. ...
sfdcfox's user avatar
  • 497k
3 votes

Checked required fields, still getting an error that a required field is missing on Insert

Even though the list of standard fields on Order says that the field name is Account, the API name of the field with the label Account Name on the Order object is actually AccountId. This is also ...
Derek F's user avatar
  • 62.7k
3 votes
Accepted

Prevent insertion of records based on a condition

You can indeed use validation rule: 1=CASE(LOWER(RIGHT(firstName__c,2)),"op",1,"gp",1,"kp",1,0) A trigger is also possible, but not necessary.
sfdcfox's user avatar
  • 497k
3 votes
Accepted

Pass value from one method to another

The trick you're trying to use would work, except that a future method, being called in a separate transaction, won't remember the contactList from the first transaction. This is because the data is ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

System.assertEquals for records in a list? Test class

Yes, you can do asserts in a for loop, if it makes sense. While it is rare to see examples of this on the Internet, there's nothing wrong with it. For example, let's say I was testing a trigger that ...
sfdcfox's user avatar
  • 497k

Only top scored, non community-wiki answers of a minimum length are eligible