+ Start a Discussion
Shreya SalesforceShreya Salesforce 

How i can show list data into a custom field of object??

Hii...I am new in Salesforce.Please help me to solve this.

I have a custom field 'NS_ID__c'(Text) on Product and Error_Message__c(Text Area) is custom field on Opportunity. These products are in OpportunityLineItem.
My requirement is if NS_ID__c is blank then these Product Names show on 'Error_Message__c' on Opportunity.
I get a list 'proid' which contains product Names...but i stuck, how to show this list in 'Error_Message__c' field.

 
List<string> proid = new List<string>(); 
Map<Id,List<String>> prodMap= new Map<Id,List<String>>();
List<Product2> prodList= [SELECT Id,Name,NS_ID__c FROM Product2 WHERE Id=:o.Product2Id];
               
if(prodList.size() > 0)
{
   Product2 prd = prodList[0]; 
   string pnsid = prd.NS_ID__c;
   System.debug('pnsid : '+pnsid);
   if(pnsid == null)
   {
       proid.add(prd.Name);
       System.debug('final product List : '+proid);
       string allstring = string.join(proid,',');
       for(Product2 pro: prodList)
       {
           prodMap.put(o.OpportunityId,proid);
           System.debug('prodMap==' +prodMap);
       }
        prodMap.get(Id).Error_Message__c = allstring;
        update prodMap.values();
    }
}
Best Answer chosen by Shreya Salesforce
BALAJI CHBALAJI CH
Hi Shreya,

Can you replace below last two lines
prodMap.get(Id).Error_Message__c = allstring;
update prodMap.values();

with below updated lines
for(Id oppId : prodMap.keyset())
{
    Opportunity Opp = new Opportunity();
    Opp.Id = oppId;
    Opp.Error_Message__c = prodMap.get(oppId);
    
    OppList.add(Opp); //Declare OppList as List<Opportunity> before method starting
}

update OppList;

Let us know if you face any issues.


Best Regards,
​​​​​​​BALAJI