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
...
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 ...
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(...
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 ...
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 ...
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 ...
5
votes
How to insert reports/dashboards inside a lightning component?
There is a Report Chart component that can be added to the lightning app builder, however, it is currently not available in the community builder. Documentation have a lightning component in the ...
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 ...
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 ...
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(...
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 ...
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 ...
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 ...
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 ...
4
votes
How to insert owner name in Account?
Your problem is on this line
Account acc1=new Account(Name='nEW',Account__r.owner.firstname='gSS');
There is no relationship account__r.owner.firstname
The relationship is account.owner....
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....
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,...
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 - ...
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;
}
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 ...
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 ...
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:
...
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":{&...
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 ...
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. ...
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 ...
3
votes
Accepted
How does a trigger behave
TLDR: without knowing more about your architecture it is difficult to say, but simple orgs and those designed well should be able to handle it without issue for the most part.
Your consumption of ...
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)...
3
votes
How to insert reports/dashboards inside a lightning component?
there is a developer blog post on this.
It basically amounts to:
let Apex Controller get Report Data by Report MetaData API
process to your needs and return to lightning component
render as needed
...
3
votes
Accepted
Remote Objects: Insert an array of objects
Ajax toolkit is a Javascript wrapper over SOAP based API .While ajax toolkit is a good option ,the problem with Ajax toolkit has been the no of API calls consumed .It will count against the API limit ....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
insert × 195apex × 93
trigger × 58
update × 20
dml × 13
list × 10
before-trigger × 9
unit-test × 8
data-loader × 8
upsert × 8
soql × 7
sobject × 7
after-trigger × 7
api × 6
map × 6
exception × 6
create-records × 6
visualforce × 5
marketing-cloud × 5
record-type × 5
bulk-api × 5
loop × 5
csv × 5
record × 5
dmlexception × 5