0

I am trying to create dynamic customer group using suite script in Net suite, I am trying below code but always getting
system INVALID_KEY_OR_REF
Invalid savedsearch reference key 21.

I have checked it is valid save search, Please help I am doing something wrong.

function createDynamicGroup(savedSearchId, groupName) {
var saveSearchObj = nlapiLoadSearch('customer', savedSearchId);
var initValues = new Array();
initValues.grouptype = 'Customer';
initValues.dynamic = 'T';
var goupRecObj = nlapiCreateRecord('entitygroup', initValues);
goupRecObj.setFieldValue('groupname', groupName);
goupRecObj.setFieldValue('savedsearch',saveSearchObj.getId());
nlapiSubmitRecord(goupRecObj);
}
Krushna
  • 5,059
  • 5
  • 32
  • 49
  • Is the saved search a Public search? Have you checked if you can select the same search when creating the Dynamic group in the User Interface? – Rusty Shackles Oct 24 '15 at 13:43
  • Yes the save search is a public one and I am able to create group using that search using UI – Krushna Oct 25 '15 at 05:30

1 Answers1

3

You need group type = 'CustJob' as well as using a public search id:

function createDynamicGroup(savedSearchId, groupName) {
    var saveSearchObj = nlapiLoadSearch('customer', savedSearchId);
    var initValues = {
        grouptype: 'CustJob', // <-- use this
        dynamic: 'T'
    };
    var goupRecObj = nlapiCreateRecord('entitygroup', initValues);
    goupRecObj.setFieldValue('groupname', groupName);
    goupRecObj.setFieldValue('savedsearch', savedSearchId);
    return nlapiSubmitRecord(goupRecObj);
}
bknights
  • 14,408
  • 2
  • 18
  • 31
  • Your solutions worked we need to pass group type as CustJob, But how did you find the info, generally it should be customer. – Krushna Oct 25 '15 at 05:36
  • Oddly I had to figure out a similar issue myself just a couple of days ago. I ended up loading an existing group in the console and inspecting its grouptype field. – bknights Oct 25 '15 at 18:02