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
Jerry ClifftJerry Clifft 

Need help with trigger that copies from opportunitylineitems to a generic field.

I have a rich text field on the opportunity that I need to populate with various peices of data on the opportunity. I have two main issues:

 

1. I am able to do this, but with only a single data type at a time. I can not seem to figure out how to combine all three lists into a single list.  Here is what I have so far.

2. My \n is not making the next line item go on to a new line down.

 

trigger OpportunityProuctCopytoHiddenField on Opportunity (before insert, before update) {
for(Opportunity t:trigger.new)
      {
        List<String> NewList= new List<String>();
        List<Decimal> NewList2= new List<Decimal>();
        List<Datetime> NewList3= new List<Datetime>();
        if (t.HasOpportunityLineItem == true)
        {
             for(OpportunityLineItem OpLine: [SELECT CreatedBy.Name, Quantity, TotalPrice, LastModifiedDate, PriceBookEntry.Name, UnitPrice, ID FROM OpportunityLineItem WHERE OpportunityId =:t.Id] )
            {
                NewList.add(OpLine.PriceBookEntry.Name);
                NewList.add(' has been authorized by ');
                NewList2.add(OpLine.Quantity);
                NewList.add(OpLine.CreatedBy.Name);
                NewList.add('and the transation has been logged into the record id: ');
                NewList.add(OpLine.ID);
                NewList.add(' on');
                NewList3.add(OpLine.LastModifiedDate);
                            }
            for(String c : NewList){
            t.Hidden_Products__c = t.Hidden_Products__c + c + '\n';
            }
        }
    }
}

 

And i am pretty sure what I am missing is somewhere around:

            for(String c : NewList){
            t.Hidden_Products__c = t.Hidden_Products__c + c + '\n';

 

Kunal01Kunal01

Hi,

 

Try Using the below code.. I have modified it... I think you were missing the escape Character..

 

t.Hidden_Products__c = t.Hidden_Products__c + c + ''\'n';

 

 

Thanks,

KR

Jerry ClifftJerry Clifft

Thanks for the reply, but that did not work, upon saving the trigger I recevied the following error:

 

ErrorError: Compile Error: line 21:64 no viable alternative at character '\' at line 21 column 64

Jerry ClifftJerry Clifft

Ok, so I found I can reslove on of the problems with by replacing:

 

t.Hidden_Products__c = t.Hidden_Products__c + c + '\n';

 

with

 

t.Hidden_Products__c = t.Hidden_Products__c + c + '<br>';

 

Now only one major issue remain, how do I concatenate NewList, NewList2 and NewList3 into a single string that will output to my hidden_field__C ?

 

Also, just curious, how do I clear the old data out of the field prior to inserting this new string?

 

Thanks