You need to sign in to do that
Don't have an account?
yujohwan99
How can I update a specific field from my records?
I'm trying to mass update a specific field using developer's console, but I only end up deleting the record itself. Here is my code:
Contact[] contactDel = [SELECT Email__c FROM Contact
WHERE Join_Date__c <= '2000'
AND Email__c LIKE '%it.work.com’];
delete contactDel;
I'm currently trying to mass delete an email field that ends with "it.work.com," but only from people who joined on or before the year 2000. I don't want to necessarily remove the field from the record, but just delete the entry so that it's blank. However, this ends up deleting the whole record. Is there another way to do this? Maybe such as retrieving the ID's of these record?
Contact[] contactDel = [SELECT Email__c FROM Contact
WHERE Join_Date__c <= '2000'
AND Email__c LIKE '%it.work.com’];
delete contactDel;
I'm currently trying to mass delete an email field that ends with "it.work.com," but only from people who joined on or before the year 2000. I don't want to necessarily remove the field from the record, but just delete the entry so that it's blank. However, this ends up deleting the whole record. Is there another way to do this? Maybe such as retrieving the ID's of these record?
(For Contact c: cons) {
// it is read as : for every contact c in the cons list
// c is a variable that holds each contact record in that list
// cons is a list of contact records returned by the SOQL
// we loop through each record in that list to see if the Email__c field has a value that ends with 'it.works.com'
}
c.Email__c.endsWithIgnoreCase('it.work.com'))
// instead of doing a wild card search in the SOQL, i used the function endsWithIgnoreCase becuase it specifically addresses your use case
massupdate
// In salesforce it's easy to hit governor limits. Hence you need to adhere to certain best practices.
// instead of updating records one at a time, I am collecting all records that need to be updated in a list which i named as massupdate
// Update massupdate means update all the records at the same time.
Try this code:
All Answers
Try the code i have posted. Let me know if you need further explanation or help.
I tried the code you gave me, but I received a "trying to dereference a null object" at around line 14 on your code. I only changed line 9 a little differently: I wrote Join_Date__c <= '2018' instead.
The data type for Join_Date__c is picklist, so I'm assuming I can just use an <= operator. I'm also still confused as to what (For Contact c: cons) is doing. Why are you comparing cons with Contact c? And I don't understand how creating a massupdate list would actually update the original list.
(For Contact c: cons){ } is a for loop (iterates over all the elements in a cons list, and runs code block for each c value from that list): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_lists.htm
I think that Rov used a massupdate list to avoid the query that use a leading wildcard. Instead, he used the endsWithIgnoreCase() function and collected the appropriate records in this list.
(For Contact c: cons) {
// it is read as : for every contact c in the cons list
// c is a variable that holds each contact record in that list
// cons is a list of contact records returned by the SOQL
// we loop through each record in that list to see if the Email__c field has a value that ends with 'it.works.com'
}
c.Email__c.endsWithIgnoreCase('it.work.com'))
// instead of doing a wild card search in the SOQL, i used the function endsWithIgnoreCase becuase it specifically addresses your use case
massupdate
// In salesforce it's easy to hit governor limits. Hence you need to adhere to certain best practices.
// instead of updating records one at a time, I am collecting all records that need to be updated in a list which i named as massupdate
// Update massupdate means update all the records at the same time.
Try this code:
One small question: for "massupdate.add(c)," I'm understanding this as adding each updated record froms variable c. Wouldn't this result in two same records for massupdate except one has an empty email field? What is "update massupdate" exactly doing?
Instead of individually updating each record, we are collecting all records in a list and updating that list
massupdate.add(c) //only adds record that need to be updated to the list
update massupdate // actually saves those new changes in the database; in other words it actually updates the records with the new value
Example: if you have 10 shirts to wash, you dont put one shirt in the washer, after it's done washing, put second shirt to wash and so on..
you collect all 10 shirts in a laundry hamper and wash all 10 shirts at the same time.
hamper.add(shirt); // add each shirt to be washed in the hamper
Wash hamper // wash all shirts that are in the hamper
I think the word massupdate is confusing you. massupdate is just the name i gave to the list, you can call whatever you want.
Also, I've ran into a new problem. The code was working fine yesterday, but after running it today, I received the same error "trying to derefence a null object." I don't understand why this is suddenly appearing again after working perfectly fine.
The code i wrote is already bulkified to get around governor limits.
The only reason i see it hitting governor limits is the SOQL query returning more than allowed records.
In that case,remove the if(c.Email__c.endsWithIgnoreCase('it.work.com')) and put the wild card search filter in the SOQL. This way, SOQL wil return lesser records to process in the for loop.
here's the modified code...
List<Contact> listUpdate = new List<Contact>();
List<Contact> cons = [SELECT ID, Name, Email__c, Join_Date__c
FROM Contact
//I'm just going to have to enter each year. Not a big deal as there isn't that many.
WHERE Join_Date_c = '2013'
AND Email__c LIKE '%it.work.com'];
system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' + cons.size());
For(Contact c: cons){
c.Email__c = null;
listUpdate.add(c);
}
if(!listUpdate.isEmpty()){
update listUpdate;
}