Rockstar
Rockstar

Reputation: 2288

Is there any way to create matrix items using suite script?

Basically my requirement is to create a matrix item through the script. I'm wondering if is there any way to create a matrix item through Restlet or any Workflow. I succeeded creating the parent item with some specific attributes but it seems after submitting the record there is no child items getting created.

Bellow is the code snippet what I'm using right now.

var record= nlapiCreateRecord('serviceitem');
record.setFieldValue('name', 'Matrix Parent Record');
record.setFieldValue('matrixtype', 'PARENT');
record.setFieldValue('custitem_matrix_op1', '2');
record.setFieldValue('custitem_matrix_op2', '3');
var id=nlapiSubmitRecord(record);

Any help or suggestions would be appreciated. Thank You.

Upvotes: 0

Views: 2342

Answers (3)

user6692574
user6692574

Reputation: 11

Little example:

var parent = nlapiCreateRecord('noninventoryitem');
parent.setFieldValue('matrixtype', 'PARENT');
parent.setFieldValue('itemid', 'zzz ttt');
parent.setFieldValue('subsidiary', 2);// internalid for subs.
parent.setFieldValue('taxschedule', 4);// internalid for N/A in my account
parent.setFieldValues('itemoptions', ['CUSTCOL_LLL_EVENTLOCATION_OPT']);//option to be shown at PDP
parent.setFieldValue('custitem_event_location', 11);// particular option id (see in your list)
var parentid = nlapiSubmitRecord(parent);

var child = nlapiCreateRecord('noninventoryitem');
child.setFieldValue('matrixtype', 'CHILD');
child.setFieldValue('parent', parentid);
child.setFieldValue('itemid', 'zzz ttt child');
child.setFieldValue('taxschedule', 4);// internalid for N/A in my account
child.setFieldValues('itemoptions', ['CUSTCOL_LLL_EVENTLOCATION_OPT']);// same as in parent record
child.setFieldValue('matrixoptioncustitem_event_location', 11);// same as in parent record
var childid = nlapiSubmitRecord(child );

It will create a matrix with one child item. Do not forget to set up additional fields like price and "display in web store" (isonline field).

Upvotes: 1

user5227543
user5227543

Reputation:

Using Suite Talk you could do something like this

/** Create Sweaters as matrix items.
* First create the parent - no matrix properties except "Matrix Type" is Parent
* Second create the matrix children with a combination of sizes and colors.
* This can be done in a single addList (as shown). 
*/
//Define mrr method
public static RecordRef mrr(String internalId)
 {
  RecordRef toRet = new RecordRef();
  toRet.setInternalId(internalId);
  return toRet;
 }

// Define makeListOrRecordRef method
public static ListOrRecordRef makeListOrRecordRef(String sTypeId, String internalId, String sName)
 {
  ListOrRecordRef toRet = new ListOrRecordRef();
  toRet.setInternalId(internalId);
  toRet.setName(sName);
  toRet.setTypeId(sTypeId);
  return toRet;
 }

public void testMatrixSample() throws Exception
    {
// Color is a Custom List of TypeId/RecType 1 that has already been created. 1,2,3 represent the
// internalIds of Red, Green, Blue
ListOrRecordRef[] colorArray = new
    ListOrRecordRef[] {makeListOrRecordRef("1","1","Red"), makeListOrRecordRef("1","2","Green"),
    makeListOrRecordRef("1","3","Blue")}; // Representing red, green and blue
// Size is a CustomList of TypeId/RecType 2 that has already been created
ListOrRecordRef[] sizeArray = new ListOrRecordRef[]{makeListOrRecordRef("2","2","Large"),makeListOrRecordRef("2","3","Small")};


//Representing large and small
        InventoryItem[] toSubmit = new InventoryItem[1+colorArray.length*sizeArray.length];
        toSubmit[0] = new InventoryItem();
        toSubmit[0].setExternalId("parentSweater");
        toSubmit[0].setItemId("sweater");
        toSubmit[0].setMatrixType(ItemMatrixType._parent);
        // set other fields on the Parent

        for (int i=0;i<colorArray.length*sizeArray.length;i++)
        {
            toSubmit[i+1] = new InventoryItem();
            toSubmit[i+1].setMatrixType(ItemMatrixType._child);
            // mrr Creates a recordRef given an internal and externalId, the latter of which we specify.
            // This makes it so we can submit all the records at once
            toSubmit[i+1].setParent(mrr((String)null,"parentSweater"));
            // "sweater-large-red","sweater-large-green"...
            toSubmit[i+1].setItemId("sweater-"+colorArray[i%3].getName() + "-" + 
                sizeArray[i % 2].getName());
            // set externalId so it's easier to find later
            toSubmit[i+1].setExternalId(toSubmit[i+1].getItemId());
            // CUSTITEM_COLOR,SIZE are the names of the Item Custom Fields, applied to
            //InventoryItem that were setup as a Matrix types.
            SelectCustomFieldRef colorRef = new SelectCustomFieldRef();
            colorRef.setInternalId("CUSTITEM_COLOR");
            colorRef.setValue(colorArray[i%3]);
            SelectCustomFieldRef sizeRef = new SelectCustomFieldRef();
            sizeRef.setInternalId("CUSTITEM_SIZE");
            sizeRef.setValue(sizeArray[i%2]);
            toSubmit[i+1].setMatrixOptionList(new MatrixOptionList(new
                SelectCustomFieldRef[]{colorRef,sizeRef}));


            // Set other matrix item child files
            //....
        }

        WriteResponseList wr = c.getPort().addList(toSubmit);
    }

Upvotes: 0

Reddy
Reddy

Reputation: 1

After creating the parent item, you need to create child item as well,In the child item set the parent item internal ID & submit the record.

but here there is a drawback. because of static sub list, am not able to append the child items to parent in the Matrix sub list.

Upvotes: 0

Related Questions