Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 16 of 16

Thread: Post javascript examples here

  1. #1
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default Post javascript examples here

    Javascript in SC 6.x is a great improvement and will make it easier to code reusable.

    Lets start posting usefull javascripts and tips & tricks.

    I'll start with a script I wrote. This script will move attachments from a call, incident, change etc to an activity record. This way it is possible to keep a history of changes in the attachments.

    Code:
    // This script moves attachment from the normal attachment container to an activity record.
    // Will create the activity record and update the attachments.
    //
    // The unique key on the activity dbdict must have "thenumber" as the first field
    // Also add new trigger to run this script. Script is called like this:
    //
    // system.library.ActivityAttachments.moveAttachment( record, oldrecord );
    // 
    function moveAttachment(record, oldrecord)
    {
     var strFileName = system.functions.filename( record );
     var strRecordID;
     var strActivityFilename;
     strRecordID = record.number ;
     switch (strFileName)
     {
      case "probsummary":
       strRecordID = record.number ;
       strActivityFilename = "activity" ;
       break;
      case "cm3r":
       strRecordID = record.number ;
       strActivityFilename = "activitychanges" ;
       break;
      case "incidents":
       strRecordID = record.incident_id ;
       strActivityFilename = "activityservicemgt" ;
       break;
      case "rootcause":
       strRecordID = record.id ;
       strActivityFilename = "activityproblem" ;
       break;
      case "knownerror":
       strRecordID = record.id ;
       strActivityFilename = "activityknownerror" ;
       break;
      default:
       break;
     }
     
     var activityFile = new SCFile(strActivityFilename);
     var sysattachmentsFile = new SCFile("SYSATTACHMENTS");
     var strSqlActivity;
     var strSqlSysattachments;
     var strActivityID = new String;
     var arrDescription = new Array();
     var today = new Date();
     var future = new Date();
     var negdatestamp = new Date();
     var intCount = 2;
     
     future.setDate(1);
     future.setMonth(1);
     future.setFullYear(2200);
     future.setHours(1);
     future.setMinutes(0);
     future.setSeconds(0);
     
     negdatestamp.setTime(future - today);
     
    // Prepare activity record
     activityFile.number = strRecordID;
     activityFile.type = "Attachment Added";
     activityFile.operator = system.functions.operator();
     activityFile.datestamp = today; 
     activityFile.negdatestamp = negdatestamp;
     
     activityFile.doInsert();
     strActivityID = activityFile.thenumber;
     arrDescription[0] = "Attachment(s) Added";
     
    // Modify attachments records in SYSATTACHMENTS to move them to the activity record
     strSqlSysattachments = "topic = \""+strRecordID+"\" and application = \""+strFileName+"\"";
     if (sysattachmentsFile.doSelect( strSqlSysattachments ) == RC_SUCCESS )
      {
          do
           {
            sysattachmentsFile.application = strActivityFilename;
            sysattachmentsFile.topic = strActivityID;
            sysattachmentsFile.doUpdate();
            arrDescription[intCount] = sysattachmentsFile.filename;
            intCount = intCount + 1;
     
           }
          while (sysattachmentsFile.getNext() == RC_SUCCESS);   
      }
     activityFile.description = arrDescription;
     activityFile.doUpdate();
     
    }
    Last edited by tommy; 2006-09-05 at 18:46.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  2. #2
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Hmm. Nobody have javascripts they want to share? Feel free to start a seperate thread with your script.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  3. #3
    Junior Member
    Join Date
    Apr 2007
    Posts
    2

    Default Auto-fill script?

    Hi Tommy,

    I've been a Peregrine user for some time now and I am now looking into writing some scripts that will make our team a little more efficient logging new calls.

    I wonder, is it possible to write an auto-fill script for Peregrine? Something that will open a dialog box where we can enter a customer name, well known fault and then fill all the data in for us? We have known issues within our customer network that happen all the time and constantly logging the same stuff is very time consuming but nonetheless important.

    Any help in the right direction will be most appreciated.

    Cheers
    Scott

  4. #4
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    714

    Default

    Quote Originally Posted by dan00b View Post
    I wonder, is it possible to write an auto-fill script for Peregrine? Something that will open a dialog box where we can enter a customer name, well known fault and then fill all the data in for us? We have known issues within our customer network that happen all the time and constantly logging the same stuff is very time consuming but nonetheless important.
    I wouldn't even think you would need a popup for that. Just add a field to fill from that would have a Problem Code or something, add a new table that has problem codes and then values for the other fields (categorization, description, resolution, etc) and fill from that table.

  5. #5
    Junior Member
    Join Date
    Apr 2007
    Posts
    2

    Default

    Hey glg,

    Thanks for the reply and help.

    I think I may have figured out a system for launching the script/query using HTA's but I have no knowledge of actually querying Peregrine and I do not have admin access to the system either. I have some scripting knowledge (vbs, js) and I see from the Peregrine documentation you can use SpiderMonkey to script.
    My question this time is can I query Peregrine and fill out a form using a script without having admin access and can you give me some examples of this? I have found what I think are some query examples but if I need admin access for this my attempts may prove futile.

    Do you need more info to answer my question?

    Thanks again

  6. #6
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    714

    Default

    Quote Originally Posted by dan00b View Post
    Hey glg,

    Thanks for the reply and help.

    I think I may have figured out a system for launching the script/query using HTA's but I have no knowledge of actually querying Peregrine and I do not have admin access to the system either. I have some scripting knowledge (vbs, js) and I see from the Peregrine documentation you can use SpiderMonkey to script.
    My question this time is can I query Peregrine and fill out a form using a script without having admin access and can you give me some examples of this? I have found what I think are some query examples but if I need admin access for this my attempts may prove futile.

    Do you need more info to answer my question?
    I would first suggest talking to your Peregrine admins and see if they can design something for you. They would be able to put something together completely within the system. That way you're not using an external scripting tool that might get screwed up by a field moving or something like that.

    Without admin access, you can't do this entirely within Peregrine. If you have to do this without the help of your admins, then you're probably best trying to work with some sort of scripting tool that would enter stuff into the fields for you.

    Most of the stuff you see in the Peregrine documentation is written with the assumption that you have admin access.

  7. #7
    Junior Member jherold's Avatar
    Join Date
    Jan 2003
    Location
    Akron, OH
    Posts
    3

    Default Removing symbols from a string

    This code is called from the JavaScript in format control on update. Gets rid of everything in the badChars variable except the double quotes.

    function RemoveChars(inString)
    // Remove all non-letters and numbers from the string
    // as listed in the 'badChars' string
    {
    var iString = "";
    var badChars = " ~`!@#$%^&*()_-+=/?\|]}[{;:,.'";
    var ipos = inString.length;
    var jpos = 0;

    while (jpos < ipos)
    {
    char_pos = badChars.indexOf(inString.charAt(jpos,1));
    if (char_pos == -1)
    {
    iString = iString+inString.charAt(jpos,1);
    }
    jpos ++;
    }
    return(iString);
    }

    //print(RemoveChars("Input _By String`~1@#$%6&8 9"));
    Jim Herold
    jherold@goodyear.com
    Akron, OH USA

  8. #8
    Senior Member
    Join Date
    Jul 2006
    Location
    San Francisco, CA
    Posts
    172

    Default

    Jim--
    Very nice. I was just looking for something like this the other day. This should save me a lot of time.
    Thanks!

  9. #9
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Post Email Validation

    This script validates an Email Adress. It returns true if its correct.
    There a couple of regexp

    [a-z0-9!#$&#37;&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jo bs|museum)\b

    This regex has two parts: the part before the @, and the part after the @.
    There are two alternatives for the part before the @: it can either consist of a series of letters,
    digits and certain symbols, including one or more dots.
    However, dots may not appear consecutively or at the start or end of the email address.
    The other alternative requires the part before the @ to be enclosed in double quotes, allowing any string of ASCII characters between the quotes.
    Whitespace characters, double quotes and backslashes must be escaped with backslashes.

    The part after the @ also has two alternatives. It can either be a fully qualified domain name (e.g. regular-expressions.info),
    or it can be a literal Internet address between square brackets.
    The literal Internet address can either be an IP address, or a domain-specific routing address.

    We get a more practical implementation of RFC 2822 if we omit the syntax using double quotes and square brackets.
    It will still match 99.99% of all email addresses in actual use today.

    [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

    A further change you could make is to allow any two-letter country code top level domain, and only specific generic top level domains.
    This regex filters dummy email addresses like asdf@adsf.adsf. You will need to update it as new top-level domains are added.


    Code:
    function email(a)
    {
    var matcher = new RegExp("[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+(?:[a-zA-Z]{2}|com|org|net|gov|biz|info|name|aero|biz|info|jobs|museum)\\b")
    //var matcher = new RegExp("\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}\\b");
    //var matcher = new RegExp("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$");
    //var matcher = new RegExp("^\\s*([0-1]{0,1}[0-9]|2[0-3])[:\\.,-_\\/;][0-5][0-9]\\s*$")  ; 
    var b = a;
    //print (b);
    var test_return = b.match(matcher);
    //print ("return: " + test_return);
    //print(matcher)
    
    if (test_return == null)
    	return false; // invalid Email Adress
    else
    	return true; // correct Email
    }
    Last edited by tuncay; 2007-11-19 at 14:04.

  10. #10
    Senior Member tuncay's Avatar
    Join Date
    Jul 2004
    Location
    Germany
    Posts
    113

    Post msglog

    This function resumes an insert in msglog:
    Code:
    function msglog(msgoperator, msgclass, msgname, msgtext,device, msgnumber, msglevel)
    {
            //defaults for optional parameter
    	msgoperator=system.functions.nullsub(msgoperator, "x");
    	msgclass=system.functions.nullsub(msgclass, "x");
    	msgname=system.functions.nullsub(msgname, "x");
    	device=system.functions.nullsub(device, "x");
    	msgnumber=system.functions.nullsub(msgnumber, 1);
    	msglevel=system.functions.nullsub(msglevel, 1);
    	//msgtext=system.functions.nullsub(msgtext, "x");
    	
    	var m = new SCFile("msglog");
    	
        m.operator=msgoperator;
        m.msg_class=msgclass;
        m.msg_time=system.functions.tod();
        m.msg_level=msglevel;  // 1 or 2 or3
        m.msg_name=msgname;
        m.msg_text=msgtext;
        m.device=device;
        m.msg_number=system.functions.val(msgnumber, 1);
        m.doInsert();
    }
    Last edited by tuncay; 2007-11-19 at 14:05.

  11. #11
    Junior Member
    Join Date
    Dec 2007
    Posts
    3

    Default

    Hi
    I have done WSDLToJS generated Javascript and i am calling a function in that javascript in my customized script ,where i am trying to set the values an array of objects,can any body provide me a sample scirpt to set the array of object,including script which has to be set
    function ArrayOfLocalPlaceValue( )
    {
    this.$$attributes = new Array();
    this.$$xmlNames = new Array();
    this.$$objNames = new Array();
    this.getName = getName;
    this.getXmlName = getXmlName;
    this.setContent = setContent;
    this.addContent = addContent;
    this.getContent = getContent;
    this.isFault = isFault;
    this.$$elementChildren = new Array();
    this.$$name = "ArrayOfLocalPlaceValue";
    this.$$xmlNames[ "ArrayOfLocalPlaceValue" ] = "ArrayOfLocalPlaceValue";
    this.item = new Array(); // of LocalPlaceValue
    this.$$isArrayType = true;
    this.newInstance = function()
    {
    var newLen = this.item.push( new LocalPlaceValue() );
    return this.item[ newLen-1 ];
    }
    this.LocalPlaceValue = this.item;
    this.$$elementChildren.push( "LocalPlaceValue" );
    this.length = function()
    {
    return this.item.length;
    }
    this.$$elementChildren.push( "item" );
    }

    function LocalPlaceValue( )
    {
    this.$$attributes = new Array();
    this.$$xmlNames = new Array();
    this.$$objNames = new Array();
    this.getName = getName;
    this.getXmlName = getXmlName;
    this.setContent = setContent;
    this.addContent = addContent;
    this.getContent = getContent;
    this.isFault = isFault;
    this.$$elementChildren = new Array();
    this.$$name = "LocalPlaceValue";
    this.$$xmlNames[ "LocalPlaceValue" ] = "LocalPlaceValue";
    this.validFor = new TimePeriod();
    this.$$elementChildren.push( "validFor" );
    this.describingSpecificationKey = new EntitySpecificationKey();
    this.$$elementChildren.push( "describingSpecificationKey" );
    this.subGraphId = new xsd_long();
    this.$$elementChildren.push( "subGraphId" );
    this.lastUpdateVersionNumber = new xsd_long();
    this.$$elementChildren.push( "lastUpdateVersionNumber" );
    }


    and i have set the values of ArrayOfLocalPlaceValue in my customized scirpt.

    Thanks
    Dinakar

  12. #12
    Junior Member
    Join Date
    Jul 2008
    Location
    Belgium
    Posts
    2

    Question script error

    [IMG]file:///C:/DOCUME%7E1/excfn/Temp/moz-screenshot-3.jpg[/IMG]Something must be wrong in this validation script, because I am receiving error (see attachment). Since I am very new to javascript, could someone please explain this error message to me? Or better, tell me what's wrong in this script:

    function by_validate_email_address ( e_mail_address )
    {
    var e_mail_pattern = /^(((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/ ;


    return ( e_mail_pattern.test( e_mail_address ) );
    }


    // DESCRIPTION :
    // =============
    // This function checks the validity of an array of e-mail addresses
    // by calling function by_validate_email_address once for each address.
    // As soon as an invalid (i.e. not well-formed) address is found, the
    // function is ended.

    // PARAMETERS :
    // ============
    // IN : Email address array
    // OUT : true if all addresses are OK, false is at least one is not



    function by_validate_email_address_array ( e_mail_address_list )
    {
    var addresses_OK = false;

    for (var i=0; i<e_mail_address_list.length() ;i++)
    {
    addresses_OK = by_validate_email_address ( e_mail_address_list[i] );
    if ( addresses_OK == true )
    { continue }
    else
    { break }
    }
    return addresses_OK;
    }
    [IMG]file:///C:/DOCUME%7E1/excfn/Temp/moz-screenshot.jpg[/IMG][IMG]file:///C:/DOCUME%7E1/excfn/Temp/moz-screenshot-1.jpg[/IMG][IMG]file:///C:/DOCUME%7E1/excfn/Temp/moz-screenshot-2.jpg[/IMG]
    Attached Images
    Last edited by tommy; 2009-09-05 at 15:47.

  13. #13
    Administrator tommy's Avatar
    Join Date
    Nov 2001
    Location
    Copenhagen
    Posts
    4,272

    Default

    Underscore is a special character. Try removing that from the function name and the variables.
    Best regards Tommy
    Blog - - ITIL certified - Accredited Integration Specialist – HP OpenView Service Management

    Want to keep this site alive? Consider making a donation. Click here.

  14. #14
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    Quote Originally Posted by EXCFN View Post
    function by_validate_email_address ( e_mail_address )
    {
    var e_mail_pattern = /^(((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/ ;

    return ( e_mail_pattern.test( e_mail_address ) );
    }
    I believe you should try this RegEx instead:

    var e_mail_pattern = /^([a-z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-z0-9\-]+\.)+))([a-z]{2,4}|[0-9]{1,3})(\]?)$/i;

    This will fix things for you.

    BTW, Tommy is right, but not quite.
    Underscore is a special character in SC but, having said that,
    it is perfectly alright in JS so as long as there's no mix up, you're OK.

    You're better off writing eMailPattern instead of e_mail_pattern

    ps. try to read some documentation on RegEx, RegEx is fun

  15. #15
    Junior Member
    Join Date
    Jul 2008
    Location
    Belgium
    Posts
    2

    Default symbol ' in email address

    Thank you for the very usefull tips, but can I add the symbol ' to this RegEX? I need it, in order to allow an email address like this one: kim.o'sullivan@helloworld.com.

    I have made this now:
    var e_mail_pattern =/^(((?:[a-zA-Z0-9'_%+-]+\.)+[a-zA-Z0-9'_%+-])|[a-zA-Z0-9'_%+-])+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/ ;

    Not receiving errors anymore...
    But I will try to avoid underscores in the future.

  16. #16
    Senior Member mateuszk's Avatar
    Join Date
    Nov 2006
    Posts
    302

    Default

    Quote Originally Posted by EXCFN View Post
    var e_mail_pattern =/^(((?:[a-zA-Z0-9'_%+-]+\.)+[a-zA-Z0-9'_%+-])|[a-zA-Z0-9'_%+-])+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/ ;
    why not:

    /^(((?:[a-z0-9'_%+-]+\.)+[a-z0-9'_%+-])|[a-z0-9'_%+-])+@(?:[a-z0-9-]+\.)+[a-z]{2,4}$/i;

    ???

    /i switch takes care of the case.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts