Basil1105
Basil1105

Reputation: 11

How to update or add fields to a sublist item (array?)

When we create a Quote in Zoho Books, we use Stripe as our payment gateway. When the customer Accepts the Quote, they are re-directed to a page for payment. Stripe is the only one we use.

We need to manually change that setting for every quote/estimate and sometimes we forget and the client cannot proceed with their payment. We use the "standard" templates for our quotes. I want to update the last line of this code, the "gateway_name":"stripe" to always be "stripe".

"payment_options":{
    "payment_gateways":[
        {
            "configured":true,
            "can_show_billing_address":false,
            "is_bank_account_applicable":false,
            "can_pay_using_new_card":true,
            "gateway_name":"stripe"
        }
    ]
},

I have written the script to update several fields in the Quote, but I cannot figure out how to map this to the sub list (array?).

I can get the values from the list with this code:

payList = inrec.getjson("payment_options").getjson("payment_gateways");
info "payList " + payList;
//.get("gateway_name");
// downpayment = "";

for each rec in payList
{
    paybyCard = rec.get("can_pay_using_new_card");
    Payname = rec.get("gateway_name");
    PaybyCC = rec.get("configured");
    info "PaybyCC: " + PaybyCC;
    info "Payname: " + Payname;
}

And I get the result:

payList {"configured":true,"can_show_billing_address":false,"is_bank_account_applicable":false,"can_pay_using_new_card":true,"gateway_name":"stripe"}
PaybyCC: true
Payname: stripe

What I don't know, is how to map this field with deluge.

The other fields I am updating are simple and I can do them like this:

quoteMAP.put("Amount",estBalance);

I have made some progress since I asked the question, only to find out a different problem. I learned I need to make a Map of the Items and include the Map in a List. I compared the code before and after the options have been selected manually.

The code in the Quote before the selections:

"payment_options":{
"payment_gateways":[
]
},

And the code after the items have been selected:

"payment_options":{
"payment_gateways":[
{
"configured":true,
"can_show_billing_address":false,
"is_bank_account_applicable":false,
"can_pay_using_new_card":true,
"gateway_name":"stripe"
}
]
},`

It seems that I need to "Add" those extra fields to the code. To achieve that, I set up the Map to reflect the code in the Quote.

stripeMap=map();
stripeMap.put("configured",true);
stripeMap.put("can_show_billing_address",false);
stripeMap.put("is_bank_account_applicable",false);
stripeMap.put("can_pay_using_new_card",true);
stripeMap.put("gateway_name","stripe");`

and the List as follows:

stripelist=list();
stripelist.add(stripeMap);

I then added the List to the quoteMAP

quoteMAP.put("payment_options", stripelist);

The code from the Info statement came like this:

{
"expiry_date":"2024-11-25",
"Amount":"407.0",
"accept_retainer":true,
"retainer_percentage":"25",
"configured":true,
"PaybyCC":true,
"payment_options":[
{
"configured":true,
"can_show_billing_address":false,
"is_bank_account_applicable":false,
"can_pay_using_new_card":true,
"gateway_name":"stripe"
}
]
}

When I ran the function:

updateQuote = zoho.books.updateRecord("estimates",organizationID,estimateID,quoteMAP,"invoice");

I received

updateQuote: {"code":1038,"message":"JSON is not well formed"}

and ran it through jsonlint.com and it came back as valid. The only thing I can think of is that I need to "add" those items in the code, since they do not exist. I don't know how to do that.

Upvotes: 1

Views: 48

Answers (0)

Related Questions