<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Efficy Overflow Q&amp;A - Recent questions tagged twice</title>
<link>https://overflow.efficy.io/?qa=tag/twice</link>
<description>Powered by Question2Answer</description>
<item>
<title>Workflow run twice on EditInitilize on converting email to oppo directly from outlook</title>
<link>https://overflow.efficy.io/?qa=2585/workflow-twice-editinitilize-converting-directly-outlook</link>
<description>&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I'm facing a strange behavior on workflow triggering.&lt;/p&gt;

&lt;p&gt;A work flow is triggered on inserting new opportunity to link some standard products by default.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// =================================================================================================
//                  VARIABLES
// =================================================================================================



var LK_PROD_FAMILY_STANDARD = 1;

var OPPO_CREATE_DATE_FIELD = &quot;F_CREATE_DATE&quot;;
//var PROJ_CREATE_DATE_FIELD = &quot;D_START&quot;;
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 &quot;Standard&quot;
function LinkStdProducts(EditHandle){
    var SQL = &quot;SELECT K_PRODUCT, NAME, F_SORT FROM PRODUCTS WHERE FAMILY =:PARAM1&quot;
    var prodDS = ExecuteQuery(SQL, LK_PROD_FAMILY_STANDARD, 810);
    if(prodDS &amp;amp;&amp;amp; !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 = &quot;&quot; + GetReference(DS.FieldByName(&quot;REFERENCE&quot;).AsString, DS.FieldByName(Field).AsFloat, Inserted);
    if(Reference != &quot;&quot;){
        var PrevSecState = Database.DisableSecurity;
        var PrevWFState = Database.DisableWorkflow;
        Database.DisableWorkflow = true;
        Database.DisableSecurity = true;

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

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

function GetReference(Reference, FieldValue, Inserted) {
    if(Reference &amp;amp;&amp;amp; Reference != &quot;&quot;){
        var oldRef = Reference.split(&quot;-&quot;);
        var share = &quot;&quot;;
        switch (FieldValue){
            case SHARES_OPTION_DIG:
                share = &quot;DIG&quot;;
                break;
            case SHARES_OPTION_DOVESCO:
                share = &quot;DOVESCO&quot;;
                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(&quot;-&quot;);
    }
    return &quot;&quot;;
}

function ExecuteQuery(SQL, QueryParams, storeID) {
    var QueryHandle = 0;
    var ContextHandle = Database.OpenTemporaryContext;
    Result = Database.ExecuteSystemSQLQuery(QueryHandle, ContextHandle, SQL, QueryParams, true, true, storeID);
    return Result;
}
&lt;/code&gt;&lt;/pre&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;I made test to disable the call for the function, then the products are not linked at all in any case.&lt;/p&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;Please advice and thank you in-advance for your help.&lt;/p&gt;

&lt;p&gt;Best Regards.&lt;/p&gt;
</description>
<category>WorkFlow / Serverscript</category>
<guid isPermaLink="true">https://overflow.efficy.io/?qa=2585/workflow-twice-editinitilize-converting-directly-outlook</guid>
<pubDate>Thu, 29 Jun 2017 08:49:17 +0000</pubDate>
</item>
</channel>
</rss>