I want to autopopulate Service Name on the serviceappointment entity

I created an OnLoad Script based on some other scripts that Experts Exchange has helped me with.

I want to add a default service name to the service activity form to expedite scheduling. It is not pasting in the service name (“Schedule”). I am not getting any errors either OnLoad or OnSave.

What did I leave out? Here is the code:

= = = = = = = = = = =

function LoadServiceName()
{    

      var oService = Xrm.Page.getAttribute('serviceid');

      var lookupData = new Array();
           lookupData[0] = new Object();
           lookupData[0].name = 'Schedule';
           lookupData[0].entityType = 'service';

// check that the field exists and contains a value to avoid errors

if (oService != null && oService.getValue() != null) {
        // set Service Activity Service to Schedule
         Xrm.Page.getAttribute("serviceid").setValue(lookupData);
    }  
}
jlblankeAsked:
Who is Participating?
 
50calConnect With a Mentor Commented:
You are checking if the value is null and if its not null trying to update it.
When you load the form the value will be null so you'll want to scrap the oService.getValue() !=null condition, or, change to oService.getValue() == null so it only updates if it is a null value.
0
 
50calCommented:
Your not specifying the ID (GUID) of the lookup value.

lookupData[0].id = GUID.
0
 
jlblankeAuthor Commented:
Thanks! I added the GUID but it is still not copying in the service name.  What am I missing? :(

Here is the revised script:

function LoadServiceName()
{    

      var oService = Xrm.Page.getAttribute('serviceid');

      var lookupData = new Array();
           lookupData[0] = new Object();
           lookupData[0].id = '{690001d1-0be3-e111-a141-00505698000e}';
           lookupData[0].name = 'Schedule';
           lookupData[0].entityType = 'service';

// check that the field exists and contains a value to avoid errors

if (oService != null && oService.getValue() != null) {
        // set Service Activity Service to Schedule
         Xrm.Page.getAttribute("serviceid").setValue(lookupData);
    }  
}
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
50calCommented:
Have you added the Event Handler on the form?

Can you add an alert to the if condition to make sure that its getting called?
if (oService != null && oService.getValue() != null) {
        // set Service Activity Service to Schedule
         Xrm.Page.getAttribute("serviceid").setValue(lookupData);
        alert("Test!");
    }  

Open in new window

0
 
jlblankeAuthor Commented:
I added the alert. It did not show up when I first opened the form.

Then I manually added the service and did a save and close. When I reopened the form I received the alert even though the field had the data in it.
0
 
jlblankeAuthor Commented:
I removed the first part of the if statement (oService != null) and changed the second part (oService == null).

Now when I open a new service activity form I get the alert. Nothing gets copied into the Service name field.
0
 
50calCommented:
I've tested it without the second part and it seems to work OK.
The only difference is obviously the GUID and the name, can you confirm that the service type you are adding has a name of Schedule and that the GUID is correct?
0
 
jlblankeAuthor Commented:
I have attached the advanced find query I used to get the GUID. I am puzzled.
For-Re-import---Services-Advance.xlsx
0
 
jlblankeAuthor Commented:
It's working now! Not sure why. Unless there is a web server refresh that has to occur.
0
 
jlblankeAuthor Commented:
Thanks for working with me on this. It is working now. Not sure why it started working - maybe a refresh delay between my office and where the webserver resides (?). At any rate it is now doing what I want it to do. Thanks for helping me with this problem and proofing my script. :)
0
All Courses

From novice to tech pro — start learning today.