Language translation in ServiceNow using scripts | Sample use-case | DynamicTranslation API
hi everyone in this video i'm going to show you how we can trigger microsoft translation api using custom scripts so there are a couple of things that we can do using those scripts first is we can detect the language of the ticket based on which field value we are passing in the api we can detect what language it is and apart from that we can also convert a given text into a target language so let's see how we can do that so to achieve this i have considered a use case here where i i have introduced a tab on the incident form name translation this is the ticket which i created in my previous video and on this translation tab it will populate the ticket language and it will also populate the short description whatever description is written by the caller here it will be populated over here as well in english language and resolution nodes will be populated in the caller's language for example if somebody enters resolution information here in english scholar does not understand english so that will be transferred over here in the college language so let's get over with it to achieve this first of all i need to populate the ticket language and ticket language will be populated over here on insert of the new record and at the same time since short description is a mandatory field we can also populate short description over here so let's just jump on to the implementation i am going to create a business rule first okay so let's create a new one hey the name of my business soul is translated insert advanced to make it an async call because there is a communication happening with the microsoft translator api which in uncertain scenarios can take some time so i need to convert whatever short description is entered by the user into english language so for that let me create a short description object and then i am going to tell you the api this is the name of my api that is dynamic translation and it contains a function name get translation okay and this function contains certain parameters first parameter is the text which is to be converted so in this case it is the short description which contains the text to be on the safety side i prefer to convert into two string and the second parameter is an object which internally contains several other parameters as well i will discuss about those so let me create complete parenthesis over here the first parameter is target languages okay so we can convert the same text into uh one single language or we can also convert it into multiple different languages okay so to do that we need to pass whatever language we want to convert our text into all languages into an array so we are going to pass the code of the language over here for english it is en the next parameter would be like we can pass additional parameters where we define the type of the text so the name is additional parameters which again contains two different parameters one of them being parameter named second one being parameter value so i'll consider a plain text and here i need to define the translator type which translator i am using whether it is google microsoft ibm watson or what so in my case it is going to be microsoft so we can also store all the static values over here into a system property but let's do it this way for now semicolon so this piece of code will give me the translated text as well as the language of the text okay so the next part is to get those things from this particular code so let me execute this in a background script so that i can show you guys what kind of response i'm going to get and how to fetch the values from there okay don't turn before if i run the script okay there is somewhere my bad i need to go back so here i need to enter our text let's consider this is my text if i click on run script again this is my response let's copy this and paste it so this is an object where i have an object variable name translations under that i have target language and the translated text which is the conversion and the detected language code is d e so from this response i can get two things one is the translated text and second thing is the code of the language which will be my ticket language okay so let's go back to a business rule and save all these things so this is going to be my translated short description here is my short description object next i had was translations which is analytic this is another because i passed translated language as english but it as i said it can be multiple other languages so first object would be the first language we passed and the second one that is where this is another so i need to fetch the first object from the array here so it is going to be something like this and then i have the translated text so this is going to be my translated text now i also need to fetch the language from here so let me create another variable this is my short description object and from here i am going to go to my detected language parameter and then there i have a code so now i have both the things which i need the translated text and my ticket language now i need to pass these values to my incident record so that it is updated now how do i do that for that purpose let me create an object first so that i don't have to create multiple arguments for my function that is why i'm doing this i'm simply going to pass the translated object and to update that i am going to call a script include and within this script include i'm going to create a function named set translated data on insert it will contain two parameters first parameter being the object itself and second parameter being the society of the incident so whatever i wanted to do within this particular business rule is now done now i am going to navigate to my script include once this is saved so this is my script include and let me copy the name of the function from here there were two parameters one is this another one is this so here first of all i am going to build my incident table yeah i know i know there is an easy way but i am used to doing it this way many people might be wondering why don't i do simply incident dot get and pass the society let's not enter into that debate so my one of my field name is ticket language okay and my second field name is short description translated now the ticket language which i have on the incident form is a reference field which refers to this underscore language table so i cannot simply pass the code of the language over here i need to pass the society and i can simply pass the short description which is translated directly over here so let me first do that this is my object and here is my short description now for ticket language what i need to do is first of all i am going to get the society of the language language society equals to i already created a function name get language society so i'm going to call this one here and then i am going to pass the language code which i have the name is this so i have already passed the ticket language now and i have the language society as well so this is pretty much it i have already set the field values now i just need to make sure of couple of other things since i am updating the same record which is getting inserted right now it is not a recommended approach i am going to make sure that i set autofill fields and workflow to false okay so i just need to call this function in my christmas tool and i think it will work fine let's navigate to the incident record now and i'm going to create a new incident let me copy the text so let's set a caller for this ticket i will tutor is the caller of the ticket for now and these fields are empty if i save the incident i am expecting two things right now one is the ticket language which is populated as german and the second thing is short description translated which is also populated in the english language this is in german over here so similarly when somebody populates the resolution information that can also be populated via the same script the only difference is you need to create a an on update business tool and you need to compare the language written over here with this language if the language written here is not already german then it needs to be translated to german and you can get the code of the language from here and accordingly populated relation nodes so this is how it's done by the script and the script which i have shown you for now just uh translates the data and from there you can also get the language which is detected but if you only want to detect the language then we have another function called get detected language which also accepts two parameters uh it would be something like this let me show it to you over here so it won't be it won't need this many parameters it will be simply like that detected language okay and then you can pass that text over here whose language needs to be detected and the second parameter would be the translator object okay so if you're not already aware this is how we pass an object okay so this is pretty much it and thank you for watching
https://www.youtube.com/watch?v=Fu5N30i4Thc