function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sailersailer 

To Add the sum to the Opportunity based on the OpportunityLineItem

HI Everyone,

I want to add the sum to opportunity of all the items and i t should be in the batch process.

Its not going into the for loop that has been coloured.

 

Pl help me out 

 

Thanks in advance

 

global class RollUpSummaryMaps implements Database.Batchable<sObject>
{

list<OpportunityLineItem> opporto =new List<OpportunityLineItem>();
list<Opportunity> optylist =new List<Opportunity>();
Opportunity optyupdate= new Opportunity();
public Double curecy=0;

global Database.QueryLocator start(Database.BatchableContext BC)
{
// Access initialState here
String lineitem='SELECT id,ListPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem';

// Database.ge
System.debug('The Query@@@@@@@@'+lineitem);
return Database.getQueryLocator(lineitem);
}

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

Map<ID, Opportunity> iMap = new Map<ID, Opportunity>([select Id,testvantage__Updatevalue__c From Opportunity ]);
Map<ID, OpportunityLineItem> mapOppty = new Map<ID, OpportunityLineItem>([SELECT Id,UnitPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem where OpportunityId IN :iMap.keySet()]);
Integer optylinesize=iMap.size();
System.debug('NEWDEBUGOptyLineItem @@@@@@@@@@@@@@############'+optylinesize);
Integer optysize=mapOppty.size();
System.debug('NEWDEBUGOptyList @@@@@@@@@@@@@@############'+optysize);
try{
if(mapOppty.size() != null && mapOppty.size()>0)
{

for(Id accId:iMap.keySet())
{
for(OpportunityLineItem lineitem:opporto)
{
system.debug('lineitem####################'+lineitem.UnitPrice);
curecy=lineitem.UnitPrice;
curecy++;
system.debug('TOTAL VALUES OF LIST PRICE@@@@@@@@@@@@@@@@@@'+curecy);
}

Id opptyId = iMap.get(accId).Id;
optyupdate = iMap.get(opptyId);
optyupdate.testvantage__Updatevalue__c=+curecy;
OptyList.add(optyupdate);

}

if(OptyList.size()>0)
{
update OptyList;
}


}
}catch(DMLException e)
{
system.debug('The batch jobs were not added to the queue successfully: '+e);
}

}

global void finish(Database.BatchableContext BC)
{

}
}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

You have to use inner SOQL query for your requirement to fulfill.

 

Try to use below updated batch class code.

 

Batch class:

global class RollUpSummaryMaps implements Database.Batchable<sObject>{    
    list<OpportunityLineItem> opporto =new List<OpportunityLineItem>();
    list<Opportunity> optylist =new List<Opportunity>();
    Opportunity optyupdate= new Opportunity();
    public Double curecy=0;    
    global Database.QueryLocator start(Database.BatchableContext BC){
        // Access initialState here
        String lineitem='SELECT id,ListPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem';        
        // Database.ge
        System.debug('The Query@@@@@@@@'+lineitem);
        return Database.getQueryLocator(lineitem);
    }
    
    global void execute(Database.BatchableContext BC,List<Sobject> batch){    
        Map<ID, Opportunity> iMap = new Map<ID, Opportunity>([select Id,testvantage__Updatevalue__c,(select id,UnitPrice from opportunityLineitems) From Opportunity ]);
        //Map<ID, OpportunityLineItem> mapOppty = new Map<ID, OpportunityLineItem>([SELECT Id,UnitPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem where OpportunityId IN :iMap.keySet()]);
        Integer optylinesize=iMap.size();
        System.debug('NEWDEBUGOptyLineItem @@@@@@@@@@@@@@############'+optylinesize);
        Integer optysize=mapOppty.size();
        System.debug('NEWDEBUGOptyList @@@@@@@@@@@@@@############'+optysize);
        try{
            if(iMap != null && iMap.size()>0){
                for(Opportunity opp: iMap.values){
                    for(OpportunityLineItem lineitem:opp.opportunityLineitems){
                        if(lineitem.UnitPrice != null){
                            opp.testvantage__Updatevalue__c += lineitem.UnitPrice;
                        }
                    }
                    OptyList.add(opp);
                }
                if(OptyList.size()>0){
                    update OptyList;
                }
            }
        }catch(DMLException e){
            system.debug('The batch jobs were not added to the queue successfully: '+e);
        }    
    }    
    global void finish(Database.BatchableContext BC){
    
    }
}

 

 

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

hitesh90hitesh90

Hi,

 

You have to use inner SOQL query for your requirement to fulfill.

 

Try to use below updated batch class code.

 

Batch class:

global class RollUpSummaryMaps implements Database.Batchable<sObject>{    
    list<OpportunityLineItem> opporto =new List<OpportunityLineItem>();
    list<Opportunity> optylist =new List<Opportunity>();
    Opportunity optyupdate= new Opportunity();
    public Double curecy=0;    
    global Database.QueryLocator start(Database.BatchableContext BC){
        // Access initialState here
        String lineitem='SELECT id,ListPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem';        
        // Database.ge
        System.debug('The Query@@@@@@@@'+lineitem);
        return Database.getQueryLocator(lineitem);
    }
    
    global void execute(Database.BatchableContext BC,List<Sobject> batch){    
        Map<ID, Opportunity> iMap = new Map<ID, Opportunity>([select Id,testvantage__Updatevalue__c,(select id,UnitPrice from opportunityLineitems) From Opportunity ]);
        //Map<ID, OpportunityLineItem> mapOppty = new Map<ID, OpportunityLineItem>([SELECT Id,UnitPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem where OpportunityId IN :iMap.keySet()]);
        Integer optylinesize=iMap.size();
        System.debug('NEWDEBUGOptyLineItem @@@@@@@@@@@@@@############'+optylinesize);
        Integer optysize=mapOppty.size();
        System.debug('NEWDEBUGOptyList @@@@@@@@@@@@@@############'+optysize);
        try{
            if(iMap != null && iMap.size()>0){
                for(Opportunity opp: iMap.values){
                    for(OpportunityLineItem lineitem:opp.opportunityLineitems){
                        if(lineitem.UnitPrice != null){
                            opp.testvantage__Updatevalue__c += lineitem.UnitPrice;
                        }
                    }
                    OptyList.add(opp);
                }
                if(OptyList.size()>0){
                    update OptyList;
                }
            }
        }catch(DMLException e){
            system.debug('The batch jobs were not added to the queue successfully: '+e);
        }    
    }    
    global void finish(Database.BatchableContext BC){
    
    }
}

 

 

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
sailersailer
Hi Hitesh,

I am getting the following errro

Error: Compile Error: Initial term of field expression must be a concrete SObject: MAP<Id,Opportunity> at line 23 column 38

Thanks for the help
hitesh90hitesh90

see below code.

 

global class RollUpSummaryMaps implements Database.Batchable<sObject>{    
    list<OpportunityLineItem> opporto =new List<OpportunityLineItem>();
    list<Opportunity> optylist =new List<Opportunity>();
    Opportunity optyupdate= new Opportunity();
    public Double curecy=0;    
    global Database.QueryLocator start(Database.BatchableContext BC){
        // Access initialState here
        String lineitem='SELECT id,ListPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem';        
        // Database.ge
        System.debug('The Query@@@@@@@@'+lineitem);
        return Database.getQueryLocator(lineitem);
    }
    
    global void execute(Database.BatchableContext BC,List<Sobject> batch){    
        Map<ID, Opportunity> iMap = new Map<ID, Opportunity>([select Id,testvantage__Updatevalue__c,(select id,UnitPrice from opportunityLineitems) From Opportunity ]);
        //Map<ID, OpportunityLineItem> mapOppty = new Map<ID, OpportunityLineItem>([SELECT Id,UnitPrice, OpportunityId, PricebookEntryId FROM OpportunityLineItem where OpportunityId IN :iMap.keySet()]);
        Integer optylinesize=iMap.size();
        System.debug('NEWDEBUGOptyLineItem @@@@@@@@@@@@@@############'+optylinesize);
        Integer optysize=mapOppty.size();
        System.debug('NEWDEBUGOptyList @@@@@@@@@@@@@@############'+optysize);
        try{
            if(iMap != null && iMap.size()>0){
                for(Opportunity opp: iMap.values()){
                    for(OpportunityLineItem lineitem:opp.opportunityLineitems){
                        if(lineitem.UnitPrice != null){
                            opp.testvantage__Updatevalue__c += lineitem.UnitPrice;
                        }
                    }
                    OptyList.add(opp);
                }
                if(OptyList.size()>0){
                    update OptyList;
                }
            }
        }catch(DMLException e){
            system.debug('The batch jobs were not added to the queue successfully: '+e);
        }    
    }    
    global void finish(Database.BatchableContext BC){
    
    }
}