Specifying parent ids of multiple new parent & child rows?

  • 2
  • Question
  • Updated 3 years ago
  • Answered
  • (Edited)
I'm working on a snippet that will mass copy selected invoice records from one model to another, which also includes child invoice line records. I've successfully added the new invoice row(s) and new invoice line rows but because the parent invoice rows aren't saved, I don't have parent Salesforce IDs for the child rows. How can I link the new child row(s) to an unsaved parent? I could just save the new invoice rows, and loop through the response to then add the new invoice lines but I want to be able to review the new invoices before they're saved.

In the screenshot, the top table shows the original invoice(s) and lines that will be selected to create new invoice(s) in another object. The bottom half shows the new invoice row and child invoice line records that are ready to be saved but are missing the parent ID (invoice number column).

Photo of Oliver Sinfield

Oliver Sinfield

  • 922 Points 500 badge 2x thumb

Posted 3 years ago

  • 2
Photo of Kalyan Kuchi

Kalyan Kuchi

  • 176 Points 100 badge 2x thumb
I am facing same issue. Can somebody help? I am using wizard and creating rows of parent rows and child rows in page 1 of the wizard. I need to be able to save on the second page of the wizard.

On the page 2, In a snippet, I am using saverows() on the parent and then child, The parent rows are getting saved fine  but since child doesnt have the parent Id set, it is throwing at me, the  existing validation rule errors.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Oliver,

Can you paste the snippet that you have so far? Then we'll go from there.
Photo of Oliver Sinfield

Oliver Sinfield

  • 922 Points 500 badge 2x thumb
It's still a work in progress...

var params = arguments[0],
	$ = skuid.$,
	models = skuid.model.map();

// get selected table rows to do mass action
var Ids = skuid.$.map(arguments[0].list.getSelectedItems(), function(item){
	return item.row.Id;
});

// set source parent/child models to copy data from
var SCMInvoices = skuid.model.getModel('SCMInvoiceList');
var SCMInvoiceLines = skuid.model.getModel('SCMInvoiceLineList');

// set destination parent/child models to copy data into
var FFAInvoices = skuid.model.getModel('FFAInvoiceListNew');
var FFAInvoiceLines = skuid.model.getModel('FFAInvoiceLineListNew');

// loop through selected parent rows
$.each(Ids, function(){
	var currentId = this;
	
	// get currently selected parent row
	var SCMInvoiceRow = SCMInvoices.getRowById(this);
	
	// set variables for new parent row
	var customerAccountId = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Customer__r.Id');
	var customerAccountName = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Customer__r.Name');
	var billingAccountId = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Billing_Account__r.Id');
	var billingAccountName = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Billing_Account__r.Name');
	
	var finalAccountId = '';
	var finalAccountName = '';
	
	if (!billingAccountId) {
		finalAccountId = customerAccountId;
		finalAccountName = customerAccountName;
	} else {
		finalAccountId = billingAccountId;
		finalAccountName = billingAccountName;		
	}
	
	var invoiceId = SCMInvoices.getFieldValue(SCMInvoiceRow, 'Id');
	var invoiceName = SCMInvoices.getFieldValue(SCMInvoiceRow, 'Name');
	var invoiceDate = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Invoice_Date__c');
	var invoiceDueDate = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__Invoice_Due_Date__c');
		
	var customerPO = SCMInvoices.getFieldValue(SCMInvoiceRow, 'SCMC__PO_Number__c');
	
	// create new parent row
	var FFAInvoicesRow = FFAInvoices.createRow({
		doAppend: true,
		additionalConditions: [
			{field: 'c2g__Account__c', value: finalAccountId, operator: '=', nameFieldValue: finalAccountName},

			{field: 'SCM_Invoice__c', value: invoiceId, operator: '=', nameFieldValue: invoiceName},
			{field: 'c2g__InvoiceDate__c', value: invoiceDate, operator: '='},
			{field: 'c2g__DueDate__c', value: invoiceDueDate, operator: '='},
			
			{field: 'AC_Customer_PO__c', value: customerPO, operator: '='}
		]
	});
	
	// set condition and activate on child model
	SCMInvoiceLineCondition = SCMInvoiceLines.getConditionByName('SCMC__Invoicing__c');
	SCMInvoiceLines.activateCondition(SCMInvoiceLineCondition);
	SCMInvoiceLines.setCondition(SCMInvoiceLineCondition, SCMInvoiceRow.Id);
	
	// query child model
	SCMInvoiceLines.updateData(function(results){
		
		// loop through child rows
		$.each(SCMInvoiceLines.data, function (i, SCMInvoiceLineRow) {
			// set new child row variables
			var invoiceLineId = SCMInvoiceLineRow.Id;
			var invoiceLineName = SCMInvoiceLineRow.Name;
			
			var invoiceLineGroupName = SCMInvoiceLineRow.RecordType.Name;
			var invoiceLineDescription = SCMInvoiceLineRow.SCMC__Amount_Description__c;
			
			var invoiceLineItemId = '';
			var invoiceLineItemName = '';
			
			if (SCMInvoiceLineRow.SCMC__Item__c) {
				invoiceLineItemId = SCMInvoiceLineRow.SCMC__Item__r.Id;
				invoiceLineItemName = SCMInvoiceLineRow.SCMC__Item__r.Name;
			}
						
			var invoiceLineQuantity = SCMInvoiceLineRow.SCMC__Quantity__c;
			if (invoiceLineQuantity === 0) { 
				invoiceLineQuantity = 1;
			}
			
			var invoiceLineAmount = SCMInvoiceLineRow.SCMC__Amount__c;
			var invoiceLineUnitPrice = SCMInvoiceLineRow.SCMC__Unit_Price__c;
			var invoiceLineFinalAmount = 0;
			
			if (invoiceLineUnitPrice === 0) {
				if (invoiceLineAmount === 0) {
					invoiceLineFinalAmount = 0;
				} else {
					invoiceLineFinalAmount = invoiceLineAmount / invoiceLineQuantity;
				}
			} else {
				invoiceLineFinalAmount = invoiceLineUnitPrice;
			}
			
			// create new child row
			var FFAInvoiceLineRow = FFAInvoiceLines.createRow({
				doAppend: true,
				additionalConditions: [
					{field: 'SCM_Invoice_Line__c', value: invoiceLineId, operator: '=', nameFieldValue: invoiceLineName},
					{field: 'c2g__Product__c', value: invoiceLineGroupName, operator: '=', nameFieldValue: invoiceLineGroupName},
					{field: 'Item_Name__c', value: invoiceLineItemId, operator: '=', nameFieldValue: invoiceLineItemName},
					{field: 'c2g__LineDescription__c', value: invoiceLineDescription, operator: '='},
					{field: 'c2g__Quantity__c', value: invoiceLineQuantity, operator: '='},
					{field: 'c2g__UnitPrice__c', value: invoiceLineFinalAmount, operator: '='}
				]
			});			
		});		
	});		
});
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Oliver,

A couple quick notes:


You don't need to call .activateCondition and then setCondition. Just use setCondition when you're passing a value.

Skuid assigns an integer as the temporary id of rows that it has created but not saved, and when if they are saved it resolves them into salesforce ids. I didn't get through the last portion of your code fully, but I think that something like this will work for you:
  1. Get the variables from the selected parent invoices
  2. Get the variables from the children of the selected invoices
  3. Create new parent rows
  4. Using the ids of the newly created parent rows as your reference values (instead of the ids from the original selected children), create new child rows.

Does that make sense?
I'm not totally confident that it will work, but its worth a shot.
Photo of Oliver Sinfield

Oliver Sinfield

  • 922 Points 500 badge 2x thumb
Thanks for the input Matt! How would I reference the new parent invoice rows if they haven't been saved yet? I know each row has an index integer, but how do I provide this to the newly created invoice line rows so that they have the relationship upon saving?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Can you loop to create the child rows within the creation of each parent row?

$.each(parentrows, function(){
      create parent row
      $.each(childrows, function(){
            create child rows
     });
});

Then you still have access to the id of the parent row while you're creating its children.
(Edited)