Coming soon: Your brand new Help Center & Community! Get a sneak-peek here

Problem to create new stakenholder in scripts

Hi,

i implement copy function to a sales with its stakeholder in SO 7.1 SR2, and it works to create new sale with copied data also links to original sale, but not with stakeholder, each time stopped by:

newStakeholder.save

could you pls. check where is my wrong?

Sub OnCurrentSaleSaved

    'set objApp = CreateObject("SuperOffice.Application")
    'set objSO = objApp.Database
    set objSO = Database

    objSO.login objSO.Username, ""

    if currentsale.Udef.ByProgId("SuperOffice:9").Value = 1 then    ' set checkbox to proceed copy function

        dim yesno2, mySale2

        If Not (objSO is Nothing)  then

            yesno2 = somessagebox ("Are you sure to copy this sale?","Copy Sale", 4)

            if yesno2 = 2 then

                set mySale2 = objSO.CreateSale

                mySale2.setdefaults

                mysale2.Contact = objSO.GetContact(currentsale.Contact.Identity)    ' copy contact

                mysale2.person = objSO.GetPerson(currentsale.Person.Identity)    ' copy person

                mysale2.project = objSO.GetProject(currentsale.Project.Identity)    ' copy project

                'mysale2.Associate = objSO.GetAssociate(currentSale.Associate.Identity)    ' no copy owner, owner is who proceed this copy

                'mySale2.SaleType = objSO.GetListItem(149, currentSale.SaleType.Id)    ' copy sale type
                mySale2.SaleType = objSO.GetListItem(149, 7)     ' copy sale type

                mySale2.Status = 1    ' new sale begin with status Open

                mySale2.Probability = objSO.GetListItem(82, 7)    ' copy probability

                mySale2.AmountAsFloat = currentSale.AmountAsFloat    ' copy amount

                mySale2.Cost = currentSale.Cost    ' copy cost

                mySale2.Credited = objSO.GetListItem(97, currentSale.Credited.Id)    ' copy credited

                mySale2.Source = objSO.GetListItem(100, currentSale.Source.Id)    ' Copy this field renamed to Product Group

                mySale2.Competitor = objSO.GetListItem(109, currentSale.Competitor.Id)    ' copy this field renamed to state central bank

                mySale2.SaleText = currentSale.SaleText    ' copy description

                mySale2.title = currentsale.title & " (Copy)"    ' add new Project No. at previous position and add "(copy)" at last position

                mySale2.Udef.ByProgId("SuperOffice:1").Value = currentsale.Udef.ByProgId("SuperOffice:1").Value    ' Kopien Unternehmensbereich
                mySale2.Udef.ByProgId("SuperOffice:2").Value = currentsale.Udef.ByProgId("SuperOffice:2").Value    ' Kopien Starttermin (PLAN)
                mySale2.Udef.ByProgId("SuperOffice:3").Value = currentsale.Udef.ByProgId("SuperOffice:3").Value    ' Kopien Endtermin (PLAN)

                mySale2.save

               If currentsale.Stakeholders.Count > 0 Then

                   dim oldStakeholder, newStakeholder

                   For Each oldStakeholder In currentsale.Stakeholders

                       Set newStakeholder = objSO.CreateSaleStakeholder
                                            
                       newStakeholder.setdefaults

                       newStakeholder.ContactId = oldStakeholder.Contact.Identity

                       newStakeholder.PersonId = oldStakeholder.Person.Identity

                       newStakeholder.Role = objSO.GetListItem(122, oldStakeholder.Role.Id)

                       newStakeholder.Sale = mysale2

                       newStakeholder.save

                   Next

               End If

               somessagebox "New sale: '" & mySale2.title & "' copied Successfully!",  "Copy Sale"

               currentsale.ActivityLinks.AddSale(mySale2)    ' create links to original sale
               currentsale.ChangeIdentity(mySale2.Identity)    ' swith to new sale
 
            else

                currentsale.Udef.ByProgId("SuperOffice:9").Value = 0    ' reset checkbox to proceed copy function to un-check

            end if    ' end of canceled YesNo

        else   

            somessagebox "Error by Connecting to SuperOffice Database", "Error"

        end if    ' end if objSO

    end if    ' end of Checkbox for Copy

    currentsale.Udef.ByProgId("SuperOffice:9").Value = 0

