Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have created a new number field within my Opportunities object which is set to required and has a default value (0), however this is causing issues with closed Opportunities that did not have the field present when editing them.

I would have liked to simply do a mass update using SOQL, but as far as I'm aware we're cant do that. I would like to know what is the quickest/easiest way I can populate the records with this field empty with a figure?

I do not want to install applications to do this, and I should note I have more than 2k records so I believe govern limits will effect the possible solutions.

One solution I have tried is to create a trigger on before update, which would populate these fields if they are empty but this does not work, I believe the required attribute is kicking in before the trigger is.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

For a one time update, try using the Execute Anonymous functionality in the Dev Console. There, one can write and execute Apex code. Note that all the governor limits are still in place (e.g. limit of 10,000 records in DML calls).

Since you mention you only have 2,000ish records to update, Execute Anonymous will he handy.

Example:

List<Account> acctList = [SELECT Id, Name FROM Account LIMIT 2000];

for(Account acct :acctList){
    acct.Name = acct.Name + ' super cool suffix';
}

update acctList;
share|improve this answer

Perhaps I'm missing something about your situation...

But for this sort of problem I would usually run code like this multiple times from the developer console:

Opportunity[] opps = [select Id from Opportunity where NewNumberField__c == null limit 100];
for (Opportunity opp : opps) {
    opp.NewNumberField__c = 0;
}
update opps;

Raise or lower the limit value depending on whether you are hitting any governor limits. The where clause excludes ones that have already been defaulted.

Correct Syntax:

Opportunity[] opps = [select Id, NewNumberField__c from Opportunity where NewNumberField__c = null limit 100];
for (Opportunity opp : opps) {
    opp.NewNumberField__c = 0;
}
update opps;

Or you could use List:

List <Opportunity> opps = [select Id, NewNumberField__c  
                             from Opportunity 
                             where NewNumberField__c = null limit 100];

for (Opportunity opp : opps) {
       opp.NewNumberField__c = 0;
}
update opps;
share|improve this answer

I usually use DemandTools for updating records like this, but that is a third-party application.

If you want to work entirely within Salesforce and not have to worry about the number of records needing an update, you can use batch apex. That allows you to bypass the governor limits. More details on batch apex are here.

If you use a querylocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed.

This is a tutorial on batch apex here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.