• pixel
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 9
    Replies

I'm trying to verify a Time (not DateTime) value entered in a text string as being valid that will be appended to a Date value in a trigger.

 

The pattern I think I need is (([0][1-9])|([1][0-2]))([U+003A])([0-5][0-9])([\b])([AP])([M]). Apex doesn't seem to like the [\b] which is there as a means to verify that there's a blank space. It also doesn't seem to like a [\s] in it's place either. I'm not entirely certain that either of those is exactly what's needed to designate that I'm looking for a blank space. Could anyone tell me what the most appropriate designator is to use is? I'd also like to verify that [U+003A] is what I need to use to search for a unicode colon.

 

With the RegEx portion out of the way, what's the best way to set up a pattern match of this kind? I've seen it done where each group is matched separately and others where they're entered as the entire RegEx pattern.

 

I've been trying get code similar to the following to work which keeps giving me a fatal error, even when I try debugging it by entering a static value for opp.Event_Start_Time__c:

 

 Pattern TimePttrn = Pattern.compile('(([1][0-2])|([0][1-9]))[U+003A]([0-5][0-9])[\b]([AP])([M])'); // hh:mm aaa'
 if (opp.Event_Start_Time__c != null){
    Matcher m = TimePttrn.matcher(opp.Event_Start_Time__c);
    boolean hasMatch = m.matches();
    system.assert(hasMatch == true);
    strtTm = m.group(0); 
    }
 
Is there something I've missed or am approaching from the wrong direction? Any helpful input greatly appreciated.
 
 
  • February 14, 2013
  • Like
  • 0

How can I take the results of a for loop, saved in decimal object, and put them into a field of a custom sObject? The field also holds decimal values. Below is an example of what I'd like to do that doesn't work. I can't assign the value of a decimal object to an sObject or equate the two directly. So how do I do it by reference? After trying countless methods, I'm at a loss.

 

// Code below is enclosed in larger for loop

for(List<Time__c> BrnH:BrnH_Tmp){
tempSum += BrnH.A_Hours__c ;
}
RsrcHrsLst.B_Hours__c = valueOf(tmpSum);

 

I should note that the lists in the for loop are Time__c Lists while the one outside of it is a ResrceTrack__c List, so they're not the same kinds of Lists even though A_Hours__c are related to B_Hours__c. I just can 't seem to find a direct way to get the sum of the A_Hours__c (Time__c) into a B_Hours__c (ResrceTrack__c) field of a RescrceTrack object after I've located and summed the A_Hours records in my trigger!

 

As one possibility, Is there a method I could use to "put" the values of tempSum into B_Hours__c? Do I need to create a map between the pairs of variables to do that? I'm at my wits end trying to do something that seems as though it should be simple.

 

- pixel

 

 

  • November 09, 2012
  • Like
  • 0

I have two different custom sObjects that have custom decimal fields. After running a for loop to sum the values of the fields in one of the sObjects, I need to transfer the sum to a field in the other sObject. How do I do that? I've tried several different ways that don't work. I should add that this is inside of a larger for loop in case that makes any difference.

 

Here's the general psuedo logic

List <TimeOject__c> BHrs;
List<RscrcTrackObject__c> Rscrc;
Decimal tempSum = 0.00

