Integrate > Service Manager integration methods and tools > Web Services > SOAP API > Consuming a Service Manager Web Service > Sample script to send a record with attachments within Service Manager

Sample script to send a record with attachments within Service Manager

The sample script below sends a record with attachments from Service Manager to ServiceManager. First, you will need to have a generated JavaScript for both Service Managers: Service Manager 1, called IncidentManagement, and Service Manager 2, called IncidentManagementTarget.

Note The lines in bold font perform the attachment handling.

try
{
	//STEP 1 --> RETRIEVE AN EXISTING INCIDENT HAVING ATTACHMENT(S)	
       //THIS EXAMPLE USE "IM10010", PLEASE ADD ONE OR MORE ATTACHMENTS TO THIS INCIDENT IF NO
	var imService = new lib.IncidentManagement.IncidentManagement();
	var request = new lib.IncidentManagement.RetrieveIncidentRequest();
	request.model.instance.IncidentID.setValue( "IM10010" );
	request.attachmentInfo = true;
	request.attachmentData = true;
  
	imService.user = "falcon";
	imService.pwd = "";
  
	var incidentResp = imService.invoke( request );
	if ( incidentResp.isFault() )
	{
		print ( incidentResp.detail );
	}
	else 
	{  
		print( "SM OOB Test: Retrieved incident:" + incidentResp.model.instance.IncidentID.getValue());
	}
       //STEP2 -->CREATE A NEW INCIDENT BASED ON THE RETRIEVED INCIDENT 
       //USING 'IncidentManagement' WEB SERVICE
	var imService2 = new lib.IncidentManagementTarget.IncidentManagement(); 
	var attach
	var newAttach = new Array();
	for (var i=0; i<incidentResp.model.instance.attachments.attachment.length; i++)
	{
		attach = imService.attachments[i];
		attach.href = "test_"+i;
		attach.action="add";
		newAttach.push( attach );			
	}
   
	var createIM = new lib.IncidentManagementTarget.CreateIncidentRequest();
	imService2.setAttachments( newAttach );
	imService2.user = "falcon";
	imService2.pwd = "";

	createIM.attachmentData = true;
	createIM.attachmentInfo = true;
			
	createIM.model.instance.OpenedBy.setValue(incidentResp.model.instance.OpenedBy.getValue());
	createIM.model.instance.Title.setValue(incidentResp.model.instance.Title.getValue());
	createIM.model.instance.Category.setValue(incidentResp.model.instance.Category.getValue());
	createIM.model.instance.Area.setValue(incidentResp.model.instance.Area.getValue());
	createIM.model.instance.Subarea.setValue(incidentResp.model.instance.Subarea.getValue());
	createIM.model.instance.Impact.setValue(incidentResp.model.instance.Impact.getValue());
	createIM.model.instance.Urgency.setValue(incidentResp.model.instance.Urgency.getValue());
	createIM.model.instance.AssignmentGroup.setValue(incidentResp.model.instance.AssignmentGroup.getValue());
	createIM.model.instance.Service.setValue(incidentResp.model.instance.Service.getValue());
	
	for (var i=0;i<newAttach.length;i++)
	{
		var attachmentXml    = createIM.model.instance.attachments.attachment_newInstance();
	        attachmentXml.action = newAttach[i].action;
	        attachmentXml.name   = newAttach[i].name;
	        attachmentXml.type   = newAttach[i].type;
	        attachmentXml.len    = newAttach[i].value.length;
	        attachmentXml.attachmentType = attach.attachmentType;			
	}
   
	for (var i=0;i<incidentResp.model.instance.Description.Description.length; i++)
	{
		newDescriptionLine = createIM.model.instance.Description.Description_newInstance();
	        newDescriptionLine.setValue(incidentResp.model.instance.Description.Description[i].getValue());				
	}
	response = imService2.invoke(createIM);
   
	if ( response.isFault() )
	{
		print ( "SM OOB Test: Outgoing request failed with error:" + response.detail );
	}
	else 
	{  
		if ( response.messages.message.length; i++ )
		{
			print ( "SM OOB Test: Outgoing request returned message:" 
	                          + response.messages.message[i].getValue() );
		}   
   
	}
       print("Incident"  + incidentResp.model.instance.IncidentID.getValue()
	     + "was retrieved from" + imService.getHost() + ":" + imService.getPort()
	     + "and copied to new incident "
	     + response.model.instance.IncidentID.getValue()
	     + "on" + imService2.getHost() + ":" + imService2.getPort());
}
catch( e )
{
	print( "SM OOB Test: Outgoing request returned message: " 
	                   + response.messages.message[i].getValue() ); );
}