Issues on Updating Lead AssignedId field on Workflow Progress screen, and run SQL query from Sage CRM properly

I wrote a SQL query to evenly distribute leads to the user. The query runs perfectly on SQL Server, but not Sage CRM 7.1SP2. Any feedback is welcome! Thanks!

The query is below. Here is what I did ,and got the SQL errors. The expected result is when the user click on Reassign Lead button, , the SQL query will be run to evenly distribute the leads, update the Assigned To field, and display the newly Assigned To user on Assigned To field on Workflow Progress screen.

First, in Leads Workflow, I copied pasted the SQL Query to a new Execute SQL Query action for the default Reassign Lead rule. When I clicked on the Reassign Lead button, the newly assigned field did not show up on Assigned To field on Workflow Progress screen as expected, when I further clicked on Save button, I got the SQL error:

select Lead_LeadID, Lead_AssignedUserID, lead_RoundRobinAssignFlag FROM Lead WHERE (lead_RoundRobinAssignFlag = 'Y')order by Lead_CreatedDate ASC FETCH NEXT FROM LeadAssignedId
A cursor with the name 'LeadAssignedId' does not exist

Next , I tried a different approach by creating a SQL job on SQL Server, and copied pasted the following statement to a new Execute SQL Query action for the default Reassign Lead rule.

USE msdb;

GO

EXEC

dbo.sp_start_job N'Lead Assignment';

GO

When I clicked on the Reassign Lead button, the newly assigned field did not show up on Assigned To field on Workflow Progress screen as expected as previous attempt, when I further clicked on Save button, I got the SQL error not like the previous one:

Feb 26 2015 16:22:28.819 1636 4876 1 fselectsql,time,sql,errormsg 16 select * from vWorkflowTransition WITH (NOLOCK) where WkTr_TransitionId=2313 Invalid object name 'vWorkflowTransition'

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

DECLARE LeadAssignedId CURSOR FOR

select

Lead_LeadId, Lead_AssignedUserId, leadRoundRobinAssignFlag FROM Lead

WHERE (lead_RoundRobinAssignFlag = 'Y')

order by Lead_LeadID ASC;

OPEN LeadAssignedId

FETCH NEXT FROM LeadAssignedId;

WHILE @@FETCH_STATUS = 0

BEGIN

UPDATE Lead

SET Lead_AssignedUserID =

(CASE

WHEN ((SELECT TOP 1 Lead_AssignedUserID FROM Lead order by Lead_LeadID DESC) = '1')

THEN '2'

ELSE

CASE

WHEN ((SELECT TOP 1 Lead_AssignedUserID FROM Lead order by Lead_LeadID DESC) = '2')

THEN '1'

ELSE '2'

END

END), lead_RoundRobinAssignFlag = 'Y'

from Lead

WHERE CURRENT OF LeadAssignedId;

FETCH NEXT FROM LeadAssignedId;

END

CLOSE LeadAssignedId;

DEALLOCATE LeadAssignedId;

GO

  • 0

    This was not the easiest of posts to read.

    Have you checked the Sage CRM logs to see how CRM is changing the SQL that you have written?

    As far as I know when using the direct execution of SQL from within workflow you can certainly have multiple statements and use #merge# fields.

    E.g.

    update Comm_Link set CmLi_Deleted = 1 where CmLi_Comm_CommunicationId in (select Comm_CommunicationId from Communication where Comm_WaveItemId=#WaIt_WaveItemId#);update Communication set Comm_Deleted=1 where Comm_WaveItemId=#WaIt_WaveItemId#

    But these separate statements can not pass variables to one another. I don't think it is possible to declare a variable in one statement then use it in another. I could be wrong.

  • 0

    Hi,

    With execute SQL it will be slightly tricky to grab the values and write the logic for such kind of requirement. Easiest and straight forward approach will be to call an ASP page on the workflow rule. Refer below blog post for reference.

    community.sagecrm.com/.../using-asp-pages-in-workflow.aspx

    From the ASP page you can either execute your stored procedure. You can also write the logic to assign leads in round robin order through an ASP page itself.

    Hope this information helps!

    Regards,

    Dinesh