Outer forLoop{ for(TimeOject__c bh:BHrsTmp) { tempSum += bh.A_Hours__c ; } BHrs[0].A_Hours__c = tempSum; // The above works because I'm only using one element of the list/array // Now I want to assign tempSum to the hours in a RscrcObject Field, but can't set one equal to the other Rscrc.B_Hours__c = BHrs[0].A_Hours__c; // The above doesn't work because they're different kinds of sObjects. // trying to assign Rscrc.B_Hours__c = tempSum didn't work either // Reset tempSum for next iteration of for loop tempSum = 0.00 update Rscrc; } // Close of outer for loop

 

So how do I assign the results of the inner for loop that calculated the sum of A_Hours__c for that instance of BHrsTmp (a TimeObject) to the B_Hours__c in the current instance of Rscrc which is a different type of custom sObject (a RescrcTrackObject) even though both of the Hours fields are decimal?

 

Do I need to create a map and do a put/get or is there hopefully a simpler solution?

 

 

- pixel

  • November 08, 2012
  • Like
  • 0

I have an sObject with a decimal field that I'm having problems creating an APEX loop that will sum the values contained in the instances of that field. The code for it is nested inside another loop. Below is some psuedo code for what's relevent.

 

Initially, before any of the loops, I created a List called Brn_Hrs. Beginning at (3) below, you'll see there's a query done to create a Temp list of records. After that, I set Brn_Hrs[0].Hours__c to (0.00) because I only want a total or sum as output from the loop. I also do not want to fill each element of the array as it goes through each iteration of the loop; both this one and the larger one it's nested inside of.

 

Finally we come to the new for loop I'm having difficulty with when we reach (4). 

 

List<Time__c> Brn_Hrs;
// (3) Create Temp list of Hours records called "BrnH_Tmp" List<Time__c> BrnH_Tmp = [SELECT Hours__c FROM Time__c WHERE Time__c.Resource__c = :rTime.Resource__c AND Time__c.OppLnk__c = :rTime.OppLnk__c]; // Below need to be reset to zero for each iteration of larger loop Brn_Hrs[0].Hours__c = (0.00); count = 0; // (4) Sum Total Burnt Hours for this combo do { Brn_Hrs[0].Hours__c += BrnH_Tmp.Hours__c ; }while (count < BrnH_Tmp.size);

 

As I understand it, I can't do the "+=" operation between a Decimal Variable and BrnH_Tmp because the latter is an sObject; thus the reason I specified the Brn_Hrs[0].Hours__c. Should I have instead specified Brn_Hrs[0].Hours__c[0] in order to make this approach work or do I need to use a "get" operation of some kind on BrnH_Tmp to sum all the instances of Hours__c in the BrnH_Tmp, perhaps by putting them into another variable? Or perhaps do I need to do something like use the "VALUE" operator on the sObject instead?

 

I'm still trying to learn how to use APEX's implicit FOR and DO-WHILE loops as opposed to the "for(i=0; i<j; i++)" type of syntax where I can also put "i" into the sObject's array name inside of brackets like what can be done with a specific value. I'm finding that quite frustrating in situations like this.

 

Any help to point me in the right direction would be most appreciated. :)

 

 - pixel

 

  • November 07, 2012
  • Like
  • 0

This is my first trigger. I'm new to the concept of implicit for loops as used in APEX compared to how I originally learned to do them using "for (i=0; i<j,i++)", etc. The references to the first nested loop below generates the following error:

 

"Didn't understand relationship 'Bhrs__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

 

NOTE: My use of the '__r' was an attempt to fix the problem when it didn't understand the relationship without it being there, so I strongly suspect that's not the real problem.

 

The trigger is intended to act on a custom Time Record containing custom Actual Hours, custom Opportunity link and custom Resource link fields, then recalculate the total Actual Hours used by the Resource on that Opportunity by retrieving all the Actual Hours records for the Resource/Opportunity. Once it does, it uses the two links (Resource and Opportunity) to locate a custom Resource Tracking record and update a field in it called Burnt Hours with the new total of Actual Hours it calculated as having been spent on the Opportunity.

 

Here's the section of code I'm referring to:

 

