• Drew Gehman 18
  • NEWBIE
  • 20 Points
  • Member since 2016
  • IT Specialist Rose-Hulman Ventures
  • Rose-Hulman Institute of Technology


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
I am trying to return the string found between parentheses in a field.  Something like this:
String s1 = 'My favorite color is xYellowy';
String s2 = s1.substringBetween('x','y');
System.assertEquals('Yellow', s2);
If I substitute square brackets for x and y everything works well. When using traditional parentheses, I thought I would need to escape the parentheses, like this:
String s1 = '(Yellow)';
String s2 = s1.substringBetween('\\(','\\)');
System.assertEquals( 'Yellow', s2);
But that didn't work either.  I bet I am 90% there, but I am missing something, 

 
Thanks to help from this forum, I created a trigger to populate task records with information from the associated account object. Because of this trigger, I now have the "account number" of the account tied to the task (via WHATID) included in the record as field "AccountBannerID". I can now export those tasks into another system, and relate them back to the account via the account number.  Thank you to @RatherGeeky, @thecrmninja and @EDLJames for assistance.

Open to suggestions, and any optimizations!

First the trigger:
trigger TaskRelatedToAcc on Task (before insert, before update)
{ 
// Created by DLG 3-31-16 
// Goal: Find the 'Account Number' of the account that a task is related to 
// and update the activity custom field 'AccountBannerID' with that value 
// If the task is related to an Account, query to find out the unique Account ID 
// Relation to an account is determined by the first three digits of the WhatID being 001
// Create collection of tasks that are related to an account (where the account is listed only once) 
Set<Id> AccIds = new Set<Id>(); 
for(Task t : trigger.new)
    {String wId = t.WhatId; 
        if(wId!=null && wId.startsWith('001') && !AccIds.contains(t.WhatId))
            {AccIds.add(t.WhatId);}
    } 
// Pull in account ids and related fields to populate task record 
List<Account> taskAccs = [Select Id, AccountNumber from Account where Id in :AccIds]; 
Map<Id, Account> accMap = new Map<Id, Account>(); 
for(Account o : taskAccs)
    {accMap.put(o.Id,o);} 
// Update the custom activity field 'Banner ID of Account' with the 'Account Number' field from Accounts object 
for(Task t : trigger.new)
    {String wId = t.WhatId; 
        if(wId!=null && wId.startswith('001'))
            { Account thisAcc = accMap.get(t.WhatId); 
                if(thisAcc!=null)
                    {t.AccountBannerID__c = thisAcc.AccountNumber;
                        if(t.AccountBannerID__c==null){t.AccountBannerID__c='BannerID Not Available';} /*if BannerID is not in Account Number let it be known */
                    }
            }
    }
}

Now the test 
@isTest
private class TaskRelatedToACCTriggerTestClass { 
static testMethod void myUnitTest() {
// Run test of code
//HERE, WE CREATE AN ACCOUNT RECORD PUTTING A VALUE IN THE FIELD THAT YOU CALL IN YOUR TRIGGER
//WE WANT TO TEST THE COMPLETE FUNCTIONALITY OF YOUR TRIGGER SO IT IS IMPORTANT TO VALIDATE, VIA THE TEST CLASS
//THAT YOUR TRIGGER ACHIEVES THE DESIRED RESULTS.. ...UPDATING A FIELD ON A TASK WITH A VALUE FROM AN ACCOUNT
Account aDS = new Account(name='SomeAccount', AccountNumber='123 or text');
insert aDS; 
//NOW, CREATE A TASK ASSIGNED TO THE ACCOUNT
Task t1 = new Task(subject='test', WhatId = aDS.id, description = 'test description'); 
insert t1; 
//NOW, RUN AN ASSERT CALL TO MAKE SURE THE CUSTOM FIELD ON THE TASK HAS THE VALUE FROM THE ACCOUNT
//THIS IS WHAT TESTS THE FUNCTIONALITY OF THE TRIGGER
Task t = [Select AccountBannerID__c from Task where id = :t1.id];
system.AssertEquals('123 or text', t.AccountBannerID__c);
}
}


 
Bet this is easy for someone, but I am so raw I am just cut and pasting... I have created my first trigger which simply takes the account number of an account, and assigns it to a custom task field AccountBannerID__C. I have tested it in my sandbox and it works. I am now creating my test. 
Here is the test code (I think the trigger code in not applicable though I can load it if necessary)

Thank you for your assistance, and I will try to pay it forward by posting the trigger and the successful test to the forums

It doesn't like this line 
String t = [Select AccountBannerID__c from Task where id = :t1.id];
I get this Compile Error: Illegal assignment from List<Task> to String

@isTest
private class TaskRelatedToACCTriggerTestClass { 
static testMethod void myUnitTest() {
// Run test of code
//HERE, WE CREATE AN ACCOUNT RECORD PUTTING A VALUE IN THE FIELD THAT YOU CALL IN YOUR TRIGGER
//WE WANT TO TEST THE COMPLETE FUNCTIONALITY OF YOUR TRIGGER SO IT IS IMPORTANT TO VALIDATE, VIA THE TEST CLASS
//THAT YOUR TRIGGER ACHIEVES THE DESIRED RESULTS.. ...UPDATING A FIELD ON A TASK WITH A VALUE FROM AN ACCOUNT
Account aDS = new Account(name='jbailer', AccountNumber='123');
insert aDS; 
//NOW, CREATE A TASK ASSIGNED TO THE ACCOUNT
Task t1 = new Task(subject='test', WhatId = aDS.id, description = 'test description'); 
insert t1; 
//NOW, THE CRITICAL PART, WE WILL RUN AN ASSERT CALL TO MAKE SURE THAT THE CUSTOM FIELD ON THE TASK HAS THE VALUE FROM THE OPP
//THIS IS WHAT TESTS THE FUNCTIONALITY OF YOUR TRIGGER

String t = [Select AccountBannerID__c from Task where id = :t1.id];
system.AssertEquals('123', t);

}
}
I am trying to return the string found between parentheses in a field.  Something like this:
String s1 = 'My favorite color is xYellowy';
String s2 = s1.substringBetween('x','y');
System.assertEquals('Yellow', s2);
If I substitute square brackets for x and y everything works well. When using traditional parentheses, I thought I would need to escape the parentheses, like this:
String s1 = '(Yellow)';
String s2 = s1.substringBetween('\\(','\\)');
System.assertEquals( 'Yellow', s2);
But that didn't work either.  I bet I am 90% there, but I am missing something,