end sub

 

Thanks!

- Jianchun

RE: Problem to create new stakenholder in scripts

The script test herunder works for me as an external script with our latest 8.1.
I will have to try your use case from inside the Save message handler but I think it might be a waste of time.
From inside a Save message, you cannot further manipulate the model.
You cannot change fields, call Save again and certainly not Change its identity.
Remember that you are inside a Save method that has not yet terminated.
We have to guard against this kind of use.

So, I have a question first: Does this have to trigger on every Save or is it something that a user could decide to do when it is othrewise convenient, e.g. from a Script Method invoked from a button in the navigator as for "Clone current sale". If so, Win has a way to hack a GUI Application record into a Script method call.

Once you can execute a vb script from outside an Event system handler, you will have more room to move..

Conrad

 

-----------------------------------------------------------------------------

set app = CreateObject("SuperOffice.Application")
Set curSale = app.CurrentSale
If Not curSale Is Nothing Then
    If curSale.Identity > 0 Then
        WScript.Echo curSale.Title
        Set newSale = app.Database.CreateSale
        newSale.Title = curSale.Title & " (Copy " & CStr( curSale.Identity ) & " ­- " & Now & ")"
        newSale.SetDefaults
        newSale.Contact = curSale.Contact
        newSale.Person = curSale.Person
        newSale.Amount = curSale.Amount
        newSale.SaleType = curSale.SaleType
        newSale.Save
        
        For Each stakeholder In curSale.Stakeholders
            ­Set newStakeholder = app.Database.CreateSaleStakeholder
            newStakeholder.Role = stakeholder.Role
            newStakeholder.Comment = stakeholder.Comment
            newStakeholder.ContactId = stakeholder.ContactId
            newStakeholder.PersonId = stakeholder.PersonId
            newSt­akeholder.Sale = newSale
            newStakeholder.Save
            WScript.Echo newStakeholder.Identity
        Next

              curSale.ChangeIdentity( newSale.Identity )
    Else
        WScript.Echo "Current sale identity = 0"
    End If 
Else
    WScript.Echo "No current sale"
End If

Af: Conrad Weyns 5. apr 2017

RE: Problem to create new stakenholder in scripts

Hi Conrad,

Now I understand your mind at:

currentsale.Udef.ByProgId("SuperOffice:9").Value = 0

after save method. This is user-defined checkbox, not by every save, only if this checkbox set by user to save, and yes confirmed by Yesno message, then copy routine will be triggered, after save it should be reset to 0 again to be ready for next copy.

I think You are right at this point but I think that could be no problem, anywhere your code works to copy sale also with its all stakeholder, althouth I put this code after curSale.save. Just for security I take your advice to put this code at beginning before save method.

Thanks a lot!

-Jianchun

Af: Jianchun You 5. apr 2017

RE: Problem to create new stakenholder in scripts

Anything you do to currentsale will fail from inside the Save message.
You can try and exploit OnCurrentSaleBeforeSave instead but ChangeIdentity will still fail. In 8.0 I have added PostSoProtocol to SoApplication. This is the only possible way to change the Current Sale identity from inside the Save message as it is a delayed call that will execute when the application is otherwise idle.

Conrad

Af: Conrad Weyns 5. apr 2017

RE: Problem to create new stakenholder in scripts

yes, that ChangeIdentity is now the single thing that not works althouth exploited the copy routine in OnCurrentSaleBeforeSave.

We will plan to upgrade to SO 8 to see any bugs in current 7 has been fixed.

Thanks again!

 

