Hot answers tagged

16

You can match the updated records with the Database.SaveResults using a list iterator, which will give you access to the original record: List<Account> accountsList = [select Id from Account]; for(Account a : accountsList) { a.Name = 'Test', } List<Database.SaveResult> res = Database.update(accountsList, false); //for (Database.SaveResult ...


11

You can do this by comparing the current value of the IsMain field with the value in the trigger.oldMap collection. This will tell you if the field has changed. Something like this will work trigger FileUploadedTrigger on File_uploaded__c (after update) { list<File_uploaded__c> changedIsMainList = new list<File_uploaded__c>(); for(...


10

Just looking at the code you have there a few things stand out. I haven't tested this but I've added comments which might help you along trigger UpdateAmount on Quote__c (after insert, after update) { //You want it on update too, right? Map<ID, Opportunity> parentOpps = new Map<ID, Opportunity>(); //Making it a map instead of list for easier ...


8

The reason your catch block is not being executed is because of the false parameter you are passing in: Database.update(accounts, false); That parameter is called is_all_or_none meaning treat the transaction atomically. If something fails, then make the entire transation fail. The reason for atomicity is pretty standard here...if one of my records fails ...


7

Upgrades between releases (API versions) is not something you can do yourself. Salesforce does this for you and all other production orgs at the same pod at the same time. These moments may vary between servers (pods) and are always announced per mail to administrators and publicly readable on the trust website. http://trust.salesforce.com/trust/...


7

Instead of using @future, you should use Queueable Apex. Queueable Apex, lets you chain jobs. To allow callouts, you need to specify Database.AllowsCallouts public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts { public void execute(QueueableContext context) { // Your processing logic here // Chain this ...


6

There won't be any errors with both triggers. Even if the triggers affected the same fields, there would be no error (it would be undefined which value would win, however). The triggers execute in an undefined order, but effectively act as if they ran concurrently; the results of each trigger are aggregated together.


6

You can code the controller as without sharing and copy the values into and out of an intermediate object. The documentation for with sharing states: The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in ...


6

No, a formula field changing doesn't change the updated by, and doesn't count as a record update. Behind the scenes formula fields aren't stored as part of the record in the database, instead they use logic embedded in SQL at query time, so the values don't persist, instead they're generated on the fly when the object is queried.


6

If you want to change the Owner you can only do it by setting the OwnerId field. If you want to set it based on Name, you will need to query for the User record whose Id you wish to set. List<User> candidates = [SELECT Id FROM User WHERE Name = :someName LIMIT 1]; if (!candidates.isEmpty()) someAccount.OwnerId = candidates[0].Id; If I were building a ...


6

No, SOQL (Salesforce Object Query Language) is a query language. It allows you to pull data from the database. If you want to push data to the database, you use DML (Database Manipulation Language) operations. Basic Example: List<Opportunity> records = [SELECT Id FROM Opportunity WHERE ...]; // above is SOQL // set some fields... update records; // ...


5

Your code is mostly likely doing what you expect it to do. Your test, however, is not doing what you are expecting. You need to requery the Opportunity at the end of your test so that you can get the updated Number_of_students__c value. The last line of your test should be changed to: Opportunity opp = [select Number_of_students__c from Opportunity where ...


5

Trigger.new in this context is going to be a collection of CampaignMember objects rather than miiEventV1__Registration__c objects. The references in your code that are CampaignMember.Account_ID__c, etc. (that explicitly use CampaignMember) are actually retrieving information about that field on the object definition rather than a record of that type of ...


5

Here's some the gaps filled in. I am assuming that Competitor__c is your MultiSelect field, that Win_Reason__c is a Picklist and that Unsolicited_Bid__c is a Checkbox. If not, adjust accordingly. ------------------------------------------------------ -- Field Type * Data Type -- ----------------------------------------------------...


5

See the SaveResult class docs: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_saveresult.htm and the example use: // Iterate through each returned result for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { // Operation was successful, so get the ID of the record that was processed ...


5

There is a few things wrong with Ashas answer: It is not handling multiple accounts being inserted or updated. It only works on the first one in the list It is doing a SOQL query that is not needed See the code below for a modified version of the code that will handle bulk creation of Accounts and will create a task for them all trigger AccountAfter on ...


5

I have simplified it to do what you wanted. Points: You do not need to query for the contacts as the ID already exists on the case object Your context should be a before insert Gather the list of Area names in a collection consolidate things where needed trigger WebToCaseCountry on Case (before insert){ //Set<Id> contactIds = new Set<Id>(); ...


5

You can do it like this: // Your map Map<Id, String> m = new Map<Id, String>(); // Update needs to know the type so create objects of the right type Opportunity[] updates = new Opportunity[] {}; for (Id id : m.keySet()) { updates.add(new Opportunity(Id = id, FieldName__c = m.get(id))); } update updates; Note that there is also a put(String ...


5

Someone will only have to come include that field in the query if they actually need to do something with it in your code. You do not need to (and absolutely should not) include every field on your object in the query. If you did find yourself in a situation in which you need every field, there are ways using dynamic apex or Field Sets to obtain all of the ...


5

You're doing things in a kinda funky way, and that's leading to your issues. A list that holds SObjects (such as your List<NFP_Lead_Account>) will hold references to the SObject instance. So, if you do something like this List<SObject> myList = new List<SObject>(); MyObject__c myRec = new MyObject__c(); myList.add(myRec); myList.add(...


5

It sounds like you want to merge two records. The Database.merge method (or the merge DML keyword) will move all children from the bad account to the good account, and delete the bad account. If you want to keep the bad account, you can simply call Database.undelete (or the undelete DML keyword) on the bad account afterwards; all of the children affected ...


5

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 Sir Arthur Conan Doyle's Sherlock Holmes when you eliminate the impossible, whatever remains, however improbable, must be the truth An NPE occurs when you ...


5

Ideally for such kind of requirements, it would be better if you can build the logic in before update event rather than after update event, in which case, there won't be any need to handle the recursion and you won't need to write the DML (update) statement as well which would make this the safest approach. If you still want to go with after update event, ...


5

If I had to take a guess at what your issue is, I'd say that you're probably getting a Duplicate object in list error. You're not seeing that error because you are smothering any exceptions you encounter with that try/catch block. I'd recommend removing the try/catch, or at the very least adding some additional debugging so that you can see that an ...


4

You should be able to do something like this: List<Opportunity> opptys = [SELECT Id, Name, (SELECT Id, PricebookEntry.Product2.Name FROM OpportunityLineItems) FROM Opportunity WHERE Id =: updatedOpportunities]; Then you can reference the list of OpportunityLineItems with something like the following to iterate over each Opportunity and create a map: ...


4

Unless you have a specific need to and include the SeeAllData annotation, your existing data won't be available to your test. Additionally, since your test may need to run in a variety of environments (full SB, Dev SB, Production), hard coding record ids is pretty much guaranteed to fail anyway. So you need to create your Account, Contact and Campaign as ...


4

Problem is that the email field is NOT necessarily unique. So even the VLOOKUP may end up in matching many ids for one email. If you would have the email in an external unique id field, you could utilize the smart-lookup feature of workbench https://workbench.developerforce.com/login.php - from all those dataloader-like tools I allways found workbench as ...


4

All Records in Salesforce must have an Owner which is either a User or a Queue, if you try and set OwnerId to Null or blank "", this will always fail as the field is mandatory therefore Null is not an acceptable value. As the Salesforce security model for data is built around record ownership this is correct behaviour as it will mean that any relevant ...


4

See the Time-Based Workflow FAQ: Will the pending actions in the queue ALWAYS trigger? No. Time-dependent actions remain in the Workflow Queue until processed or the rule criteria for the Workflow Rule are evaluated as "false." If a record no longer matches the rule criteria when the rule is evaluated, Salesforce removes the time-dependent actions ...


4

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 Workflow INFO and Apex Code DEBUG Or, you may have workflow rules or Process Builder rules that do updates on the Lead. The object will have changed and per SFDC ...


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