You need to sign in to do that
Don't have an account?
Eric Blaxton
Update fields on a custom object...Can I write a stored procedure in APEX?
Requirement: Loop through custom object and change the Owner field to match the value in the Sales Rep field.
What is the best way to accomplish this in APEX? I have done this in Oracle by writing an update statement, but not in APEX.
Thanks,
What is the best way to accomplish this in APEX? I have done this in Oracle by writing an update statement, but not in APEX.
Thanks,
The answer depends on a few things:
Is the Sales Rep field a lookup field? (ie. it contains the Salesforce ID of a User record)
Or does it use some other external ID (eg. Sales Rep employee number)
How many records do you need to update? If it's more than 10,000, you'll need to use Batch Apex.
Or do you want to update records only when they're changed by a user (ie. in a trigger).
The code in the following post will work in the Execute Anonymous window of Developer Console assuming that the Sales_Rep__c field is a lookup that's always populated, and there are not more than 10,000 Custom_Object__c records.
Let me know if this works for you.
Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon
All Answers
The answer depends on a few things:
Is the Sales Rep field a lookup field? (ie. it contains the Salesforce ID of a User record)
Or does it use some other external ID (eg. Sales Rep employee number)
How many records do you need to update? If it's more than 10,000, you'll need to use Batch Apex.
Or do you want to update records only when they're changed by a user (ie. in a trigger).
The code in the following post will work in the Execute Anonymous window of Developer Console assuming that the Sales_Rep__c field is a lookup that's always populated, and there are not more than 10,000 Custom_Object__c records.
Let me know if this works for you.
Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon
List<Custom_Object__c> list_Records = new List<Custom_Object__c>
( [ SELECT Id, OwnerId, Sales_Rep__c
FROM Custom_Object__c
]
);
for ( Custom_Object__c record : list_Records )
{
record.OwnerId = record.Sales_Rep__c;
}
update list_Records;
</pre>