APIs & Integrations

RReiffer
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

All,

I am trying to do the following in Deals.  If the amount is null or = 0 then replace the amount with the Total Contract Value (hs_tcv).  My flow appears to do everything, no errors but the amount field stays blank.  Below is the code and a screen shot of my input and output variables.

const hubspot = require('@hubspot/api-client');
 
exports.main = async (event, callback) => {
 
    const hubspotClient = new hubspot.Client({
        accessToken: "pat-na1-NOPE"
    });
    //console.log("Made it past OAuth")
    //First, make a call to get deal associations  
    var amount = event.inputFields['amount'];
        //console.log(amount)
        var hs_mrr = Number(event.inputFields['hs_mrr']);
  var hs_tcv = event.inputFields['hs_tcv'];
  //const closedate = event.inputFields['closedate'];
//console.log(hs_tcv)
      //console.log("1",amount)
      var amount_new = amount
        if(amount_new > 0) {var amount_new = amount} else {var amount_new = hs_tcv}
        //console.log("2",amount_new)
    amount = amount_new
        //console.log("3", amount)
        callback({
        outputFields: {
          amount: amount
        }
      });
}

RReiffer_0-1687281986121.png

 

0 Upvotes
1 Accepted solution
RReiffer
Solution
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hey everyone, I finally figured it out (duh).  I needed to add a step in the workflow that actually copies that value BACK to the deal from the custom code.   I assumed that the code actually wrote data back but it only writes data back to the custom code workflow object. 

RReiffer_0-1687284455592.png

 

View solution in original post

8 Replies 8
HubDoPete
Guide | Gold Partner
Guide | Gold Partner

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hi Richard, I'm curious why you wrote custom code for this.

 

If your Deal workflow:

  • triggers on MRR property is 'known', and permits deal re-enrollment
  • then copies the Deal MRR property to Deal Amount
  • will that also suffice?

best

Pete

0 Upvotes
RReiffer
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Pete,

Wew wanted to have Total Contract Value and not MRR.

Rich

0 Upvotes
HubDoPete
Guide | Gold Partner
Guide | Gold Partner

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hi Richard, in that case, could you just copy across the Deal TCV?

  • workflow triggers on TCV property is 'known', and permits deal re-enrollment
  • then copies the Deal TCV property to Deal Amount

That property is also automatically updated by HubSpot per deal. If you need custom code then sure thing - I was just curious as there are four calculated fields on each deal that you can copy to deal amount:

  • Total contract value (TCV),
  • Annual contract value (ACV),
  • Annual recurring revenue (ARR)
  • and Monthly recurring revenue (MRR).

We ended up writing a help page on automating deal value because it comes up regularly for apps like DoPricer which change line item prices and discounts in the background via the API. So the workflow keeps the deal amount accurate.

 

best

Pete

 

0 Upvotes
KimM
Top Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hey @RReiffer , Glad to see you found a solution to this.

 

I did notice that in your initial post you've used what looks like your access token. Just wanted to make sure that you changed this as otherwise people can access your Hubspot data via API with this.

 

Happy Hubspotting!

Jaycee_Lewis
Community Manager
Community Manager

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Good catch, @KimM 🙌 I went ahead and edited it out of the original post. And, @RReiffer should definitely rotate that key if they haven't already. — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
RReiffer
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Done! Thanks

0 Upvotes
RReiffer
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hi @Teun@louischausse@johnelmer - here is the code I was referring to.  Thanks

0 Upvotes
RReiffer
Solution
Contributor

Issue with my code not writing data back to a Deal with the updated value

SOLVE

Hey everyone, I finally figured it out (duh).  I needed to add a step in the workflow that actually copies that value BACK to the deal from the custom code.   I assumed that the code actually wrote data back but it only writes data back to the custom code workflow object. 

RReiffer_0-1687284455592.png