locked
Plugin lookup on second entity RRS feed

  • Question

  • CRM2016

    I have a custom entity new_Headers which has a lookup to a custom entity new_originatingaccounts

    The lookup field on new_headers is new_oaccountid

    I an trying to retrieve the value from new_originatingaccounts for the field new_sun

    my code below is not returning any record although I know the record exists

                            Try
                                ' Need to Lookup SUN 
                                Dim strSUN As String = String.Empty
                                If entity.Contains("new_oaccountid") Then
                                    Dim serviceFactory As IOrganizationServiceFactory =
                                            DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), 
                                                       IOrganizationServiceFactory)
                                    Dim service As IOrganizationService = Nothing
                                    service = serviceFactory.CreateOrganizationService(context.UserId)
                                    Dim req As New RetrieveRequest()
                                    req.ColumnSet = New ColumnSet(New String() {"new_sun"})
                                    req.Target = New EntityReference("new_originatingaccounts",
                                                                     DirectCast(entity.Attributes("new_originatingaccountsId"), 
                                                                                EntityReference).Id)
                                    Dim resp As RetrieveResponse
                                    resp = service.Execute(req)
                                    strSUN = DirectCast(resp.Entity.Attributes("new_sun"), String)
                                End If
                            Catch ex As Exception
                                Throw New InvalidPluginExecutionException("Error looking up SUN  - " & ex.Message.ToString)
                            End Try
    


    Dont ask me .. i dont know

    Thursday, September 22, 2016 10:55 AM

