Hot answers tagged

15 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
12 votes
Accepted

Apex Trigger giving "cannot specify Id in an insert call: [Id]" error

You have your DML statement inside of the for loop. So it is trying to insert the list each time through the loop. This will work fine for a single transaction, but the minute you do this with more ...
Chris Duncombe's user avatar
9 votes
Accepted

Create static resource with apex code

You will have to go with MetaData API. Something as below - MetadataService.MetadataPort service = createService(); MetadataService.StaticResource staticResource = new MetadataService....
Amit Bangad's user avatar
  • 3,860
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
  • 148k
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
  • 90.9k
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
  • 90.9k
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
  • 148k
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
  • 148k
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 ...
Richard Jimenez's user avatar
5 votes
Accepted

Insert data to custom object with relationship using external Id

Change: aff.npe5__Organization__c = accReference; To upsert accReference; aff.npe5__Organization__c = accReference.Id; accReference is an Account sObject, you need the ID. Same thing goes for ...
Boris Bachovski's user avatar
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
  • 148k
5 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
  • 34.1k
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
  • 67.6k
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
  • 55.9k
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
  • 148k
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
  • 468k
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
  • 90.9k
4 votes
Accepted

Trigger to insert new record not firing

I think your trigger doesn't even pass your first IF statement due trying to reference the record type name. You're doing c.RecordType.Name which means you're trying to access field on the parent ...
Boris Bachovski's user avatar
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....
cropredy's user avatar
  • 67.6k
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
  • 148k
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
  • 148k
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
  • 148k
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
  • 468k
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
  • 55.9k
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
  • 468k
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 ...
Adrian Larson's user avatar
  • 148k
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 ...
Christian Szandor Knapp's user avatar
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 ....
Mohith Shrivastava's user avatar
3 votes

Remote Objects: Insert an array of objects

I don't believe it's possible (can't see any list methods in the API). So you only option if you want to use Remote Objects is looping on create - not a great way to do it. However, there is an ...
Caspar Harmer's user avatar

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