- Jianchun

 

Af: Jianchun You 5. apr 2017

RE: Problem to create new stakenholder in scripts

SInce I am not convinced that triggering this from a UDef checkbox is the best way to go, I take the oportunity to describe once more how to bind script methods to a button in the navigator. This will give you more freedom.

 

Admin (this is Web.admin):
Add a gui application:

 

The parenthesis are required. This is a Win specific hack.
CloneCurrentSale is the script method name you want to bind to.
Such a method's signature is always:

Sub MethodName( params )
End Sub

params is a string that can be empty.
You can use the Parameters field in the GUI App with template variables..

Now add a script file that contains the CloneCurrentSale method to your so_arc scripts folder and activate with SoAdmin.

My example is herunder. Observe: Now I can Change the Current Sale's identity!
This should work in 7.5
Restart SoCrm and you should be good to go.

Conrad

--------- Clone current sale.vbs .------------

Sub CloneCurrentSale( params )
    'MsgBox "CloneCurrentSale: " & params, vbSystemModal
    On Error Resume Next
    
    Err.Clear
    Set curSale = CurrentSale
    If Not curSale Is Nothing Then
        If curSale.Identity > 0 Then
            Err.Clear
            Set newSale = Database.CreateSale
             If Err.Number = 0 Then
                newSale.Title = curSale.Title & " (Copy)"
                newSale.Contact = curSale.Contact
                newSale.Person = curSale.Person
                newSale.Amount = curSale.Amount
                newSale.Associate = curSale.Associate
                newSale.SaleType = curSale.SaleType
                newSale.Private = curSale.Private
                newSale.PrivateToGroup = curSale.PrivateToGroup
                ' whatever else....
                
                Err.Clear
                newSale.Save
                If Err.Number = 0 Then
                    For Each stakeholder In curSale.Stakeholders
                        Err.Clear
                        Set newStakeholder = Database.CreateSaleStakeholder
                        If Err.Number = 0 Then
                            newStakeholder.Role = stakeholder.Role
                            newStakeholder.Comment = stakeholder.Comment
                            newStakeholder.ContactId = stakeholder.ContactId
                            newStakeholder.PersonId = stakeholder.PersonId
                            newStakeholder.Sale = newSale
                            Err.Clear
                            newStakeholder.Save
                        End If
                    Next
                    
                    curSale.ChangeIdentity( newSale.Identity )
                Else
                    Application.SOMessageBox "Unable to save new sale!", "Script encountered an error", 2
                End If
                        
            Else
                Application.SOMessageBox "Unable to create new sale!", "Script encountered an error", 2
            End If
        Else
             Application.SOMessageBox "Current sale identity = 0", "Copy Sale", 2
        End If
    Else
     Application.SOMessageBox "No Current sale instance", "Copy Sale", 2
    End If
End Sub
----------------------------------------------------

Af: Conrad Weyns 6. apr 2017

RE: Problem to create new stakenholder in scripts

Hi conrad,

thanks about this good idea! I will try this way to bind scripts as application with button. Just now we run So 7.1 sr2 as wrote at beginning of this thread, i think it will not work, we wait for planed upgrade later.

Thanks again!

 

-Jianchun

Af: Jianchun You 6. apr 2017

RE: Problem to create new stakenholder in scripts

Well... 7.5, 7.1 - this is years ago. I can't remember when I added this without digging into source cotrol. Might be worth a try anyway :-)

/conrad

Af: Conrad Weyns 6. apr 2017

RE: Problem to create new stakenholder in scripts

Yes I have tried with its sub name, but it happens nothing by press on button. I wait til our managemengt says ok to upgrade.

But that's the really nice to have feature for my request to trigger sth. by button not event.

- Jianchun

Af: Jianchun You 6. apr 2017

RE: Problem to create new stakenholder in scripts

I don't have easy access to a 7.1 db but I can see that this feature is in our 7.1 code branch. I reckon it should work...