Answers

  • Hi,

    I have added the screenshot below to get the PostImageEntity in your plugin.

    Register New Image for your plugin:

       

    Note:
    Make sure the Parameters is All Attributes. If it is not All Attributes, then it`ll always get you only those Attributes, which are selected and the ones that have value in it.

    Then in the code you can use it as below:

    if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] != null)
    Entity productImage = context.PostEntityImages["PostImage"];

    HTH!

     
    Thanks!
    Sam

      

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Pete Newman Monday, September 26, 2016 12:49 PM
    Monday, September 26, 2016 12:42 PM
  • You need to fill this code:

    Public Sub Execute(serviceProvider As IServiceProvider) Implements IPlugin.Execute
    ' First Define needed servises
    Dim tracingService As ITracingService = DirectCast(serviceProvider.GetService(GetType(ITracingService)), ITracingService)
    Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    ' I take your code. If it is wrong, so service will return nothing
    Dim serviceFactory As IOrganizationServiceFactory =
              DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), 
                         IOrganizationServiceFactory)
    Dim service = serviceFactory.CreateOrganizationService(context.UserId)
    
    If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity Then
    	Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity)
    	Dim NeedColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_complete", "new_oaccountid");
    	Dim EntityFromBase = service.Retrieve(entity.LogicalName, entity.Id, NeedColumns)
    	
    	Dim bolIsUploadCompleted As Boolean = False
    	If entity.Contains("new_complete") Then
    		' If new_complete have been changed, it will be here
    		bolIsUploadCompleted = entity.GetAttributeValue(Of Boolean)("new_complete")
    	Else
    		If (EntityFromBase.Contains("new_complete") Then
    			' If new_complete have not been changed, it will be here
    			bolIsUploadCompleted = EntityFromBase.GetAttributeValue(Of Boolean)("new_complete")
    		Else
    			' Some mistake
    			bolIsUploadCompleted = False
    		End If
    	End If
    	
    	Dim oaccountEntityId As Guid
    	If entity.Contains("new_oaccountid") Then
    		' If new_complete have been changed, it will be here
    		oaccountEntityId = entity.GetAttributeValue(Of Guid)("new_oaccountid")
    	Else
    		If (EntityFromBase.Contains("new_oaccountid") Then
    			' If new_complete have not been changed, it will be here
    			oaccountEntityId = EntityFromBase.GetAttributeValue(Of Guid)("new_oaccountid")
    		Else
    			' Some mistake
    			oaccountEntityId = 'I don't know what to write here I belive Guid.Empty. C# code: oaccountEntityId = Guid.Empty;
    		End If
    	End If
    
    	If bolIsUploadCompleted = True Then
    		Using BACSsw As StreamWriter = New StreamWriter("C:\Temp\Test.txt")
    			Try
    				' Need to Lookup SUN 
    				BACSsw.WriteLine("Complete = 01")
    				If oaccountEntityId <> Guid.Empty Then
    					BACSsw.WriteLine("Complete = 02")
    					' Write to a file oaccountEntityId wich contains GUID of new_originatingaccounts
    					BACSsw.WriteLine(DirectCast(oaccountEntityId, string))
    					Dim NeedoaccountColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_sun");
    					Dim oaccountEntityFromBase = service.Retrieve("new_originatingaccounts", oaccountEntityId, NeedoaccountColumns)
    					' Write to a file "new_sun" value
    					BACSsw.WriteLine(oaccountEntityFromBase.GetAttributeValue(Of string)("new_sun"))
    				End If
    			Catch ex As Exception
    				Throw New InvalidPluginExecutionException("Error looking up SUN  - " & ex.Message.ToString)
    			End Try
    			' Write out TEST File
    			BACSsw.WriteLine("Finish")
    		End Using
    	
    	End If
    
    End If
    
    End Sub
    Here
    'Here
    Dim NeedColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_complete", "new_oaccountid");
    'and here
    Dim NeedoaccountColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_sun");

    • Marked as answer by Pete Newman Monday, September 26, 2016 10:02 AM
    Friday, September 23, 2016 9:41 AM
  • Hi Pete,

    I think I found a catch in your code.
     
    I don`t exactly know, on what message is your plug-in.
     
    If the new_oaccountid field is not getting updated, then you won`t get that particular attribute in Target.
     
    Why don`t you use the PostImage? It`ll get you new_oaccountid always irrespective whether it is updated or not.
     
    HTH!
     
    Thanks,
    Sam

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Pete Newman Monday, September 26, 2016 10:02 AM
    Friday, September 23, 2016 12:58 PM

All replies

  • Hi,

    Where are you getting a new_Headers identifier?

    As i understand. your plug-in runs on a new_Headers entity, so your code is about it:

    If entity.Contains("new_oaccountid") Then

    // Getting a GUID of Linked entity Guid oaccountGuid = entity["new_oaccountid"].id

    // Getting linked Entity Entity oaccountEntity = service.Retrieve("new_headers", oaccountGuid, new ColumnSet("new_sun"))

    ...




    • Edited by xjomanx Thursday, September 22, 2016 12:13 PM
    Thursday, September 22, 2016 12:11 PM
  • Hi ,

    Thank you for your response. How would this sit in my code in vb.net. 


    Dont ask me .. i dont know

    Thursday, September 22, 2016 1:47 PM
  • Hi ,

    Thank you for your response. How would this sit in my code in vb.net. 


    Dont ask me .. i dont know

    I don't write vb.net, but, as I believe you should write something like this:

    Try
    ' Need to Lookup SUN
    Dim strSUN As String = String.Empty
    If entity.Contains("new_oaccountid") Then
      Dim serviceFactory As IOrganizationServiceFactory =
              DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)),
                         IOrganizationServiceFactory)
      Dim service As IOrganizationService = Nothing
      'Getting service
      service = serviceFactory.CreateOrganizationService(context.UserId)
      Dim oaccountEntity As Entity
      ' Retrive linked entity (its ID contains attribute "new_oaccountid")
      oaccountEntity = service.Retrieve("new_headers", entity["new_oaccountid"].id, new ColumnSet("new_sun"))
      If oaccountEntity.Contains("new_sun") Then
        strSUN = oaccountEntity["oaccountEntity"];
      End If
    End If
    Catch ex As Exception
     Throw New InvalidPluginExecutionException("Error looking up SUN  - " & ex.Message.ToString)
    End Try


    • Edited by xjomanx Thursday, September 22, 2016 2:23 PM
    Thursday, September 22, 2016 2:19 PM
  • HI,

    I think I have found the problem, but cannot see what the problem is. I don't think the lookup field is being seen.

    I have cut out all the code to check if I am getting into the loop .. and I am not

    code

            Public Sub Execute(serviceProvider As IServiceProvider) Implements IPlugin.Execute
                Dim tracingService As ITracingService = DirectCast(serviceProvider.GetService(GetType(ITracingService)), ITracingService)
                Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    
     
    
                If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity Then
                    Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity)
    
                    Try
                        ' Check if new_complete is true
                        Dim bolIsUploadCompleted As Boolean = False
                        Try
                            If entity.Contains("new_complete") Then
                                bolIsUploadCompleted = entity.GetAttributeValue(Of Boolean)("new_complete")
                            Else
                                bolIsUploadCompleted = False
                            End If
    
                        Catch ex As Exception
                            tracingService.Trace("Error Reading new-complete " & ex.Message.ToString)
                            Throw
                        End Try
    
                        ' new_compelete is 1 (Upload completed) 
                        If bolIsUploadCompleted = True Then
                            Using BACSsw As StreamWriter = New StreamWriter("C:\Temp\Test.txt")
    
                                 Try
                                    ' Need to Lookup SUN 
                                    BACSsw.WriteLine("Complete = 01")
                                    If entity.Contains("new_oaccountid") Then
                                        BACSsw.WriteLine("Complete = 02")
                                                                        End If
                                Catch ex As Exception
                                    Throw New InvalidPluginExecutionException("Error looking up SUN  - " & ex.Message.ToString)
                                End Try
    
    
                                ' Write out TEST File
                                BACSsw.WriteLine("Finish")
                            End Using
    
                        End If
    
    
                    Catch ex As Exception
                    Throw New InvalidPluginExecutionException("Error Generating BACS File" & ex.Message.ToString)
                End Try
    
    
                End If
    
            End Sub

    Result from file is

    Complete = 01
    Finish

    What this is saying is that the entity does not contain the field new_oaccountid . the custom fields from crm below


    Dont ask me .. i dont know

    Thursday, September 22, 2016 4:05 PM
  • Once again:

    You have two entities: new_Headers and new_originatingaccounts

    new_headers Contains a lookup new_oaccountid to a new_originatingaccounts.

    You have a plug-in, witch is firing on a new_headers action (create or update). Q1: Is it right?

    context.InputParameters("Target").Contains("new_complete") only when "new_complete" is changed. So, if you need to get a value on any plug-in execution (Q2: do you need this value on any execution of plug-in, or only when it change?), you need to check if

    context.InputParameters("Target").Contains("new_complete")

    and find your value in a base, if it does not changed.



    Friday, September 23, 2016 5:58 AM
  • I am now totally confused. I stripped my code back to basics

            Public Sub Execute(serviceProvider As IServiceProvider) Implements IPlugin.Execute
                Dim tracingService As ITracingService = DirectCast(serviceProvider.GetService(GetType(ITracingService)), 
                                                                   ITracingService)
    
                Dim context As IPluginExecutionContext =
                        DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    
                If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity _
                                Then
                    Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity)
    
                    Using BACSsw As StreamWriter = New StreamWriter("C:\Temp\Test.txt")
                        If entity.Contains("new_LicenceIdName") Then
                            BACSsw.WriteLine("new_LicenceIdName")
                        End If
                        If entity.Contains("new_oaccountidName") Then
                            BACSsw.WriteLine("new_oaccountidName")
                        End If
                        If entity.Contains("new_transactionsetsId") Then
                            BACSsw.WriteLine("new_transactionsetsId")
                        End If
                        If entity.Contains("new_serialnumber") Then
                            BACSsw.WriteLine("new_serialnumber")
                        End If
                        If entity.Contains("new_LicenceId") Then
                            BACSsw.WriteLine("new_LicenceId")
                        End If
                        If entity.Contains("new_oaccountid") Then
                            BACSsw.WriteLine("new_oaccountid")
                        End If
                        If entity.Contains("new_transactioncount") Then
                            BACSsw.WriteLine("new_transactioncount")
                        End If
                        If entity.Contains("new_submissionvalue") Then
                            BACSsw.WriteLine("new_submissionvalue")
                        End If
                        If entity.Contains("new_complete") Then
                            BACSsw.WriteLine("new_complete")
                        End If
                        ' Write out TEST File
                        BACSsw.WriteLine("Finish")
                    End Using
                End If
            End Sub
    

    When firing the plugin on post Update ( filter new_complete ) , the test.txt file was created but contained on only "Finish"

    however when setting the plugin to fire post create I get the following in the file

    new_serialnumber
    new_oaccountid
    Finish


    Dont ask me .. i dont know

    Friday, September 23, 2016 9:10 AM
  • You need to fill this code:

    Public Sub Execute(serviceProvider As IServiceProvider) Implements IPlugin.Execute
    ' First Define needed servises
    Dim tracingService As ITracingService = DirectCast(serviceProvider.GetService(GetType(ITracingService)), ITracingService)
    Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    ' I take your code. If it is wrong, so service will return nothing
    Dim serviceFactory As IOrganizationServiceFactory =
              DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), 
                         IOrganizationServiceFactory)
    Dim service = serviceFactory.CreateOrganizationService(context.UserId)
    
    If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity Then
    	Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity)
    	Dim NeedColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_complete", "new_oaccountid");
    	Dim EntityFromBase = service.Retrieve(entity.LogicalName, entity.Id, NeedColumns)
    	
    	Dim bolIsUploadCompleted As Boolean = False
    	If entity.Contains("new_complete") Then
    		' If new_complete have been changed, it will be here
    		bolIsUploadCompleted = entity.GetAttributeValue(Of Boolean)("new_complete")
    	Else
    		If (EntityFromBase.Contains("new_complete") Then
    			' If new_complete have not been changed, it will be here
    			bolIsUploadCompleted = EntityFromBase.GetAttributeValue(Of Boolean)("new_complete")
    		Else
    			' Some mistake
    			bolIsUploadCompleted = False
    		End If
    	End If
    	
    	Dim oaccountEntityId As Guid
    	If entity.Contains("new_oaccountid") Then
    		' If new_complete have been changed, it will be here
    		oaccountEntityId = entity.GetAttributeValue(Of Guid)("new_oaccountid")
    	Else
    		If (EntityFromBase.Contains("new_oaccountid") Then
    			' If new_complete have not been changed, it will be here
    			oaccountEntityId = EntityFromBase.GetAttributeValue(Of Guid)("new_oaccountid")
    		Else
    			' Some mistake
    			oaccountEntityId = 'I don't know what to write here I belive Guid.Empty. C# code: oaccountEntityId = Guid.Empty;
    		End If
    	End If
    
    	If bolIsUploadCompleted = True Then
    		Using BACSsw As StreamWriter = New StreamWriter("C:\Temp\Test.txt")
    			Try
    				' Need to Lookup SUN 
    				BACSsw.WriteLine("Complete = 01")
    				If oaccountEntityId <> Guid.Empty Then
    					BACSsw.WriteLine("Complete = 02")
    					' Write to a file oaccountEntityId wich contains GUID of new_originatingaccounts
    					BACSsw.WriteLine(DirectCast(oaccountEntityId, string))
    					Dim NeedoaccountColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_sun");
    					Dim oaccountEntityFromBase = service.Retrieve("new_originatingaccounts", oaccountEntityId, NeedoaccountColumns)
    					' Write to a file "new_sun" value
    					BACSsw.WriteLine(oaccountEntityFromBase.GetAttributeValue(Of string)("new_sun"))
    				End If
    			Catch ex As Exception
    				Throw New InvalidPluginExecutionException("Error looking up SUN  - " & ex.Message.ToString)
    			End Try
    			' Write out TEST File
    			BACSsw.WriteLine("Finish")
    		End Using
    	
    	End If
    
    End If
    
    End Sub
    Here
    'Here
    Dim NeedColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_complete", "new_oaccountid");
    'and here
    Dim NeedoaccountColumns as ColumnSet = 'I don't know what to write here. C# code: ColumnSet NeedColumns = new ColumnSet("new_sun");

    • Marked as answer by Pete Newman Monday, September 26, 2016 10:02 AM
    Friday, September 23, 2016 9:41 AM
  • Hi Pete,

    I think I found a catch in your code.
     
    I don`t exactly know, on what message is your plug-in.
     
    If the new_oaccountid field is not getting updated, then you won`t get that particular attribute in Target.
     
    Why don`t you use the PostImage? It`ll get you new_oaccountid always irrespective whether it is updated or not.
     
    HTH!
     
    Thanks,
    Sam

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Pete Newman Monday, September 26, 2016 10:02 AM
    Friday, September 23, 2016 12:58 PM
  • Hi Pete,

    I think I found a catch in your code.
     
    I don`t exactly know, on what message is your plug-in.
     
    If the new_oaccountid field is not getting updated, then you won`t get that particular attribute in Target.
     
    Why don`t you use the PostImage? It`ll get you new_oaccountid always irrespective whether it is updated or not.
     
    HTH!
     
    Thanks,
    Sam

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Hi,

    My postImmage contains only attributes witch will be updated (or, maybe, I'm looking wrong place) :

    If you show, where exactly i can get needed attributes without Retrive it, i'll be very glad.

    Friday, September 23, 2016 1:44 PM
  • Hi,

    I have added the screenshot below to get the PostImageEntity in your plugin.

    Register New Image for your plugin:

       

    Note:
    Make sure the Parameters is All Attributes. If it is not All Attributes, then it`ll always get you only those Attributes, which are selected and the ones that have value in it.

    Then in the code you can use it as below:

    if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] != null)
    Entity productImage = context.PostEntityImages["PostImage"];

    HTH!

     
    Thanks!
    Sam

      

    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Pete Newman Monday, September 26, 2016 12:49 PM
    Monday, September 26, 2016 12:42 PM
  • Hi Sam,

    Thanks for the help, and to everyone else. I now have it all sorted. the postImage was exactly what was required


    Dont ask me .. i dont know

    Monday, September 26, 2016 12:50 PM