Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

JS Code Only Works on 1st Look Up, does not work as expected on subsequent lookups

(0) ShareShare
ReportReport
Posted on by 5

I have a JS script that fires onChange for a field that is a lookup (called service).  On the initial selection of the lookup, the JS works correctly (populates other fields on the form like (description of the service and the duration of the service, etc.).  However, if the lookup is selected again (user wants to select a different service) and a different item is selected, the fields that are populated by the JS are blank/cleared and the JS appears to not be running correctly.  

When the Lookup is selected and "Remove Value" is chosen first, then choosing lookup again and selecting an item in the list works fine also.   This is in Dynamics 365 8.2 

This JS code was running in Dynamics 2015 without issue, and each time the lookup was selected and a different item chosen, the JS code correctly populated all other fields in the form based on the selection.  There was no need to use the "Remove Value" button, etc.

I have ran this form in debugger and see that regardless what the second lookup is, it is not falling into the else if statement, even though it looks like it should to me.  From debugger:

Below, the_service_name clearly is "TA TAS" but this did not fall into this code to set the description and AT/PA values and I do not understand why...

debugger_5F00_221101_5F00_1045.PNG

  • fredericksjr Profile Picture
    5 on at
    RE: JS Code Only Works on 1st Look Up, does not work as expected on subsequent lookups

    function PopulateActivityFromSvcActivityCustomer()

    {

       debugger;

       // Fields on the Service Activity Page...

       var Subject = "subject";

       var SubjectField = Xrm.Page.getAttribute(Subject);

       var ProgramArea = "ec_programarea";

       var ProgramAreaField = Xrm.Page.getAttribute(ProgramArea);

       var Service = "serviceid";

       var the_service = Xrm.Page.getAttribute(Service).getValue();

       var Svc_Act_Customer = "customers";

       var Svc_Act_CustomerValue = Xrm.Page.getAttribute(Svc_Act_Customer).getValue();

       var ActivityType = "ec_activitytype";

       var ActivityTypeField = Xrm.Page.getAttribute(ActivityType);

       // Activity Type Values

       var Orientation = 1;

       var Certification = 2;

       var Other = 3;

       var ABAWD = 200000000;

       // Program Area Values

       var SNAP = 1;

       var TA = 2;

       var MA = 3;

       var LTC = 4;

       var HEAP = 948180000;

       var Homeless = 948180001;

       var EmergencySvcs = 948180002;

       var ServiceActivityTab = Xrm.Page.ui.tabs.get("service");

       var SNAP_Office_Section_Name = "service_section_3";

       var SNAP_Office_Section = ServiceActivityTab.sections.get(SNAP_Office_Section_Name);

       // Create new array to hold customer info

       var customer_name = Svc_Act_CustomerValue[0].name;              //name of customer

       var customer_id = Svc_Act_CustomerValue[0].id;                  //guid of customer

       var customer_entityType = Svc_Act_CustomerValue[0].entityType;  //entity schema name of customer

       // Set Activity Values

       if (the_service == null)

       {

           var description = customer_name + " Appointment";

           var AT = Other;

           var PA = "";

       }

       else

       {

           // Create new array to hold service info

           var the_service_name = the_service[0].name;             //name of service

           var the_service_id = the_service[0].id;                 //guid of service

           var the_service_entityType = the_service[0].entityType; //entity schema name of service

           if (the_service_name == "LTC Appointment")

           {

               var description = customer_name + " LTC Appt";

               var AT = Certification;

               var PA = LTC;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "MA Wed Interview")

           {

               var description = customer_name + " MA Wed Interview";

               var AT = Certification;

               var PA = MA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "SNAP Expedite Service")

           {

               var description = customer_name + " SNAP Exp Appt";

               var AT = Certification;

               var PA = SNAP;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "SNAP Service")

           {

               var description = customer_name + " SNAP Appt";

               var AT = Certification;

               var PA = SNAP;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "TA EFP CERT")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA EFP CERT Refugee")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA EFP CERT Spanish")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA EFP EXPEDITE")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA EFP Homeless")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA Orientation")

           {

               var description = customer_name + " Orientation";

               var AT = Orientation;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA TAS")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA TAS CERT EXPEDITE")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA TAS CERT Spanish")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA TTW INC")

           {

               var description = customer_name + " Cert Appt";

               var AT = Certification;

               var PA = TA;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "SNAP ABAWD")

           {

               var description = customer_name + " SNAP ABAWD Appt";

               var AT = ABAWD;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "ABAWD NTA-SNAP Information Session")

           {

               var description = customer_name + " ABAWD NTA-SNAP Information Appt";

               var AT = ABAWD;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "ABAWD NTA-SNAP Re-Establishment Session")

           {

               var description = customer_name + " ABAWD NTA-SNAP Re-Establishment Appt";

               var AT = ABAWD;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "ABAWD-TA Re-Establishment Session")

           {

               var description = customer_name + " ABAWD-TA Re-Establishment Appt";

               var AT = ABAWD;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "ABAWD Job Search Appointment")

           {

               var description = customer_name + " ABAWD Appt";

               var AT = ABAWD;

               SNAP_Office_Section.setVisible(true);

           }

           else if (the_service_name == "TA CERT Expedite")

           {

               var description = customer_name + " TA Cert Exp Appt";

               var AT = Certification;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA CERT Expedite Spanish")

           {

               var description = customer_name + " TA Cert Exp Appt - Spanish";

               var AT = Certification;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "TA Skype CERT")

           {

               var description = customer_name + " TA Skype CERT Interview";

               var AT = Certification;

               SNAP_Office_Section.setVisible(false);

           }

           else if (the_service_name == "")

           {

               var description = customer_name + " XXXXXXX";

               var AT = Other;

               var PA = "";

               SNAP_Office_Section.setVisible(false);

           }

       } //end "if (the_service != null)"

       // Setting field values based on the Service chosen by the worker.

       SubjectField.setValue(description);

       ProgramAreaField.setValue(PA);

       ActivityTypeField.setValue(AT);

    } // end of function PopulateActivityFromCustomer

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: JS Code Only Works on 1st Look Up, does not work as expected on subsequent lookups

    Hi,

    Can you please share your code?

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Sahra Profile Picture

Sahra 43

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans