-1

I want to do a bulk update on any when the filed have a null value. Update the field to have random number. Kindly help me with code.

2
  • 2
    Have you looked at Math.random() function? Or maybe just Datetime in epoch format? What is the use case for updating a random number? – manjit5190 May 29 at 5:58
  • What have you tried? Where are you stuck? SFSE is happy to help with specific technical problems, but strongly discourages requests for others to write code for you. See How to Ask and help center. – David Reed May 29 at 16:05
1

I would like to suggest you use the Math.random() function to get random numbers.

Math.random() function returns a value :- A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

Here in this example given below, I have used 100. This 100 is a limit of the range of random number like it will generate random values from 0 to 100(exclusive) because if I don't use any value as a limit like this

Integer randomNumber = Integer.valueOf(Math.random());

then it will generate a value between 0(inclusive) and 1(exclusive) and as I am getting values in Integer data-type, I will only get 0 as output(in the case when don't give any limit).

List<Account> accList = [SELECT Id, NumberOfEmployees FROM Account WHERE NumberOfEmployees = null LIMIT 1000];
for(Account acc: accList){
    Integer randomNumber = Integer.valueOf((Math.random()*100));
    acc.NumberOfEmployees = randomNumber;
}
update accList;
System.debug(accList);

Hope this explanation will resolve your query.

Thanks

Akshay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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