/conrad

Af: Conrad Weyns 7. apr 2017

RE: Problem to create new stakenholder in scripts

Hi Conrad,

as said it happens nothing by pressing button.

now it not works to insert image here on devnet, but see screenshot at externe links:

https://cmelchersg.bitrix24.com/docs/pub/7d4fe46aacdfbf127c203e6c32a8ecf6/default/?&

and our SO version 7.1 SR2:

https://cmelchersg.bitrix24.com/docs/pub/ccfe75532bbac4d79e716efc7519b931/default/?&

 

- Jianchun

Af: Jianchun You 7. apr 2017

RE: Problem to create new stakenholder in scripts

----------------
>Such a method's signature is always:

Sub MethodName( params )
End Sub
------------------------

Change:
Sub cloneCurrentTicket
to

Sub cloneCurrentTicket( param )

Also, no need to create an Applicaiton instance, it is implicit when running a script in-process. Simply address Application.

Have you copied your script file to the scripts folder in so_arc and activate it with SoAdmin/Scripts?

If the button in the navigator is disabled, hover over it with the mouse to get to see a tooltip. It mgiht give you some extra info.

Turn on preference for "Enable Script developer mode" in SoAdmin. You'll get a Script utils menu in SoCrm with useful items.

Observe also that if you target the GUI App to Tasks/Sale dialog, you should get an extra menu item for "Clone current ticket" in the Sale menu. This may be a better place to have it instead of a button in the navigator.

/conrad

Af: Conrad Weyns 7. apr 2017

RE: Problem to create new stakenholder in scripts

Hi Conrad,

yes now it works also in 7.1 sr2. I must put method parameter (params) althouth it not used, and needn't to CreateObject("SuperOffice.Application"), now i can proceed this copy routine per button and the user checkbox can be removed.

And then it works now also ChangeIdentity  to swich to new copied ticket, Thank you very much!

And it grows up my wishes maybe by next time:

I try to put this as a task in Sale dialog since it's only to do with current sale and too much buttons now on navi, but i don't found this task in sales. In SO 7 there is no more sale dialog but Sale page like Company, Project...

 

- Jianchun

Af: Jianchun You 7. apr 2017

RE: Problem to create new stakenholder in scripts

This is good news!
Eventhough the Sale dialog has become a standard card, the target "Sale dialog" should still be available in SoAdmin. This will then show up under the Sale menu, of the main Menubar. Towards the bottom of the Sale menu.
(This is not new, also when we had the Sale dialog, the Ext Apps targetted to the Dialog would show up as items under the main Sale menu as well as the right-click context menu on any sale row.

If this is not the case, then may be this is a 7.1 issue. In that case, you are left with a Navigator button, a menu item under the Tools navigator or a menu item under the View menu. The Tools navigator dropdown could be a second best choice.

(I know som ehave used up the available space in the navigator with lots of user buttons...)

Anyway, I think you have a good solution now.
Conrad

Af: Conrad Weyns 7. apr 2017

RE: Problem to create new stakenholder in scripts

Hi Conrad,

the menu item for copy sale doesn't appear under sale menu if i set sale dialog for user app in admin,screenshot:

https://cmelchersg.bitrix24.com/~IX1kS

but the buton in navi and under view menu is also good, maybe good idea by next time to get user apps target available on action bar at the bottom?

Thanks!

- Jianchun

Af: Jianchun You 9. apr 2017

RE: Problem to create new stakenholder in scripts

> maybe good idea by next time to get user apps target available on action bar at the bottom?

Ehm...
Try this:

 

Right-click on bottom toolbar:

 

Or configure toolbar..

 

 

 

 

Works for me :-)
Conrad

Af: Conrad Weyns 9. apr 2017

RE: Problem to create new stakenholder in scripts

You are the best one. now i got toolbar to place my scripts as app at the bottom of user interface.

Thanks!!!

. jianchun

Af: Jianchun You 9. apr 2017