Hi, anyone have any success getting to the credits...
# suitescript
m
Hi, anyone have any success getting to the credits applied data from Bill Payment record? In script? Unfortunately, there is no 'credits' sublist like the apply sublist.
m
Thank you very much
j
I recently tackled this, with guidance from @battk.
for customer payments, but similar thing
First I made a template that is ONLY used by my script which contained the following:
Copy code
<#if record.apply?has_content><apply>[<#list record.apply as apply>{"due":"${apply.due}","currency":"${apply.currency}","applydate":"${apply.applydate}","disc":"${apply.disc}","internalid":"${apply.internalid}","total":"${apply.total}","amount":"${apply.amount}","refnum":"${apply.refnum}"}<#if !apply?is_last>,</#if></#list>]</apply></#if>
		
		<#if record.credit?has_content><credit>[<#list record.credit as credit>{"currency":"${credit.currency}","creditdate":"${credit.creditdate}","appliedto":"${credit.appliedto}","internalid":"${credit.internalid}","amount":"${credit.amount}","type":"${credit.type}","refnum":"${credit.refnum}"}<#if !credit?is_last>,</#if></#list>]</credit></#if>
then my script does the following to grab the data and dump it into JS objects:
Copy code
// Start by gathering all the data we will display.
// Get the XML template results, which will give us the payment & credit data.		
var templated_xml = render.transaction({
	entityId: parseInt(payment_id),
	formId: 270,				
	printMode: render.PrintMode.HTML
});

var xml = templated_xml.getContents();	

var applied_to = [];
var credits = [];		

// Is there anything in the <apply>?
var apply_xml_start = xml.indexOf('<apply>');
var apply_xml_end = xml.indexOf('</apply>');

if(apply_xml_start !== -1 && apply_xml_end !== -1) {

	var apply_xml = xml.substring(apply_xml_start + 7, apply_xml_end);              
	applied_to = JSON.parse(apply_xml);
  
}

// Is there anything in the <credit>?
var credit_xml_start = xml.indexOf('<credit>');
var credit_xml_end = xml.indexOf('</credit>');

if(credit_xml_start !== -1 && credit_xml_end !== -1) {

	var credit_xml = xml.substring(credit_xml_start + 8, credit_xml_end);			
	credits = JSON.parse(credit_xml);

}
m
Thanks a lot @jen!
s
please don't do the unreasonable solution
j
?
m
@battk referred to this solution as the “unreasonable” one in the link he posted above. It’s a fair assessment, but it’s still a very clever workaround IMO.
j
ohhh
Can we really consider it “unreasonable” if it’s literally the only way to get that list via SuiteScript?
s
It sounds like a search can do it?
b
both search and query represent the link between a vendor credit and vendor payment as a join between the bill and credit
while records represent the link as between a credit and payment
there is an additional restriction in that the credits sublist on the payment is only available during create or view
you cant get it from loading the payment, though it is available on the credit
so to get from payment to related credits, you have to search/query the related credits from the bill, which could be inaccurate if you have multiple payments that applied credits to the same bill
you can load each credit to see which payments it applies to, and that usually is fine, its unusual to have so many credits applied from multiple payments that you risk running out of points
j
so to get from payment to related credits, you have to search/query the related credits from the bill, which could be inaccurate if you have multiple payments that applied credits to the same bill
this was the problem I ran into. I’d have credits applied as part of a “payment” that were applied to Bills that weren’t linked to that payment
also other payments applied to bills too e.g. Payment ABC would have a list of credits that included Payment DEC being applied to Bill 123, when Bill 123 did not appear on the “apply” list on Payment ABC.
Good times.