Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Data in fields get removed....

Posted on by 2,645

I am having an issue with the below script.  When I put information in the null fields below and having nhs_accumulatorsource not filled in, then click save, it wipes the data out and it shouldn't.  It should only wipe the data out when nhs_accumulatorsource contains data


function AccumulatorIDOnChange()
{
var AccumulatorID = Xrm.Page.getAttribute("nhs_accumulatorsource").getValue();
if (AccumulatorID != null || AccumulatorID != 'undefined' || AccumulatorID !='')
{
Xrm.Page.getAttribute("new_fileformat").setValue(null);
Xrm.Page.getAttribute("nhs_accumulatorfilelayoutcomment").setValue(null);
Xrm.Page.getAttribute("nhs_programmingtype").setValue(null);
Xrm.Page.getAttribute("nhs_ftpprovider").setValue(null);
Xrm.Page.getAttribute("new_productionftpdirectory").setValue(null);
Xrm.Page.getAttribute("nhs_productionfilename").setValue(null);
Xrm.Page.getAttribute("new_monday").setValue(null);
Xrm.Page.getAttribute("new_tuesday").setValue(null);
Xrm.Page.getAttribute("new_wednesday").setValue(null);
Xrm.Page.getAttribute("new_thursday").setValue(null);
Xrm.Page.getAttribute("new_friday").setValue(null);
Xrm.Page.getAttribute("new_saturday").setValue(null);
Xrm.Page.getAttribute("new_sunday").setValue(null);
Xrm.Page.getAttribute("new_approximateloadtime").setValue(null);
Xrm.Page.getAttribute("nhs_holidayschedule").setValue(null);
Xrm.Page.getAttribute("nhs_accumulatorfrequencycomments").setValue(null);
Xrm.Page.getAttribute("nhs_accumulatorresultserrorfile").setValue(null);
Xrm.Page.getAttribute("nhs_resultserrorftpdirectory").setValue(null);
Xrm.Page.getAttribute("nhs_resultserrorfilename").setValue(null);
Xrm.Page.getAttribute("nhs_resultserrorfilecomments").setValue(null);
}
}


function AccumulatorIDOnLoad()
{
AccumulatorIDOnChange();
}

