Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to track the status reason changed in Opportunity using Audit and MS SQL

Posted on by 435

Hi Friends , 

I need a report to fetch the opportunity status changed by , changed date and some column of opportunity field using MS SQL query.

SELECT O.AccountIdName as Account, O.modifiedbyname , A.ChangeData,

O.createdon , O.modifiedon, O.createdbyname, O.modifiedbyname ,O.StatusCode , A.Action,

CASE

WHEN O.StatusCode = 1 THEN 'Progress'
WHEN O.StatusCode =2 THEN 'Hold'
WHEN O.StatusCode =3 THEN 'Won'
WHEN O.StatusCode =4 THEN 'Cancelled'
WHEN O.StatusCode =5 THEN 'Out Sold'
WHEN O.StatusCode = 240220000 THEN 'Completed Won'
WHEN O.StatusCode = 240220001 THEN 'Completed Won'
WHEN O.StatusCode = 240220002 THEN 'COMPLTED LOST'
WHEN O.StatusCode =240220002 THEN 'COMPLETED WON DEFEND'
END

AS [Status Reason]

FROM AUDIT A
INNER JOIN Opportunity O ON A.objectid = O.opportunityid

WHERE A.Action IN (2) AND o.StatusCode IN (240220002,240220003,240220000,240220001,1,2,3,4)
GROUP BY AccountIdName as Account, O.modifiedbyname , A.ChangeData,
O.createdon , O.modifiedon, O.createdbyname, O.modifiedbyname ,O.StatusCode , A.Action, O.StatusCode

ORDER BY ModifiedOn DESC

In this query it return all the data which are updated and have status code .

I dont know how i get only thoes data whoes status has been changed.

Please help 

*This post is locked for comments

  • CgRuMy Profile Picture
    CgRuMy 395 on at
    RE: How to track the status reason changed in Opportunity using Audit and MS SQL

    Hi WindyMill

    I''m busy with the same requirement just with leads instead of accounts/opportunities...

    In your where clause replace the "AND o.StatusCode IN (240220002,240220003,240220000,240220001,1,2,3,4)" with "AND A.AttributeMask LIKE '%54%'"

    *remove the "" (double quotation marks)

    AttributeMask column 54 is the StatusReason column within the Audit entity so you exclusively telling SQL to only return these updates

    I hope this helps?

    In my case the users want to see the status reason update per month on all active leads - so I would probably have to write 2 queries, 1 to pull all the lead records and 2 to only explicitly call the status reason updates in order to update the monthly columns

    cheers

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans