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
GMASJGMASJ 

Remove null string and align string in apex code

Hi, 

In this below code. 
 
public class OpportuntiyProductPurchased{
    
    @InvocableMethod
    public static void OpplineProdPurch(List<ID> OppId){
    String Str;

    list<opportunitylineitem> oplnlst = new list<opportunitylineitem>([select id, name,f_Entitlement_Product_Description__c from opportunitylineitem where opportunityid = :OppId]);
    list<opportunity> opplst = new list<opportunity>();

    for(opportunity opp :[select id, Products_Purchased__c from opportunity where id = :OppId]){
    for(opportunitylineitem opl : oplnlst){
       system.debug(opl.f_Entitlement_Product_Description__c);  
      Str += opl.f_Entitlement_Product_Description__c;
        If (Str <> null){
         Str = Str;
        } 
    }
     opp.Products_Purchased__c = Str; 
      opplst.add(opp);
    }

    System.debug(Str);

    if(opplst.size() > 0){
      update opplst;
    }       
  } 
}
Values are assigned to string and that string is getting updated to textarea field in opportunity. 

Problem I have here is null and string alignment is one next to another Please suggest me for how to remove null and align string values one below another.

Thanks
Sudhir
Best Answer chosen by GMASJ
SarvaniSarvani
Hi Sudhir,

Give a try with below code:
public class OpportuntiyProductPurchased{
    
    @InvocableMethod
    public static void OpplineProdPurch(List<ID> OppId){
    String[] stringList = new String[0];

    list<opportunitylineitem> oplnlst = new list<opportunitylineitem>([select id, name,f_Entitlement_Product_Description__c from opportunitylineitem where opportunityid = :OppId]);
    list<opportunity> opplst = new list<opportunity>();

    for(opportunity opp :[select id, Products_Purchased__c from opportunity where id = :OppId]){
    for(opportunitylineitem opl : oplnlst){
     
        If (opl.f_Entitlement_Product_Description__c!=NULL){
            stringlist.add(opl.f_Entitlement_Product_Description__c);
        
        } 
    }
//Using join method to concatenate f_Entitlement_Product_Description__c field of all the opp line item records
     String allproducts = String.join(stringList, '\\n'); 
     opp.Products_Purchased__c = allproducts.remove('null');
     opplst.add(opp);
    }

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

Hope this helps! Please mark as best if it does.

Thanks

All Answers

SarvaniSarvani
Hi Sudhir,

Give a try with below code:
public class OpportuntiyProductPurchased{
    
    @InvocableMethod
    public static void OpplineProdPurch(List<ID> OppId){
    String[] stringList = new String[0];

    list<opportunitylineitem> oplnlst = new list<opportunitylineitem>([select id, name,f_Entitlement_Product_Description__c from opportunitylineitem where opportunityid = :OppId]);
    list<opportunity> opplst = new list<opportunity>();

    for(opportunity opp :[select id, Products_Purchased__c from opportunity where id = :OppId]){
    for(opportunitylineitem opl : oplnlst){
     
        If (opl.f_Entitlement_Product_Description__c!=NULL){
            stringlist.add(opl.f_Entitlement_Product_Description__c);
        
        } 
    }
//Using join method to concatenate f_Entitlement_Product_Description__c field of all the opp line item records
     String allproducts = String.join(stringList, '\\n'); 
     opp.Products_Purchased__c = allproducts.remove('null');
     opplst.add(opp);
    }

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

Hope this helps! Please mark as best if it does.

Thanks
This was selected as the best answer
GMASJGMASJ
Thanks Savani this helped to remove null can you help me how to align text one below another I am adding this inside a text area field right now it is getting included one after another. 

Thanks
Sudhir
SarvaniSarvani
Hi Sudhir,

Can you please try replacing '\\n' in line 19 of my code with '\n' (single slash n in quotes).

Thanks,
Sarvani
GMASJGMASJ
It is working now Thank you very much Sarvani this is working now. I will markt this case as answered.