HH Create Demand when Idea is accepted

PHOTO EMBED

Tue Mar 22 2022 17:48:42 GMT+0000 (Coordinated Universal Time)

Saved by @taracatac #javascript

//run before update
//condition  = current.state == 2 && current.demand.nil() && previous.state != 2

// gs.hasRole('demand_manager')  && current.state == 2 && current.demand.nil() && previous.state != 2
var demandTable = "dmn_demand";
if(GlidePluginManager.isActive('com.snc.project_management_v3')){
	demandTable = SNC.PPMConfig.getDemandTable(current.getTableName());
}

var demand = new GlideRecord(demandTable);
demand.initialize();
var fields = ['business_case', 'short_description', 'submitter', 'sys_domain', 'business_unit', 'department', 'impacted_business_units', 'business_capabilities', 'business_applications'];
for(var i in fields){
	var field = fields[i];
	if(demand.isValidField(field)){
		demand.setValue(field, current.getValue(field));
	}
}
//carry over and_or set feilds from idea to demand
demand.setValue("category","strategic");
demand.setValue('u_domain',current.module);
demand.setValue("type", "enhancement");
demand.setValue("parent", current.sys_id);
demand.setValue("idea", current.sys_id);
demand.setValue('u_sub_domain',getSubDomain(current.sys_id));
demand.setValue('assignment_group', current.u_it_assignment_group);
demand.setValue('priority',current.priority);
demand.setValue('u_growth',current.u_growth2);
demand.setValue('u_quality_safety',current.u_quality_and_safety2);
demand.setValue('u_financial',current.u_financial2);
demand.setValue('u_ppl',current.u_people2);
demand.setValue('u_it_wrk_effort',current.u_analyst_work_effort);
demand.setValue('u_complexity',current.u_complexity_of_coordination);
demand.setValue('u_training',current.u_training_needs);
demand.setValue('u_cust_wrk_effort',current.u_r_d_focus);
demand.setValue('u_prj_advanatages',current.u_epic_advantages);
demand.setValue('u_targeted_date',current.u_target_date);
demand.setValue('u_add_justification',current.u_additional_justification);
demand.setValue('u_est_loe',current.u_est_loe);//NG added 1.20.22 NOAH separation requirements

//since impacted portfolio is a list field you need to convert the values to strings in order to carry them over to the new record
var impactedPort = current.u_impacted_portfolio.toString();
if(impactedPort != ''){
demand.setValue('u_impacted_portfolios',impactedPort);	
}


demand.work_notes = current.work_notes.getJournalEntry(-1);
demand.work_notes = current.comments.getJournalEntry(-1);

var desc = current.idea_description; 
var regX = /<\/?[^>]+(>|$)/g;
var regxFormatted = desc.replace(regX, " "); //format desc with above regex
var spaceFormatted = regxFormatted.replaceAll('&nbsp;', " "); //replace all nbsp html characters with a space
finalDesc = spaceFormatted.replaceAll('&ndash;', "-"); // replace all ndash encoded html w/ ascii dash
demand.setValue("description", finalDesc);

if(GlidePluginManager.isActive('com.snc.apm')){
	demand.setValue("size", current.effort);
	if(current.pm_program)
		demand.setValue("primary_program", current.pm_program);
	if(current.start_by_fiscal_year)
		demand.setValue("start_date", new GlideDateTime(current.start_by_fiscal_year.fiscal_start_date_time).date);
	if(current.implement_by_fiscal_year)
		demand.setValue("requested_by", new GlideDateTime(current.implement_by_fiscal_year.fiscal_end_date_time).date);
	
	var demandPrice = parseFloat(current.estimated_benefit.getReferenceValue());
	demand.setValue("financial_benefit",  current.estimated_benefit.getReferenceCurrencyCode() + ';' + demandPrice);
}

var dmnId = demand.insert();
demand.get(dmnId);
current.demand = dmnId;
current.stage = 'demand';
GlideSysAttachment.copy('idea', current.sys_id, 'dmn_demand', demand.sys_id); // second paramenter is the current record and new record you created
var link = ' <a href ="/' + demandTable + '.do?sysparm_query=number%3D' + demand.getValue('number') + '">'+ demand.getValue('number') +'</a>';
var message = gs.getMessage("Demand {0} has been created");
	message = message.replace("{0}", link);
		
		gs.addInfoMessage(message);


function getSubDomain(id){
var gr = new GlideRecord('im_m2m_idea_category');
	gr.addQuery('idea',id);
	gr.query();
	if(gr.next()){
		return gr.category_id;
	}
	
}
content_copyCOPY