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
Parmeshwar Bhore 17Parmeshwar Bhore 17 

Public class ArtifactsHandler{ public void beforeInsert(list<Product_Artifact_Requirement__c> newList){ list<id> productList = new list<id>(); for(Product_Artifact_Requirement__c prod : newList){

   map<id,string> prodArtifectsmap = new map<id,string>();
Repalce String AS List  in a map i have probleam in the for loop .. how can i do that. what is correct solution for that.please let me know..
Sangeet kaseraSangeet kasera
Hi Parmeshwar,

As per my understanding for your question-
Below is a few line of code to understand Map with List<String>

   Map<String, List<String>> MapData= new Map<String, List<String>>();
        String objectName;
        String records;
        for(Account AccData : [Select Id, Name ,Phone from Account where Id =: someIds ]){        
            List<String> lstData = new List<String>();
            if(MapData.containsKey(AccData.Name)){
                lstData = MapData.get(AccData.Name);
            }
            lstData.add(AccData);
            MapData.put(AccData.Name,lstData);
            System.debug('lstData=====' + lstData);
        }
        System.debug('MapData' + MapData);

If you find this information helpful. Please mark this answer Best. It may help others in the community. Thank You

Regards,
Sangeet
Parmeshwar Bhore 17Parmeshwar Bhore 17
Below is my complete code i use u r logic but i have again error. Public class ArtifactsHandler{ public void beforeInsert(list newList){ list productList = new list(); for(Product_Artifact_Requirement__c prod : newList){ productList.add(prod.Product__c); } System.debug('productList'+productList); if(productList.size() > 0){ map prodArtifectsmap = new map(); //replace String as list // List Artlist = [Select name from Product_Artifect where ProductId=: recordId ]; // return Artlist; list prodArtifects = [select id,Product__c,Artifects__c from Product_Artifect__c where Product__c IN : productList]; String str=''; for(Product_Artifect__c pr : prodArtifects ){ prodArtifectsmap.put(pr.Product__c,pr.Artifects__c ); // for(Product_Artifect__c pa : newList){ str = str+pr.Artifects__c; } for(Product_Artifact_Requirement__c par : newList){ if(prodArtifectsmap.containsKey(par.Product__c)){ par.Artifacts__c=str; System.debug('asad'+par.Artifacts__c); } // update newList; } } } }
Sangeet kaseraSangeet kasera

Hi Parmeshwar,

There are lots of mistake in your code

Please find below code i have made some changes and use it  accordinly
Public class ArtifactsHandler{ 
    public void beforeInsert(list<String> newList){ 
        list<String> productList = new list<String>(); 
        for(Product_Artifact_Requirement__c prod : newList){ 
            productList.add(prod.Product__c); 
        }
        System.debug('productList'+productList); 
        if(productList.size() > 0){ 
            Map<String,String> prodArtifectsmap = new Map<String,String>();
            //replace String as list //
            // No need to use double backslash both side of comment for single line comment you can use only double backslash at the starting of comment
            //What is recordId
            List<String> Artlist = [Select name from Product_Artifect where ProductId=: recordId ]; // 
            //Method is not return type no need for return type directly you can use it in loop
            return Artlist; 
            List<String> prodArtifects = [select id,Product__c,Artifects__c from Product_Artifect__c where Product__c IN : productList]; 
            String str=''; 
            for(Product_Artifect__c pr : prodArtifects ){ 
                prodArtifectsmap.put(pr.Product__c,pr.Artifects__c ); 
                // for multiple line comment use /* ---------*/ Like Below
                /*for(Product_Artifect__c pa : newList){ 
                str = str+pr.Artifects__c;
                }  */
                //use str = str+pr.Artifects__c; this outside of loop
                str = str+pr.Artifects__c;
                for(Product_Artifact_Requirement__c par : newList){ 
                    if(prodArtifectsmap.containsKey(par.Product__c)){  
                        par.Artifacts__c=str; 
                        System.debug('asad'+par.Artifacts__c); 
                    } //
                    update newList; 
                }
            }
        }
    }
}

Regards,
Sangeet

Sangeet kaseraSangeet kasera
Please choose it as best if it solved your question.

Regards,
Sangeet