0

Also trying to get that custom code that extracts the months from the string with regex in my code snippet. I believe I am close but not quite. Console log is returning "undefined" values for the key/value pairs and 0 for the months when it should return 60. Any thoughts on how to restructure this would be very much appreciated! I am trying to get the highest number of months/years from an array and set it to a property in HubSpot. Thank you kindly for any advice on how to properly configure to get correct values.

          hubspotClient.crm.lineItems.batchApi.read({
            inputs,
            properties: ['hs_recurring_billing_period', 'recurringbillingfrequency',]
            })
            .then(res => {
              const inputs = res.body.results.map(result => {
                result.properties.recurringbillingfrequency = 
                result.properties.recurringbillingfrequency;
                result.properties.months = Number(result.properties.months);
                return { term: hs_recurring_billing_period, frequency: recurringbillingfrequency };
                                   
              })
              console.log(inputs);
              
              let term = 0;
              const largestNum = (years) => {
                
              //let term = 0;  
              for (let i=0; i <res.body.results.length; i++){
              let { recurringbillingfrequency, hs_recurring_billing_period } = 
              res.body.results[i].properties;
              console.log(recurringbillingfrequency, hs_recurring_billing_period)
              
              if(recurringbillingfrequency = "Annually")
              {
                let months = Number(hs_recurring_billing_period.replace(/\D/g, ''));
                let years = months / 12;

                
                // let term = 0;
                if (years[i] > term) {
                  term = years[i];
                }
              }
            }

                return term;
              }
              console.log(largestNum(term));
              return;
  • This is the output I am getting back: – Web_Guy2020 Mar 26 '21 at 02:11
  • [ { id: 'numberhere', term: undefined, frequency: undefined }, { id: 'numberhere', term: undefined, frequency: undefined }, { id: 'numberhere', term: undefined, frequency: undefined }, ] annually P36M annually P12M annually P60M 0 – Web_Guy2020 Mar 26 '21 at 02:14

2 Answers2

0

The map function looks strange to me:

const inputs = res.body.results.map(result => {
  result.properties.recurringbillingfrequency = result.properties.recurringbillingfrequency;
  result.properties.months = Number(result.properties.months);
  return { term: hs_recurring_billing_period, frequency: recurringbillingfrequency };                            
})

within the scope of the mapping function, recurringbillingfrequency and hs_recurring_billing_period in the return object are not defined. Would it work by replacing the return value with as so?

return { 
  hs_recurring_billing_period: result.properties.hs_recurring_billing_period, 
  recurringbillingfrequency: result.properties.recurringbillingfrequency
};                            

Also, I am not quite sure how this line is necessary:

result.properties.recurringbillingfrequency = result.properties.recurringbillingfrequency;
  • Hi Maxime - thank you for taking the time to look at my issue. It actually did work with your return statement above. I now am getting an array of results, Thank you!: `[ hs_recurring_billing_period: 'P36M', recurringbillingfrequency: 'annually' }, { hs_recurring_billing_period: 'P60M', recurringbillingfrequency: 'annually' }, { hs_recurring_billing_period: 'P12M', recurringbillingfrequency: 'annually' } ]` etc..... – Web_Guy2020 Mar 26 '21 at 03:26
  • I need to figure out how to also get that Regex string code in there so that I can remove the P and M from the Terms value. Will keep working on it, but my current loop function is returning 0. I am trying to get the highest value from the hs_recurring_billing_period value, which I know will be 60. Would you happen to have suggestions on these two? – Web_Guy2020 Mar 26 '21 at 03:32
  • Got the P and M removed from the map array ` return {                         hs_recurring_billing_period: result.properties.hs_recurring_billing_period.replace(/\D/g, ''),                         recurringbillingfrequency: result.properties.recurringbillingfrequency,                       } almost there...now to figure out the For Loop...get the P and M removed from the loop and find the highest value – Web_Guy2020 Mar 26 '21 at 03:54
  • 1
    I am glad that the mapping worked! Unfortunately, I am terrible with regex so I wouldn't want to risk giving you bad advice on this topic. Now For the highest value, maybe this could elp? https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects – Maxime Moreillon Mar 26 '21 at 03:56
  • Thanks very much for this link and your help. I am taking a look now and hopefully can figure out the for loop and extract the highest value. so far keep getting 0. – Web_Guy2020 Mar 26 '21 at 13:09
0

So either this loop will work and extract the months and set to years or you can use Lodash with one line of code.

              let term = 0;  
              for (let i=0; i <inputs.length; i++){
              let { recurringbillingfrequency, hs_recurring_billing_period } = 
              inputs[i];
              console.log(recurringbillingfrequency, hs_recurring_billing_period)
              
              if(recurringbillingfrequency.toLowerCase() === 'annually')
              {
                let months = hs_recurring_billing_period;

                let years = months / 12.0;
                /*
                 let highest = 0;
                 function getHighestTerm(values) {
                   for (let j=0; j < values.length; j++)
                  
                   if (j === 0) {
                     highest = values;
                   } else if (highest > values[j]) {
                     highest = values[j];
                   }
                   return highest;
                 }
                
                */
                term = _.max(_.map(inputs, 'hs_recurring_billing_period')) / 12.0;