none
Set Lookup field to null programmatically in CRM 2011

    Question

  • Hi ,

    I am trying to clear the lookup value to null programatically through plug-in but no luck.

    I am writing th ebelow line of code.

                Dim recordUpdated As Boolean =

    False

               

    Dim objEntity As Entity =

    Nothing

               

    Dim objParentCOntactRleation As EntityReference =

    Nothing

               

    Try

                   

    'Create Entity

                    objEntity =

    New

    Entity(CNT_CONTACTRELATIONS)

                    objParentCOntactRleation =

    New

    EntityReference()

                   

    'Add Properties to entity

                    objEntity.Id =

    New

    Guid(strGUID)

                    objEntity.Attributes.Add(CNT_COPIEDCONTACTRELATION,

    False

    )

                    

    objEntity(CNT_PARENTCONTACTRELATIONID) = New EntityReference(String.Empty, Guid.Empty)

    //Eevn i tried by setting the lookup attribute to to NULL but no luck

    

                    msCrmService.Update(objEntity)

               

    Catch ex As

    FaultException

                   

    Throw

    ex

               

    Catch ex As

    Exception

                   

    Throw

    ex

               

    Finally

                    objEntity =

    Nothing

               

    End

    Try

    Wednesday, June 22, 2011 5:32 AM

Answers

All replies

  • I believe you need something like this: 

    objEntity.<Lookup Field Name> = nothing
    

     


    Daniel Cai | http://danielcai.blogspot.com
    Wednesday, June 22, 2011 5:40 AM
  • Hi daniel,

     i have already tried this out but no luck..

     

    -Mitesh

    Wednesday, June 22, 2011 5:47 AM
  • Mitesh,

    try this

    objEntity(CNT_PARENTCONTACTRELATIONID) = New EntityReference(<<"EntityName">>, Guid.Empty)


    Mahain : My Dynamics CRM Blog
    Wednesday, June 22, 2011 5:51 AM
  • Hi mahender ,

    i even tried that it says no record exists for empty gUID.

    In CRM 4.0 ,lookup used to have IsNull and IsNull specified but here i could not find similar prop in CRM 2011.

    Any input is appreciated

    Wednesday, June 22, 2011 5:57 AM
  • Hi Mites,

    I used to code using c# ,and below code is working for me

    Enty.Attributes.Add("Attribute", null);

     


    Mahain : My Dynamics CRM Blog

    Wednesday, June 22, 2011 6:23 AM
  • Hi Mitesh, I am stuck in the same kind of problem. did you find solution for this. Please do share it with me. thank you.
    Wednesday, August 24, 2011 6:28 PM
  • Hi,

    If setting a lookup attribute to null is not workign for you then try Disassociate Request:  http://mileyja.blogspot.com/2011/05/how-to-use-disassociate-requests-to.html


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, August 24, 2011 6:45 PM
  • Hi Javeed,

    Thank you for the reply. I have looked into the Disassociate Request, but I think it is used to disassociate relationship between two entities. Have you got any idea of using it my scenario, which is disaccociating the data with my look up field. your help will be much appreciated.

    Thank you.

    Thursday, August 25, 2011 12:15 PM
  • Hi,

    Yes, dissasosiate Request removes association b/w two entities records and have a lookup fields means an entity (that has lookup field) having N:1 relationship with Parent Entity. By passing the GUID of the records will dissaosiate the relationship b/w them (will not delete the relationship itself).  You can pass the Parent record Entity name and GUID in Target and in Related can pass the child record entity name and GUID. like in the example below:   

     

    DisassociateRequest dreq = new DisassociateRequest();

             //Target is the entity that you are disassociating your entities with.

             dreq.Target = new EntityReference("account", new Guid("A6951D47-FB71-E011-882E-1CC1DEF17774"));

             //RelatedEntities are the entities you are disassociating to your target (can be more than 1)

             dreq.RelatedEntities = new EntityReferenceCollection();

             dreq.RelatedEntities.Add(new EntityReference("contact", new Guid("C7EE81A3-0572-E011-882E-1CC1DEF17774")));

     

             //The relationship schema name in CRM you are using to disassociate the entities. 

             //found in settings - customization - entity - relationships

             dreq.Relationship = new Relationship("contact_customer_accounts");

     

             //execute the request

             service.Execute(dreq);

     


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Thursday, August 25, 2011 12:27 PM
  • Hi

    In CRM 2011, if you use the below code using XRM class you wont be able to update lookup and datetime field to Null. This is the limitation of MS Dynamics CRM 2011.
    Entity entity = new Entity("EntityName")
    [entity].[AttributeName] = null;

    Solution:
    To update the lookup and datetime field to null use the below code.
    [entity].Attributes["AttributeName"] = null;

    Thanks
    Mukesh

    Thursday, August 07, 2014 5:08 AM