*This post is locked for comments

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    Thank you.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Data in fields get removed....

    Add the following function to your code:

    function getLookupName(fieldName) {

       var field = Xrm.Page.getAttribute(fieldName);

       if (field != null) {

           var fieldId = field.getValue();

           if (fieldId != null)

               return fieldId[0].name.toString();

           else

               return '';

       }

    }

    In your AccumulatorIDOnChange replace the following code:

    var AccumulatorID = Xrm.Page.getAttribute("nhs_accumulatorsource").getValue();

    //var lookupName = null;

    if(lookup != null)

    var lookupName  = lookup[0].name;

    if (lookupName != null && lookupName != 'undefined' && lookupName !='')

    with

    var AccumulatorID = getLookupName("nhs_accumulatorsource");

    if (AccumulatorID != '')

    That should be it.

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    Getting error that lookup is undefined

    function AccumulatorIDOnChange()

    {

    var AccumulatorID = Xrm.Page.getAttribute("nhs_accumulatorsource").getValue();

    var lookupName = null;

    if(lookup != null)

    var lookupName  = lookup[0].name;

    if (lookupName != null || lookupName != 'undefined' || lookupName !='')

    {

    Xrm.Page.getAttribute("new_fileformat").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfilelayoutcomment").setValue(null);

    Xrm.Page.getAttribute("nhs_programmingtype").setValue(null);

    Xrm.Page.getAttribute("nhs_ftpprovider").setValue(null);

    Xrm.Page.getAttribute("new_productionftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_productionfilename").setValue(null);

    Xrm.Page.getAttribute("new_monday").setValue(null);

    Xrm.Page.getAttribute("new_tuesday").setValue(null);

    Xrm.Page.getAttribute("new_wednesday").setValue(null);

    Xrm.Page.getAttribute("new_thursday").setValue(null);

    Xrm.Page.getAttribute("new_friday").setValue(null);

    Xrm.Page.getAttribute("new_saturday").setValue(null);

    Xrm.Page.getAttribute("new_sunday").setValue(null);

    Xrm.Page.getAttribute("new_approximateloadtime").setValue(null);

    Xrm.Page.getAttribute("nhs_holidayschedule").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfrequencycomments").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorresultserrorfile").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilename").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilecomments").setValue(null);

    }

    }

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Data in fields get removed....

    Sorry.. later to the party.. Aric has covered it..

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    Would it be something like this?

    function AccumulatorIDOnChange()

    {

    var AccumulatorID = Xrm.Page.getAttribute("nhs_accumulatorsource").getValue();

    //var lookupName = null;

    if(lookup != null)

    var lookupName  = lookup[0].name;

    if (lookupName != null && lookupName != 'undefined' && lookupName !='')

    {

    Xrm.Page.getAttribute("new_fileformat").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfilelayoutcomment").setValue(null);

    Xrm.Page.getAttribute("nhs_programmingtype").setValue(null);

    Xrm.Page.getAttribute("nhs_ftpprovider").setValue(null);

    Xrm.Page.getAttribute("new_productionftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_productionfilename").setValue(null);

    Xrm.Page.getAttribute("new_monday").setValue(null);

    Xrm.Page.getAttribute("new_tuesday").setValue(null);

    Xrm.Page.getAttribute("new_wednesday").setValue(null);

    Xrm.Page.getAttribute("new_thursday").setValue(null);

    Xrm.Page.getAttribute("new_friday").setValue(null);

    Xrm.Page.getAttribute("new_saturday").setValue(null);

    Xrm.Page.getAttribute("new_sunday").setValue(null);

    Xrm.Page.getAttribute("new_approximateloadtime").setValue(null);

    Xrm.Page.getAttribute("nhs_holidayschedule").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfrequencycomments").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorresultserrorfile").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilename").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilecomments").setValue(null);

    }

    }

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Data in fields get removed....

    I believe it's time to learn how to debug webresources - blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

    Good luck!

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    My field that I am checking is a lookup

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    I think I am still missing something:

    function AccumulatorIDOnChange() {

      var fieldId = field.getValue();

      if (field != null) {

          var fieldOption = field.getText();

          if (fieldOption != null)

              return fieldId[0].name.toString();

          else

              return '';

      }

    }

    var AccumulatorID = getOptionSetText("nhs_accumulatorsource");

    if (AccumulatorID !='')

    {

    Xrm.Page.getAttribute("new_fileformat").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfilelayoutcomment").setValue(null);

    Xrm.Page.getAttribute("nhs_programmingtype").setValue(null);

    Xrm.Page.getAttribute("nhs_ftpprovider").setValue(null);

    Xrm.Page.getAttribute("new_productionftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_productionfilename").setValue(null);

    Xrm.Page.getAttribute("new_monday").setValue(null);

    Xrm.Page.getAttribute("new_tuesday").setValue(null);

    Xrm.Page.getAttribute("new_wednesday").setValue(null);

    Xrm.Page.getAttribute("new_thursday").setValue(null);

    Xrm.Page.getAttribute("new_friday").setValue(null);

    Xrm.Page.getAttribute("new_saturday").setValue(null);

    Xrm.Page.getAttribute("new_sunday").setValue(null);

    Xrm.Page.getAttribute("new_approximateloadtime").setValue(null);

    Xrm.Page.getAttribute("nhs_holidayschedule").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorfrequencycomments").setValue(null);

    Xrm.Page.getAttribute("nhs_accumulatorresultserrorfile").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorftpdirectory").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilename").setValue(null);

    Xrm.Page.getAttribute("nhs_resultserrorfilecomments").setValue(null);

    }

    doesn't work.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Data in fields get removed....

    I think you have an issue here:

    if (AccumulatorID != null || AccumulatorID != 'undefined' || AccumulatorID !='')

    You have negation with Or, so I think this will always hit

    Try adding this function:

    function getOptionSetText(fieldName) {

       var field = Xrm.Page.getAttribute(fieldName);

       if (field != null) {

           var fieldOption = field.getText();

           if (fieldOption != null)

               return fieldOption;

           else

               return '';

       }

    }

    Then replace your if statement (and above line) as shown here:

    var AccumulatorID = getOptionSetText("nhs_accumulatorsource");

    if (AccumulatorID !='')

    {

      // Your logic here

    }

    If your field is not an option set (but a lookup), you can modify the following lines to this:

           var fieldId = field.getValue();

           if (fieldId != null)

               return fieldId[0].name.toString();

    Hope this helps.

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: Data in fields get removed....

    When doing F12 and I put in break points on all of it, it hits all of them and nulls the fields even when the Accumulator Source field is not filled in.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans