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
Vidya H 4Vidya H 4 

Please help me to write batch classs

I have Created a field called Funding on Quote with values - Partial , Full and NA .
and i Created a field called Funding on Opportunity.
Write a batch class to rollup the unique funding field values from Quote to Opportunity if Opportunity stage is closed/won. Ex: if an opportunity has 6 quotes with funding Full, Full, NA, Partial ,NA, Partial then field on opportunity should be in format - Partial,Full,NA.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below Apex Batch class.
 
global class UpdateFunding implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id,StageName FROM opportunity where StageName='Closed Won']);
    }

    global void execute(Database.BatchableContext context, List<sObject> batch){
        Set<Id> Opportuniyids = new Set<Id>();

        for (sObject oppy : batch) {
            Opportuniyids.add(oppy.Id);
        }

        updatequote(Opportuniyids);
    }

    global void finish(Database.BatchableContext context) {}

    public void updatequote(set<id> oppyids){
        system.debug('opportuiy ids'+oppyids);
        List<opportunity> opportunities=[SELECT Id ,Funding__c FROM opportunity WHERE Id IN :oppyids];
        for(Opportunity opp:opportunities) {
       Set<String> systems = new Set<String>();
        for (Quote__c oppObj : [SELECT Id, Funding__c, opportunity__c FROM Quote__c WHERE opportunity__c = :opp.id]) {
        if (String.isNotBlank(oppObj.Funding__c)) {
            systems.addAll(oppObj.Funding__c.split(';'));
        }
    }
    String accountSystems = '';
    for (String value : systems) {
        accountSystems += value + ';';
    }
    accountSystems = accountSystems.removeEnd(';');
    try {
        update new Opportunity(Id = opp.id,Funding__c  = accountSystems);
    } catch (Exception e) {
        System.debug('Exception: ' + e.getMessage());
    }
        }       
    }

}

If this solution helps, Please mark it as best answer.

Thanks,
 
Vidya H 4Vidya H 4
@Sai Praveen  code is not working properly, please help me to solve this
we need to populate Opportunity's funding field in this format -->for example if that opportunity has 4 quotes like Full,partial,NA,full then  
funding field on opportunity should be in format - Partial,Full,NA
and other condition is, the above scenario should execute only if Opportunity's stage is closed/won..
 
Vidya H 4Vidya H 4
Hi @Sai Praveen 
u have used for loop inside for loop in line number 21 and 23 could you please modify it?