0 votes

Hello,

I'm facing a strange behavior on workflow triggering.

A work flow is triggered on inserting new opportunity to link some standard products by default.

// =================================================================================================
//                  VARIABLES
// =================================================================================================



var LK_PROD_FAMILY_STANDARD = 1;

var OPPO_CREATE_DATE_FIELD = "F_CREATE_DATE";
//var PROJ_CREATE_DATE_FIELD = "D_START";
var OPPO_LEAD_REF = 2;
//var PROJ_LEAD_REF = 3;
var SHARES_OPTION_DIG = 1;
var SHARES_OPTION_DOVESCO = 2;

// =================================================================================================
//                  TRIGGERS
// =================================================================================================

//On Edit Initilize

function EditInitializedOppo(EditHandle, Inserted) {
    if(Inserted){
        SetCreateDate(EditHandle, OPPO_CREATE_DATE_FIELD);
        LinkStdProducts(EditHandle);
    }
}


// MAIN COMMITS

function BeforeCommitOppo(EditHandle, Inserted) {
    if(Inserted){
        SetReference(EditHandle, OPPO_LEAD_REF);
    }
}

/*function BeforeCommitProj(EditHandle, Inserted) {
    if(Inserted){
        SetReference(EditHandle, PROJ_LEAD_REF);
    }
}*/

function AfterCommitOppo(EditHandle, Inserted) {
    SetCustomReference(EditHandle, Inserted, 'F_SHARE');
}


// =================================================================================================
//                  FUNCTIONS
// =================================================================================================
//AAL: Link all products with family category "Standard"
function LinkStdProducts(EditHandle){
    var SQL = "SELECT K_PRODUCT, NAME, F_SORT FROM PRODUCTS WHERE FAMILY =:PARAM1"
    var prodDS = ExecuteQuery(SQL, LK_PROD_FAMILY_STANDARD, 810);
    if(prodDS && !prodDS.IsEmpty){
        while(!prodDS.Eof){
                Database.InsertDetail(EditHandle, ntProd, prodDS.FieldByName('K_PRODUCT').AsFloat, -1, false);
                Database.UpdateDetailStringField(EditHandle, ntProd, 0, 0, 'NAME', prodDS.FieldByName('NAME').AsString);
                Database.UpdateDetailField(EditHandle, ntProd, 0, 0, 'K_SORT', prodDS.FieldByName('F_SORT').AsFloat);
            prodDS.Next();
        }
    }
}

function SetReference(EditHandle, RefNo) {
    Database.SetReference(EditHandle, RefNo);
}

function SetCreateDate(OppoHandle, Field){
    var dsOppo = Database.GetMasterDataSet(OppoHandle, 0);
    var createDate = new EffiDate();
    createDate.FromJavaDate(new Date());
    dsOppo.Edit;
    dsOppo.FieldByName(Field).AsString = createDate.GetDateStr_Format2();
}

function SetCustomReference(EditHandle, Inserted, Field){
    var DS = Database.GetMasterDataSet(EditHandle, 0);

    var Reference = "" + GetReference(DS.FieldByName("REFERENCE").AsString, DS.FieldByName(Field).AsFloat, Inserted);
    if(Reference != ""){
        var PrevSecState = Database.DisableSecurity;
        var PrevWFState = Database.DisableWorkflow;
        Database.DisableWorkflow = true;
        Database.DisableSecurity = true;

        DS.Edit;
        DS.FieldByName("REFERENCE").AsString = Reference;
        Database.CommitChanges(EditHandle, false);

        Database.DisableWorkflow = PrevWFState;
        Database.DisableSecurity = PrevSecState;
    }
}

function GetReference(Reference, FieldValue, Inserted) {
    if(Reference && Reference != ""){
        var oldRef = Reference.split("-");
        var share = "";
        switch (FieldValue){
            case SHARES_OPTION_DIG:
                share = "DIG";
                break;
            case SHARES_OPTION_DOVESCO:
                share = "DOVESCO";
                break;
        }

        var newRef = [];
        newRef.push(oldRef[0]);
        newRef.push(share);
        if(Inserted){//X-YYYY-nnnnn
            newRef.push(oldRef[1]);
            newRef.push(oldRef[2]);
        }
        else{//X-DIG/DOVESCO-YYYY-nnnnn
            newRef.push(oldRef[2]);
            newRef.push(oldRef[3]);
        }
        return newRef.join("-");
    }
    return "";
}

function ExecuteQuery(SQL, QueryParams, storeID) {
    var QueryHandle = 0;
    var ContextHandle = Database.OpenTemporaryContext;
    Result = Database.ExecuteSystemSQLQuery(QueryHandle, ContextHandle, SQL, QueryParams, true, true, storeID);
    return Result;
}

When i create new opportunity in Efficy, it's working perfectly. Even when i convert some project or email in Efficy is also ok.

Only when using outlook add-in to convert email directly to opportunity then the products are linked twice. In queryLog the query to get the products to link is run twice when converting email to opportunity from outlook.

I made test to disable the call for the function, then the products are not linked at all in any case.

So basically for some reason the trigger is run twice (even Inserted flag is checked) when converting email to oppo fro outlook. So Any idea why?

Please advice and thank you in-advance for your help.

Best Regards.

asked in WorkFlow / Serverscript by (559 points)

1 Answer

0 votes

Hi,

I am not telling it's a normal behaviour, I am just trying to help :

assuming the event is triggered twice, and that when the second trigger occurs the editcontext is already linked to your products (what it seems to be)

just try to add a test in your event like :

function EditInitializedOppo(EditHandle, Inserted) {

    if(Inserted){
        var ds = GetDetailDataSet(EditHandle, ntProd);
        ds.First();
        if(!ds.EoF){
             LinkStdProducts(EditHandle);
        }

        SetCreateDate(EditHandle, OPPO_CREATE_DATE_FIELD);
    }
}
answered by (989 points)
Hi Louis,

Thank you for the advice and help. it sounds good. I applied another workaround to come over this. i used
Database.ClearDetail(EditHandle, ntProd)
which is working perfectly.
I was trying to see if someone has also faced the same issue before or it's a knowon bug maybe, i've opened a case with support so R&D may check it.
Best Regards.
1,249 questions
1,519 answers
1,859 comments
328 users