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
The new LearnerThe new Learner 

How to filter long text area in test class

Hi Experts,

I have small requirement where i need to filter a long text area field called Desc__c which i am not able to do that can anyone help me please.

OrderItem oli1 = new OrderItem ();
        oli1.Orderid = order.Id;
        oli1.Desc__c= 'Test OLI 1'; 
      insert oli1;


 OrderItem oli2 = [SELECT Id, Orderid
                    FROM OrderItem WHERE  Desc__c = 'Test OLI 1' LIMIT 1];

In this filter condition its throwing an error can anyone help me please thanks in adavance
Best Answer chosen by The new Learner
Bhargavi TunuguntlaBhargavi Tunuguntla
Then the first solution might work:

All the orderItems with 'Test OLI 1' in desc__c will be added to the list 'oli2'.
List<OrderItem > oli2= new List<OrderItem >(); 
for(OrderItem  oli : [SELECT Id, Orderid FROM OrderItem) { 
if(oli.Desc__c.contains('Test OLI 1')) { 
oli2.add(oli);
} 
}

All Answers

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi
We cannot apply filter on long text area. So my suggestion is to go with the below solution:
List<OrderItem > oli2= new List<OrderItem >(); 
for(OrderItem  oli : [SELECT Id, Orderid FROM OrderItem) { 
if(oli.Desc__c.contains('Test OLI 1')) { 
oli2.add(oli);
} 
}
If you only need first record (LIMIT 1):
OrderItem  oli2= new OrderItem (); 
for(OrderItem  oli : [SELECT Id, Orderid FROM OrderItem) { 
if(oli.Desc__c.contains('est OLI 1')) { 
 oli2=oli;
break;
} 
​​​​​​​}

Hope this will be helpful;
Thanks
​​​​​​​Bhargavi.
The new LearnerThe new Learner
Hi Bhargavi,

If i have multiple records then do i need to do that , can you help me please
Bhargavi TunuguntlaBhargavi Tunuguntla
Then the first solution might work:

All the orderItems with 'Test OLI 1' in desc__c will be added to the list 'oli2'.
List<OrderItem > oli2= new List<OrderItem >(); 
for(OrderItem  oli : [SELECT Id, Orderid FROM OrderItem) { 
if(oli.Desc__c.contains('Test OLI 1')) { 
oli2.add(oli);
} 
}
This was selected as the best answer
The new LearnerThe new Learner
hi Bhargavi,

Thanks alot , in case of formula field how do i need to approch for this kind of sceario pls
Bhargavi TunuguntlaBhargavi Tunuguntla
We cannot reference long text area field in formulae field. You can either use workflows,process builders to get the values and update the related fields.

If you only need to update a formulae field then my choice is to :

Use a workflow which updates a text area field with the value of long text area field.We can use the text area field in the formulae field.
This might be the solution your expecting for but we dont have a choice to use long text area fields in formulae.

Thanks
Bhargavi.