Script Button

<< Click to Display Table of Contents >>

Navigation:  Using JavaScript >

Script Button

Script Action Button - Sample Code

Scenario: Update value of "Risk Level" column based on value of columns "Impact" and "Probability of Failure". These columns are all on the same SharePoint list.

Overview: A Script Action button is placed the form. In this example, it is used to set value of a column on the Form based on values of other columns. Script action processing should finish within 30 seconds; otherwise, the Forms code continues with the processing without waiting for script action result.

At the end of processing, script action must call 'functionCallback' to return control back to the NITRO Forms. There are three ways to call this function:

1.Success and no message to be shown to user: functionCallback();

2.Success and show a message to user: functionCallback(false, 'success message text');

3.Failure and show a message to user: functionCallback(true, 'failure message text');

 

 

// Parameters:

 // formContext: NITRO Form context

 // listColumnsInfo: Object with key as the internal name of column and value as list column meta data

 // currentItem: SharePoint object for the current list item on which action is being executed

 // functionCallback: To be called after finishing the processing. Called automatically after the 30 seconds if not called

 

try{

   // Add check to see if Impact exists

   if(listColumnsInfo.hasOwnProperty("Impact")){

     if(listColumnsInfo.hasOwnProperty("ProbabilityofFailure")){

       if(listColumnsInfo.hasOwnProperty("RiskLevel")){

         // Get the value of Impact from Form.

         var Impact = formContext.fetchColumnValueUI("Impact");

         // Get the value of Probability of Failure from Form.

         var probabilityofFailure = formContext.fetchColumnValueUI("ProbabilityofFailure");  

         var RiskLevel  = "Low";

 

         if(Impact == "Low" && probabilityofFailure == "Low"){

           RiskLevel = "Low";

         }else if(Impact == "Low" && probabilityofFailure == "Normal"){

           RiskLevel = "Normal";

         }

         else if(Impact == "Low" && probabilityofFailure == "High"){

           RiskLevel = "Normal";

         }

         else if((Impact == "Normal" && probabilityofFailure == "Normal")

                 || (Impact == "Normal" && probabilityofFailure == "High")

                 || (Impact == "Normal" && probabilityofFailure == "Low")){

           RiskLevel = "Normal";

         }

         else if(Impact == "High"){

           RiskLevel = "High";

         }

         formContext.setColumnControlValueByName("RiskLevel",RiskLevel);

         functionCallback();

       }

       else{

         //true indicates error occured and second parameter is used for error message will be shown below the button

         functionCallback(true, "'Risk Level' column does not exist");

       }

    }else{

       //true indicates error occured and second parameter is used for error message will be shown below the button

       functionCallback(true, "'Probability of Failure' column does not exist");

     }

   }

   else {

     //true indicates error occured and second parameter is used for error message will be shown below the button

     functionCallback(true, "'Impact' column does not exist");

   }

}

catch(ex){

   //true indicates error occured and second parameter is used for error message will be shown below the button

   functionCallback(true, ex.message);

}