+ Start a Discussion
sailersailer 

Save error: Expression must be a list type: SET<Id>

global class RollUpSummaryMapNewTest implements Database.Batchable<sObject>
{
//one map and one loop.
Map<Id,Double> optylineitem=new Map<Id,Double>();
List<Opportunity> updatelist = new List<Opportunity>();
Set<Id> OptyIds = new Set<Id>();

//List<OpportunityLineItem> oppotylineitem =new List<OpportunityLineItem>();
public Double curecy=0;

global Database.QueryLocator start(Database.BatchableContext BC)
{

String lineitem='SELECT id,ListPrice, OpportunityId FROM OpportunityLineItem';

return Database.getQueryLocator(lineitem);
}

global void execute(Database.BatchableContext BC,List<Sobject> batch)
{

List< OpportunityLineItem> Lineitemcast = (List<OpportunityLineItem>) batch;
system.debug('Cast Values'+Lineitemcast);
for(OpportunityLineItem optyrecord : Lineitemcast)
{

if(optylineitem.containsKey(optyrecord.opportunityid))
{

Double listPrice = optyrecord.ListPrice + optylineitem.get(optyrecord.opportunityid);
system.debug('List Price '+listPrice);
optylineitem.remove(optyrecord.opportunityid);
optylineitem.put(optyrecord.opportunityid, listPrice);

}else
{

optylineitem.put(optyrecord.opportunityid,optyrecord.listPrice);
}

}
system.debug('Map Values '+optylineitem.values());

}

global void finish(Database.BatchableContext BC)
{
Set<Id> optylist = new Set<Id>(optylineitem.keySet()); //need to keep in the set as it reuring any values
system.debug('Map List Values'+optylist.size());
for(Integer i=0;i<optylist.size();i++)
{
Opportunity tempstore= new Opportunity(id=optylist[i]); //Save error: Expression must be a list type: SET<Id>
tempstore.testvantage__RollupBatches__c=optylineitem.get(optylist[i]);
updatelist.add(tempstore);
}
try
{
if(updatelist.size()>0)
{
//update updatelist;
system.debug('Final List######### '+updatelist);
}
}catch(DMLException e)
{
system.debug('Batch dosent contain records'+e);
}
}

}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

Try to use below code.

 

Batch class:

global class RollUpSummaryMapNewTest implements Database.Batchable<sObject>{
    //one map and one loop.
    Map<Id,Double> optylineitem=new Map<Id,Double>();
    List<Opportunity> updatelist = new List<Opportunity>();
    //List<OpportunityLineItem> oppotylineitem =new List<OpportunityLineItem>();
    public Double curecy=0;
    global Database.QueryLocator start(Database.BatchableContext BC){
        String lineitem='SELECT id,ListPrice, OpportunityId FROM OpportunityLineItem';
        System.debug('The Query@@@@@@@@'+lineitem);
        return Database.getQueryLocator(lineitem);
    }
    global void execute(Database.BatchableContext BC,List<Sobject> batch){
        List< OpportunityLineItem> Lineitemcast = (List<OpportunityLineItem>) batch;
        for(OpportunityLineItem optyrecord : Lineitemcast){
            if(optylineitem.containsKey(optyrecord.opportunityid)){
                Double listPrice = optyrecord.ListPrice + optylineitem.get(optyrecord.opportunityid);
                optylineitem.remove(optyrecord.opportunityid);
                optylineitem.put(optyrecord.opportunityid, listPrice);
            }else{
                optylineitem.put(optyrecord.opportunityid,optyrecord.listPrice);
            }
        }
    }
    global void finish(Database.BatchableContext BC){
        set<Id> optylist = new set<Id>(optylineitem.keyset());
        system.debug('Map List Values'+optylist);
        for(Id oppid: optylist){
            Opportunity tempstore= new Opportunity(id=oppid);
            //tempstore.id=optylist=optylineitem.ketSet().get('Id');
            //tempstore.id=optylineitem.get('Id');
            tempstore.testvantage__RollupBatches__c=optylineitem.get(oppid);
            updatelist.add(tempstore);
        }
        try{
            if(updatelist.size()>0){
                system.debug('Final List######### '+updatelist);
            }
        }catch(DMLException e){
            system.debug('Batch dosent contain records'+e);
        }
    }
}

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

GlynAGlynA

You can't index into a set, but you can iterate over the set in the for loop:

 

global void finish(Database.BatchableContext BC)
{
    Set<Id> optylist = new Set<Id>(optylineitem.keySet()); //need to keep in the set as it reuring any values
    system.debug('Map List Values'+optylist.size());
    for(Id anId : optylist)
    {
        Opportunity tempstore= new Opportunity(id=anId);
        tempstore.testvantage__RollupBatches__c=optylineitem.get(anId);
        updatelist.add(tempstore);
    }
    try
    {
        if(updatelist.size()>0)
        {
            //update updatelist;
            system.debug('Final List######### '+updatelist);
        }
    }
    catch(DMLException e)
    {
        system.debug('Batch dosent contain records'+e);
    }
}

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

