This topic contains 2 replies, has 0 voices, and was last updated by ironside 8 years ago.

  • Author
    Posts
  • #6054

    sherwin_suremak

    Im trying to copy a projecttask to another project. The source task has many sub-task under it and im trying to go through it of the child task and copy it to the target project. However when i tried to set the parent task, i got an INVALID_KEY_REF error. Below is the code im trying:

    ===================================

    _copyTask(158824, 193532,null); // 158824 is the taskID of my source task. 193532 is the targetProject. null is initially pass since there will be no parent task for the initial call

    function _copyTask(srcTaskId, targetProjectId, parentTaskId){

    // Make sure source task exist

    var oSrcTask = nlapiLoadRecord(‘projecttask’, srcTaskId);

    if( null==oSrcTask ) {

    return {error:’Invalid source task ID’};

    }

    // create a copy of the sourceTask

    var oNewTask = nlapiCopyRecord(‘projecttask’, srcTaskId);

    oNewTask.setFieldValue(‘company’ ,targetProjectId);

    if(null!=parentTaskId && ”!=parentTaskId) { /* change parent if parentTaskId is provided */

    oNewTask.setFieldValue(‘parent’ ,parentTaskId);

    }

    // I still have other thinks to do in task here

    var newTaskId = nlapiSubmitRecord( oNewTask ); /** this is where the INVALID_KEY_REF error happens */

    // get sourceTask children

    var srSrcTaskChild = nlapiSearchRecord(‘projecttask’, null

    ,[new nlobjSearchFilter(‘parent’,null,’is’,srcTaskId)]

    ,[new nlobjSearchColumn(‘internalid’)]

    );

    if(srSrcTaskChild.length>0) {

    for ( var i = 0; srSrcTaskChild != null && i This is a cached copy. Click here to see the original post.

  • #6055

    david.smith

    I’m thinking this is the same but simplified if statement:

    Code:
    if(parentTaskId)
    I know this wasn’t in your original question but this will throw an error eventually.

    Code:
    // get sourceTask children
    var srSrcTaskChild = nlapiSearchRecord(‘projecttask’, null
    ,[new nlobjSearchFilter(‘parent’,null,’is’,srcTaskId)]
    ,[new nlobjSearchColumn(‘internalid’)]
    );
    if(srSrcTaskChild.length>0) {
    I learned a little trick from someone once and I really like doing my searches like this:

    Code:
    // get sourceTask children
    var srSrcTaskChild = nlapiSearchRecord(‘projecttask’, null
    ,[new nlobjSearchFilter(‘parent’,null,’is’,srcTaskId)]
    ,[new nlobjSearchColumn(‘internalid’)]
    ) || [];
    if(srSrcTaskChild.length>0) {
    If the search returns null it will fail on the length of your if statement. Making sure the search returns an array (even if empty) will prevent this.

  • #6056

    ironside

    I use

    Code:
    if ( _.some(srSrcTaskChild) ) {}
    in cases like this. It describes more clearly what you’re actually trying to achieve -‘if there’s some values, then do something…’

    _.some() works reliably with collections, null, undefined, [], or even objects.

You must be logged in to reply to this topic.