Enabling resources (based on skills)on Project creation

Jira Code: CN-22
This script is for auto-filling resource allocation of the project with all resources from sales centre and employee centre and the employees(skill based) with the check for resource checkbox checked, on the creation of project task.

User Event Script : CN-22 JJ UE Resource Allocation(skill based)

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
* CLIENTNAME:C2 NATIVE
* CN-6
* Project Page Customization
* **************************************************************************
* Date : 03-11-2018
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : This script is for autofilling resource allocation of project with all resources from sales centre and employee centre 
*                       and the employees(skill based) with the check for resource checkbox checked, on the creation of project task
* Date created : 03-11-2018
*
* REVISION HISTORY
*
* Revision 1.0 ${30-08-2018} nd : created
* Revision 1.1 ${03-11-2018} nd : updated						
* 
*
******************************************************************************/
define(['N/record','N/search'],

function(record,search) {
   


    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @param {string} scriptContext.type - Trigger type
     * @Since 2015.2
     */
    function afterSubmit(scriptContext) {
    	try{
    		if(scriptContext.type == 'create'){
    		
    		var resourceAlloc = scriptContext.newRecord;
        	//create search
    		var employeeSearchObj = search.create({
    			   type: "employee",
    			   filters:
    			   [
    			      ["role.centertype","anyof","EMPLOYEE","SALESCENTER"], 
    			      //updated to include the extra employees added based on skill
    			      "OR", 
    			      ["custentity_check_for_resource","is","T"]
    			   ],
    			   columns:
    			   [
    			      search.createColumn({name: "internalid", label: "Internal ID"})
    			   ]
    			});
        	var searchResult = employeeSearchObj.run().getRange({
        		start:0,
        		end:100
        	});

        	for(var i=0;i<searchResult.length;i++){
        		var singleResult = searchResult[i];
        		var internalId = singleResult.getValue({
       			 name: "internalid"
       		});

       		if((internalId != 699)&&(internalId != 830)){
       			var allocRecord = record.create({
           		    type: record.Type.RESOURCE_ALLOCATION, 
           		    isDynamic: true
              		});
           		allocRecord.setValue({
           		    fieldId: 'allocationresource',
           		    value: internalId
                  		});
           		
           		allocRecord.setValue({
           		    fieldId: 'project',
           		    value: scriptContext.newRecord.id
                  		});
           		allocRecord.setValue({
           		    fieldId: 'allocationamount',
           		    value: 1
                  		});
     		
           		allocRecord.save({
           		    enableSourcing: true,
           		    ignoreMandatoryFields: true
           		});
        		}
        		
        	}
    		}	

    	}catch(e){
    		log.error('error',e)
    	}
    	
    }

    return {
        
        afterSubmit: afterSubmit
    };
    
});

Leave a comment

Your email address will not be published. Required fields are marked *