// Declare variables used in loops below
SFL5_SFL5_Time__c BrntH;
SFL5_SFL5_Time__c BrnHrs;
List<SFL5_SFL5_Time__c> BH;
// Get count and record IDs being processed by trigger List AHrsID = [ SELECT SFL5_SFL5_Time__c.Id FROM SFL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.Id IN :Trigger.new]; // This single query finds the ID of every Time record that is associated with the trigger // Test to see if AHrsID.size returns null // Code Block omitted here for brevity // (1) Collect data to calculate new Burnt Hours as part of loop for each // resource/opplink combo on Time Records that hit the trigger List<SFL5_SFL5_Time__c> rList = [ SELECT id, Actual_Hours__c, SFL5_SFDC_Resource__c, Opportunity_Link__c FROM SFL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.Id = :AHrsID.Id]; for (SFL5_SFL5_Time__c[] Bhrs:rList ){ // (2) Create list of ActualHours records called "BrnHrs" for each unique combo of // OppLinks and Resource as the loop iterates thru instances of rList // Error is returned with line below. Can't declare Bhrs prior to loop since is declared in for statement.
BH = [ SELECT Bhrs__r.Actual_Hours__c FROMS FL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.SFL5_SFDC_Resource__c = :Bhrs__r.SFL5_SFDC_Resource__c AND SFL5_SFL5_Time__c.Opportunity_Link__c = :Bhrs__r.Opportunity_Link__c]; BrnHrs.Actual_Hours__c = '0.00'; // Needs to be reset to zero for each iteration of the loop
// (3) Sum Total Burnt Hours
for (SFL5_SFL5_Time__c BH:BrnHrs){ BrnHrs.Actual_Hours__c += BH.Actual_Hours__c ; } // (4) Retrieve record to update BrntH = [ SELECT Burnt_Hours__c FROM Resource_Tracking__c WHEREResource_Tracking__c.Contact__c = :BH.SFL5_SFDC_Resource__c AND Resource_Tracking__c.Opportunity__c = :BH.Opportunity_Link__c]; // (5) Update Burnt Hours into Opportunity Resource Tracking record Add BrntH.Burnt_Hours__c; // Uses last value of BH.Actual_Hours__c in loop (3) to update BrntH.Burnt_Hours__c
try{ updateBrntH; }catch(ListException e) { String myMessage4 = 'Unable to update Burnt Hours for this record with ID ='; // List Exception handling code here System.debug(e.getDmlMessage + myMessage4 + BrntH.Id); } }

 

Any suggestions on how to solve this problem would be most appreciated for this newbie to APEX and this type of implicit loop programming.

 

I'm confident this isn't the most efficient way to code this in APEX, but it's the only way I've been able to figure out how to approach writing the code for this within the constraints of APEX based on my experience of having used other programming languages. Were this in Fortran or C, I'd have been finished long ago.

 

Once I get a better grasp of a few more concepts, I think I'll do much better. :)

 

- pixel

  • November 01, 2012
  • Like
  • 0

I'm trying to verify a Time (not DateTime) value entered in a text string as being valid that will be appended to a Date value in a trigger.

 

The pattern I think I need is (([0][1-9])|([1][0-2]))([U+003A])([0-5][0-9])([\b])([AP])([M]). Apex doesn't seem to like the [\b] which is there as a means to verify that there's a blank space. It also doesn't seem to like a [\s] in it's place either. I'm not entirely certain that either of those is exactly what's needed to designate that I'm looking for a blank space. Could anyone tell me what the most appropriate designator is to use is? I'd also like to verify that [U+003A] is what I need to use to search for a unicode colon.

 

With the RegEx portion out of the way, what's the best way to set up a pattern match of this kind? I've seen it done where each group is matched separately and others where they're entered as the entire RegEx pattern.

 

I've been trying get code similar to the following to work which keeps giving me a fatal error, even when I try debugging it by entering a static value for opp.Event_Start_Time__c:

 

 Pattern TimePttrn = Pattern.compile('(([1][0-2])|([0][1-9]))[U+003A]([0-5][0-9])[\b]([AP])([M])'); // hh:mm aaa'
 if (opp.Event_Start_Time__c != null){
    Matcher m = TimePttrn.matcher(opp.Event_Start_Time__c);
    boolean hasMatch = m.matches();
    system.assert(hasMatch == true);
    strtTm = m.group(0); 
    }
 
Is there something I've missed or am approaching from the wrong direction? Any helpful input greatly appreciated.
 
 
  • February 14, 2013
  • Like
  • 0

How can I take the results of a for loop, saved in decimal object, and put them into a field of a custom sObject? The field also holds decimal values. Below is an example of what I'd like to do that doesn't work. I can't assign the value of a decimal object to an sObject or equate the two directly. So how do I do it by reference? After trying countless methods, I'm at a loss.

 

// Code below is enclosed in larger for loop

for(List<Time__c> BrnH:BrnH_Tmp){
tempSum += BrnH.A_Hours__c ;
}
RsrcHrsLst.B_Hours__c = valueOf(tmpSum);

 

I should note that the lists in the for loop are Time__c Lists while the one outside of it is a ResrceTrack__c List, so they're not the same kinds of Lists even though A_Hours__c are related to B_Hours__c. I just can 't seem to find a direct way to get the sum of the A_Hours__c (Time__c) into a B_Hours__c (ResrceTrack__c) field of a RescrceTrack object after I've located and summed the A_Hours records in my trigger!

 

As one possibility, Is there a method I could use to "put" the values of tempSum into B_Hours__c? Do I need to create a map between the pairs of variables to do that? I'm at my wits end trying to do something that seems as though it should be simple.

 

- pixel

 

 

  • November 09, 2012
  • Like
  • 0

I have two different custom sObjects that have custom decimal fields. After running a for loop to sum the values of the fields in one of the sObjects, I need to transfer the sum to a field in the other sObject. How do I do that? I've tried several different ways that don't work. I should add that this is inside of a larger for loop in case that makes any difference.

 

Here's the general psuedo logic

List <TimeOject__c> BHrs;
List<RscrcTrackObject__c> Rscrc;
Decimal tempSum = 0.00

Outer forLoop{ for(TimeOject__c bh:BHrsTmp) { tempSum += bh.A_Hours__c ; } BHrs[0].A_Hours__c = tempSum; // The above works because I'm only using one element of the list/array // Now I want to assign tempSum to the hours in a RscrcObject Field, but can't set one equal to the other Rscrc.B_Hours__c = BHrs[0].A_Hours__c; // The above doesn't work because they're different kinds of sObjects. // trying to assign Rscrc.B_Hours__c = tempSum didn't work either // Reset tempSum for next iteration of for loop tempSum = 0.00 update Rscrc; } // Close of outer for loop

 

So how do I assign the results of the inner for loop that calculated the sum of A_Hours__c for that instance of BHrsTmp (a TimeObject) to the B_Hours__c in the current instance of Rscrc which is a different type of custom sObject (a RescrcTrackObject) even though both of the Hours fields are decimal?

 

Do I need to create a map and do a put/get or is there hopefully a simpler solution?

 

 

- pixel

  • November 08, 2012
  • Like
  • 0

I have an sObject with a decimal field that I'm having problems creating an APEX loop that will sum the values contained in the instances of that field. The code for it is nested inside another loop. Below is some psuedo code for what's relevent.

 

Initially, before any of the loops, I created a List called Brn_Hrs. Beginning at (3) below, you'll see there's a query done to create a Temp list of records. After that, I set Brn_Hrs[0].Hours__c to (0.00) because I only want a total or sum as output from the loop. I also do not want to fill each element of the array as it goes through each iteration of the loop; both this one and the larger one it's nested inside of.

 

Finally we come to the new for loop I'm having difficulty with when we reach (4). 

 

List<Time__c> Brn_Hrs;
// (3) Create Temp list of Hours records called "BrnH_Tmp" List<Time__c> BrnH_Tmp = [SELECT Hours__c FROM Time__c WHERE Time__c.Resource__c = :rTime.Resource__c AND Time__c.OppLnk__c = :rTime.OppLnk__c]; // Below need to be reset to zero for each iteration of larger loop Brn_Hrs[0].Hours__c = (0.00); count = 0; // (4) Sum Total Burnt Hours for this combo do { Brn_Hrs[0].Hours__c += BrnH_Tmp.Hours__c ; }while (count < BrnH_Tmp.size);

 

As I understand it, I can't do the "+=" operation between a Decimal Variable and BrnH_Tmp because the latter is an sObject; thus the reason I specified the Brn_Hrs[0].Hours__c. Should I have instead specified Brn_Hrs[0].Hours__c[0] in order to make this approach work or do I need to use a "get" operation of some kind on BrnH_Tmp to sum all the instances of Hours__c in the BrnH_Tmp, perhaps by putting them into another variable? Or perhaps do I need to do something like use the "VALUE" operator on the sObject instead?

 

I'm still trying to learn how to use APEX's implicit FOR and DO-WHILE loops as opposed to the "for(i=0; i<j; i++)" type of syntax where I can also put "i" into the sObject's array name inside of brackets like what can be done with a specific value. I'm finding that quite frustrating in situations like this.

 

Any help to point me in the right direction would be most appreciated. :)

 

 - pixel

 

  • November 07, 2012
  • Like
  • 0

This is my first trigger. I'm new to the concept of implicit for loops as used in APEX compared to how I originally learned to do them using "for (i=0; i<j,i++)", etc. The references to the first nested loop below generates the following error:

 

"Didn't understand relationship 'Bhrs__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

 

NOTE: My use of the '__r' was an attempt to fix the problem when it didn't understand the relationship without it being there, so I strongly suspect that's not the real problem.

 

The trigger is intended to act on a custom Time Record containing custom Actual Hours, custom Opportunity link and custom Resource link fields, then recalculate the total Actual Hours used by the Resource on that Opportunity by retrieving all the Actual Hours records for the Resource/Opportunity. Once it does, it uses the two links (Resource and Opportunity) to locate a custom Resource Tracking record and update a field in it called Burnt Hours with the new total of Actual Hours it calculated as having been spent on the Opportunity.

 

Here's the section of code I'm referring to:

 

// Declare variables used in loops below
SFL5_SFL5_Time__c BrntH;
SFL5_SFL5_Time__c BrnHrs;
List<SFL5_SFL5_Time__c> BH;
// Get count and record IDs being processed by trigger List AHrsID = [ SELECT SFL5_SFL5_Time__c.Id FROM SFL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.Id IN :Trigger.new]; // This single query finds the ID of every Time record that is associated with the trigger // Test to see if AHrsID.size returns null // Code Block omitted here for brevity // (1) Collect data to calculate new Burnt Hours as part of loop for each // resource/opplink combo on Time Records that hit the trigger List<SFL5_SFL5_Time__c> rList = [ SELECT id, Actual_Hours__c, SFL5_SFDC_Resource__c, Opportunity_Link__c FROM SFL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.Id = :AHrsID.Id]; for (SFL5_SFL5_Time__c[] Bhrs:rList ){ // (2) Create list of ActualHours records called "BrnHrs" for each unique combo of // OppLinks and Resource as the loop iterates thru instances of rList // Error is returned with line below. Can't declare Bhrs prior to loop since is declared in for statement.
BH = [ SELECT Bhrs__r.Actual_Hours__c FROMS FL5_SFL5_Time__c WHERE SFL5_SFL5_Time__c.SFL5_SFDC_Resource__c = :Bhrs__r.SFL5_SFDC_Resource__c AND SFL5_SFL5_Time__c.Opportunity_Link__c = :Bhrs__r.Opportunity_Link__c]; BrnHrs.Actual_Hours__c = '0.00'; // Needs to be reset to zero for each iteration of the loop
// (3) Sum Total Burnt Hours
for (SFL5_SFL5_Time__c BH:BrnHrs){ BrnHrs.Actual_Hours__c += BH.Actual_Hours__c ; } // (4) Retrieve record to update BrntH = [ SELECT Burnt_Hours__c FROM Resource_Tracking__c WHEREResource_Tracking__c.Contact__c = :BH.SFL5_SFDC_Resource__c AND Resource_Tracking__c.Opportunity__c = :BH.Opportunity_Link__c]; // (5) Update Burnt Hours into Opportunity Resource Tracking record Add BrntH.Burnt_Hours__c; // Uses last value of BH.Actual_Hours__c in loop (3) to update BrntH.Burnt_Hours__c
try{ updateBrntH; }catch(ListException e) { String myMessage4 = 'Unable to update Burnt Hours for this record with ID ='; // List Exception handling code here System.debug(e.getDmlMessage + myMessage4 + BrntH.Id); } }

 

Any suggestions on how to solve this problem would be most appreciated for this newbie to APEX and this type of implicit loop programming.

 

I'm confident this isn't the most efficient way to code this in APEX, but it's the only way I've been able to figure out how to approach writing the code for this within the constraints of APEX based on my experience of having used other programming languages. Were this in Fortran or C, I'd have been finished long ago.

 

Once I get a better grasp of a few more concepts, I think I'll do much better. :)

 

- pixel

  • November 01, 2012
  • Like
  • 0