You need to sign in to do that
Don't have an account?

batch apex class with upsert
Hi All,
I have 4 objects Forecast__c, ForeCast_Details__C, Opportunity and Opportunity_Forecast__c. Opportunity object contains multiple Opportunity_Forecast__c. Forecast__c has looup to Opportunity Object. Now i have to write batch apex class with upsert the ForeCast_Details__C object records with value of Opportunity_Forecast__c.Could anyone please help on this.
Thanks in Advance.
I have 4 objects Forecast__c, ForeCast_Details__C, Opportunity and Opportunity_Forecast__c. Opportunity object contains multiple Opportunity_Forecast__c. Forecast__c has looup to Opportunity Object. Now i have to write batch apex class with upsert the ForeCast_Details__C object records with value of Opportunity_Forecast__c.Could anyone please help on this.
Thanks in Advance.
String query = select id,Opportunity_Forecast__c,Opportunity_Forecast__r.name from Opportunity; //include all the required fields in the query
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);}
global void execute(Database.BatchableContext BC, List<sObject> scope){
List<ForeCast_Details__C> foreCastDetailsToInsert = new List<ForeCast_Details__C>();
for(Opportunity s : scope)
{
for(Opportunity_Forecast__c of: s.Opportunity_Forecast__c){
ForeCast_Details__C f = new ForeCast_Details__C();
f.name = of.name; //set all other fields, id for upsert
foreCastDetailsToInsert.add(f); }
}
if(!foreCastDetailsToInsert .isEmpty){
upsert foreCastDetailsToInsert ;
}
}
global void finish(Database.BatchableContext BC){
}
}
I have written below batch class. But still i am getting error as Loop must iterate over collection: Id at line 14 column 7. Please suggest me.