web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can we set lookup field as NULL in plugin.

(1) ShareShare
ReportReport
Posted on by 255

Hello,

Can we set Look field as Null or empty to remove link between two entities using plugin in CRM 2013.

Trying this code but failed as Invalid Cast Exception.

caseEntity.Attributes[EntityAttributeConstants.CaseRelatedLog] = null;

service.Update(caseEntity);

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    EntityAttributeConstants.CaseRelatedLog is a string containing the EXACT logical name of the lookup field?

  • Royal King Profile Picture
    27,686 on at

    setting null value should clear lookup value. I would suggest checking the attribute name (lookup field attribute name in the case entity).

  • Abhishek Bihani Profile Picture
    255 on at

    Yes. it contains exact logical name.

  • Royal King Profile Picture
    27,686 on at

    When you go to case entity customization-> fields, does this attribute type shows as lookup? Also make sure attribute name is all in lower case letters.

  • Guido Preite Profile Picture
    54,086 Moderator on at

    then post the exact exception and the full code of this plugin (and also under which message it's triggered)

  • Abhishek Bihani Profile Picture
    255 on at

    Yes Chitra .. it is lookup type field & also attribute name is in small letters as well.

  • Royal King Profile Picture
    27,686 on at

    May be issue is caused by some other attribute value not with this lookup value. Like Guido mentioned post the plugin code may be that will help to direct to find an issue.

  • Abhishek Bihani Profile Picture
    255 on at

    Hi Chitra/Guido,

    Below line of code I am using, on update of case entity.

    EntityCollection entityCollection = GetCaseEntity(currentCaseEntity, service);

    if (entityCollection != null)

                   {

                       if (entityCollection.Entities.Count > 0)

                       {

                           foreach (Entity caseEntity in entityCollection.Entities)

                           {

    bool cancelRelatedLogs = caseEntity.Attributes.Contains("cancelrelatedlog_att") ? (bool)caseEntity["cancelrelatedlog_att"] : false;

    if (cancelRelatedLogs == false)

                                   {

            caseEntity.Attributes["relatedlogid_att"] = null;

                           service.Update(caseEntity);

    }

           }

      }

    }

    System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #B495F49A

  • Guido Preite Profile Picture
    54,086 Moderator on at

    cancelrelatedlog_att and relatedlogid_att are not valid field names

    or you post your REAL code (full) or we are not able to help you.

  • Abhishek Bihani Profile Picture
    255 on at

    Hi Guido,

    It is valid field names(just without publisher as prefix) & real code as well.

    please see my corrected field names.

    cancelrelatedlog_att = wrg_cancelrelatedlog_att

    relatedlogid_att        = wrg_relatedlogid_att

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans