+ Start a Discussion
Ross Hopkins 8Ross Hopkins 8 

Create Records from comma separated values stored in a text field

Hi all, 
I have a text field against the Opportunity object that sometimes contains data such as:

0123456,0234567,0345678 etc

There could sometimes only be one value and on other occasions there might be 10 or so.

When this field is populated I'd like to use these IDs to lookup a Product record (via a custom SKU field) and subsequently create an OpportunityProduct record linked to the Opportunity.

What is the best approach here? I'm not an Apex developer although I have written simple triggers after gaining advice and examples from the community previously. I started to experiment in Flow, but stumbled early on when I couldn't see a way to split the comma seperated text value

Looking forward to your advice.
Raj VakatiRaj Vakati
use this  and add the other fields
trigger OpportunityProduct on Opportunity (After insert) {
    List<Opportunity> opp = new List<Opportunity>();
    List<OpportunityLineItem> lines = new List<OpportunityLineItem>();
		for(Opportunity o:Trigger.New){
        List<String> prodIs = o.Name.splite(',');
		for(String s : prodIs){
            lines.add(new OpportunityLineItem( OpportunityId=o.Id,ProductId =s , Quantity=1));//add other fields if you wnat to copy from the trigger
         insert lines;


Ross Hopkins 8Ross Hopkins 8
Hi Raj,

Thanks for this! I can follow most of the above - I'd rather understand it than just copy and paste into my org ;-)

On lines 2 and 4, this isn't creating a new Opportunity it's just referencing the Opportunity that fired this trigger - is that correct?

Secondly, line 7 makes sense to me for creating the OpportunityLineItem record but I think there's a step missing before that, because I'd need to lookup the Product.ID with the custom SKU field before using that ID in the OpportunityLineItem record addition. How would I do that?

Ross Hopkins 8Ross Hopkins 8
I've since made some progress with this, but I'm running into an error on line 47 of the below - INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY. I've checked user permissions and I believe they sare fine. Any ideas?
trigger Shopify_Add_OpportunityProduct_to_Rental_Enquiry_Opportunity on Opportunity (before insert) {
    List<Opportunity> o = new List<Opportunity>();
    List<OpportunityLineItem> op = new List<OpportunityLineItem>(); //op = OpportunityProduct

        for(Opportunity o:Trigger.New){
            if(o.shopify_add_line_items__c == TRUE){			
                List<String> commaSeperatedAssetNumbers = o.Oppty_Asset_Number__c.split(','); 

                    for(String assetNumber : commaSeperatedAssetNumbers){			

                        // Use the Asset Number to locate a Product2.ProductId
                        Product2 matchedProductToAsset = [SELECT Name, 
                                                          FROM Product2
                                                          WHERE (wk_shopify__SKU__c = :assetNumber)
                                                          LIMIT 1];
                        // Insert a row into the OpportunityProduct/OpportunityLineItem 
                        op.add(new OpportunityLineItem(
                                                          OpportunityId = o.Id,
                                                          Product2Id = matchedProductToAsset.Id ,
                                                          Quantity = 1
            insert op;
            o.shopify_add_line_items__c = FALSE;