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 

Im trying to copy picklist values to comma separated values. but I am not getting proper output please help me

I have picklist field "Funding" on Quote object and text field "Funding" on Opportunity object. I want to copy the picklist values from all Quotes related to the Opportunity and display it on "Funding" field on Opportunity separated by comma. I tried this code but not working, 

I need to use string join instead of split. please help

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(';'));
        }
            System.debug('ccc'+systems);
    }
    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());
    }
        }       
    }

}
John Pipkin 14John Pipkin 14
@viyda

Without knowing exactly what you are seeing, here is my guess on what you are trying to accomplish. I added some notes on best practices like avoiding SOQL and DML inside for loops. 
 
global class UpdateFunding implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([SELECT Id,StageName, Funding__c FROM opportunity where StageName='Closed Won']);
    }

    global void execute(Database.BatchableContext context, List<Opportunity> batch){ // changed SObject to Opportunity
//        Set<Id> Opportuniyids = new Set<Id>();
//
//        for (sObject oppy : batch) {
//            Opportuniyids.add(oppy.Id);
//        }
        // ^^ that is not needed
        updatequote(batch); // pass in the List<Opportunity>
    }

    global void finish(Database.BatchableContext context) {}

    public void updatequote(List<Opportunity> opportunities){ // changed intake to accept List<Opportunity>
        // system.debug('opportuiy ids'+oppyids);
//        List<opportunity> opportunities=[SELECT Id ,Funding__c FROM opportunity WHERE Id IN :oppyids];
        // ^^ not needed as you already queried the opps in the start() method

        // avoid using SOQL inside for loops. use lists and maps
        Map<Id, List<Quote__c>> mapQuotes = new Map<Id, List<Quote__c>>(); // this will hold a list a Quotes for each opportunity id
        for (Quote__c oppObj : [SELECT Id, Funding__c, opportunity__c FROM Quote__c WHERE opportunity__c in: opportunities]) {
           if(!mapQuotes.containsKey(oppObj.Opportunity__c)) {
               mapQuotes.put(oppObj.Opportunity__c, new LIst<Quote__c>());
           }
            mapQuotes.get(oppObj.Opportunity__c).add(oppObj);
        }


        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(';'));
//                }
//                System.debug('ccc'+systems);
//            }
            // ^^ avoid SOQL in for loops, moved to above
            Quote__c[] quotes = mapQuotes.get(opp.Id);
            quotes = quotes == null ? new List<Quote__c>() : quotes; // check for nulls
            for(Quote__c q : quotes) {
                if(String.isNotBlank(q.Funding__c)) {
                    systems.addAll(q.Funding__c); // picklists don't need String.split. Only multiselect picklists need it
                }
                System.debug('ccc'+systems);
            }
//            String accountSystems = '';
//            for (String value : systems) {
//                accountSystems += value + ';';
//            }
//            accountSystems = accountSystems.removeEnd(';');
            // ^^^ its better to use String.join
            opp.Funding__c = String.join(systems, ';'); 
//            try {
//                update new Opportunity(Id = opp.id,Funding__c  = accountSystems);
//            } catch (Exception e) {
//                System.debug('Exception: ' + e.getMessage());
//            }
            // ^^^ avoid DML inside for loops
        }

        update opportunities; // call DML outside of for loop
    }

}