hitesh90hitesh90

Hi,

 

Try to use below code.

 

Batch class:

global class RollUpSummaryMapNewTest implements Database.Batchable<sObject>{
    //one map and one loop.
    Map<Id,Double> optylineitem=new Map<Id,Double>();
    List<Opportunity> updatelist = new List<Opportunity>();
    //List<OpportunityLineItem> oppotylineitem =new List<OpportunityLineItem>();
    public Double curecy=0;
    global Database.QueryLocator start(Database.BatchableContext BC){
        String lineitem='SELECT id,ListPrice, OpportunityId FROM OpportunityLineItem';
        System.debug('The Query@@@@@@@@'+lineitem);
        return Database.getQueryLocator(lineitem);
    }
    global void execute(Database.BatchableContext BC,List<Sobject> batch){
        List< OpportunityLineItem> Lineitemcast = (List<OpportunityLineItem>) batch;
        for(OpportunityLineItem optyrecord : Lineitemcast){
            if(optylineitem.containsKey(optyrecord.opportunityid)){
                Double listPrice = optyrecord.ListPrice + optylineitem.get(optyrecord.opportunityid);
                optylineitem.remove(optyrecord.opportunityid);
                optylineitem.put(optyrecord.opportunityid, listPrice);
            }else{
                optylineitem.put(optyrecord.opportunityid,optyrecord.listPrice);
            }
        }
    }
    global void finish(Database.BatchableContext BC){
        set<Id> optylist = new set<Id>(optylineitem.keyset());
        system.debug('Map List Values'+optylist);
        for(Id oppid: optylist){
            Opportunity tempstore= new Opportunity(id=oppid);
            //tempstore.id=optylist=optylineitem.ketSet().get('Id');
            //tempstore.id=optylineitem.get('Id');
            tempstore.testvantage__RollupBatches__c=optylineitem.get(oppid);
            updatelist.add(tempstore);
        }
        try{
            if(updatelist.size()>0){
                system.debug('Final List######### '+updatelist);
            }
        }catch(DMLException e){
            system.debug('Batch dosent contain records'+e);
        }
    }
}

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
Satyendra RawatSatyendra Rawat

Hi
In batchable class is call the async call means member variable does not share the member value,
If you want to share the member value please implement the "Database.Stateful" interface it is share member variable value


global class RollUpSummaryMapNewTest implements Database.Batchable<sObject>,Database.Stateful
{
//one map and one loop.
Map<Id,Double> optylineitem=new Map<Id,Double>();

Set<Id> OptyIds = new Set<Id>();

//List<OpportunityLineItem> oppotylineitem =new List<OpportunityLineItem>();
public Double curecy=0;
global Database.QueryLocator start(Database.BatchableContext BC)
{

String lineitem='SELECT id,ListPrice, OpportunityId FROM OpportunityLineItem limit 50000';

return Database.getQueryLocator(lineitem);
}
global void execute(Database.BatchableContext BC,List<Sobject> batch)
{
List<OpportunityLineItem> Lineitemcast = (List<OpportunityLineItem>) batch;
system.debug('Cast Values'+Lineitemcast);
for(OpportunityLineItem optyrecord : Lineitemcast)
{

if(optylineitem.containsKey(optyrecord.opportunityid))
{

Double listPrice = optyrecord.ListPrice + optylineitem.get(optyrecord.opportunityid);
system.debug('List Price '+listPrice);
optylineitem.remove(optyrecord.opportunityid);
optylineitem.put(optyrecord.opportunityid, listPrice);
}else
{

optylineitem.put(optyrecord.opportunityid,optyrecord.listPrice);
}

}
system.debug('Map Values '+optylineitem.values());

}
global void finish(Database.BatchableContext BC)
{
Set<Id> optylist = optylineitem.keySet(); //need to keep in the set as it reuring any values
system.debug('Map List Values'+optylist.size());
List<Opportunity> updatelist = new List<Opportunity>();
for(Id oppId: optylineitem.keySet())
{
Opportunity tempstore= new Opportunity(id=oppId); //Save error: Expression must be a list type: SET<Id>
tempstore.testvantage__RollupBatches__c=optylineitem.get(optylist[i]);
updatelist.add(tempstore);
}
try
{
if(updatelist!=null && updatelist.size()>0)
{
update updatelist;
system.debug('Final List######### '+updatelist);
}
}catch(DMLException e)
{
system.debug('Batch dosent contain records'+e);
}
}

}

sailersailer

Hi Hitesh,

 

Can we write the block of code in the finish method,

 

Actually i am trying to update the Opty object in the finish method.

 

Is that possible?

 

 

Regards

Sailer

hitesh90hitesh90

yes, you can write code there..