//use GlobalDecribe to get a list of all available Objects
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Set<String> objectKeys = gd.keySet();
for(String objectKey: objectKeys)
{
//Iterate through all objects to locate selected Object
if (objectKey == 'account')
{
Schema.SObjectType systemObjectType = gd.get(objectKey);
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap();
Set<String> fieldNames = M.keySet();
//iterate through all fields of the object to locate the field
for(String fieldName: fieldNames)
{
if (fieldName == lookupField.toLowerCase())
{
sObjLst = new List<SObject>() ;
String query = 'select id , name from ' + fieldName +' limit 10000';
sObjLst = Database.Query(query) ;
}
}
}
}
global class SearchAndReplace implements Database.Batchable<sObject>{ global final string query; global final string field; global final string value; public SearchAndReplace(string q,string f,string v) { query=q; field=f; value=v; } global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc,List<sobject> lstsobj) { for(sobject s: lstsobj) { s.put(field,value); // Here how can i use condition to update the value? ********************** } update lstsobj; } global void finish(Database.BatchableContext bc) { } }
System Log:
SearchAndReplace obj=new SearchAndReplace ('select Id,name from DataLoadTest__c','name','ANKITA'); database.executebatch(obj);
I used this code:
But here all the name field values in my DataLoadTest__c object will be updating.
how can i update name value based on specific condition.
suppose ,
if(name=='Ajay')
then i need to update this value from Ajay to ANKITA
Try this :
If(String.valueOf(s.get('name')).toLowercase == 'ajay')
Thanks
Ankit Arora
Blog | Facebook | Blog Page
All Answers
A small sample :
And list of all SObject Method : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm
Another good link : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm
Thanks
Ankit Arora
Blog | Facebook | Blog Page
10x ANKIT
Regards:
ANKITA
global class SearchAndReplace implements Database.Batchable<sObject>{
global final string query;
global final string field;
global final string value;
public SearchAndReplace(string q,string f,string v)
{
query=q;
field=f;
value=v;
}
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<sobject> lstsobj)
{
for(sobject s: lstsobj)
{
s.put(field,value); // Here how can i use condition to update the value? **********************
}
update lstsobj;
}
global void finish(Database.BatchableContext bc)
{
}
}
System Log:
SearchAndReplace obj=new SearchAndReplace ('select Id,name from DataLoadTest__c','name','ANKITA');
database.executebatch(obj);
I used this code:
But here all the name field values in my DataLoadTest__c object will be updating.
how can i update name value based on specific condition.
suppose ,
if(name=='Ajay')
then i need to update this value from Ajay to ANKITA
Try this :
If(String.valueOf(s.get('name')).toLowercase == 'ajay')
Thanks
Ankit Arora
Blog | Facebook | Blog Page