Set document to complete via CRM Script

lock
push_pin
done
Answered
2

Hi Guys,

We have a customer with a lot of uncompleted documents in the database. The users set the documents to uncomplete after thy are created but forget to set them to complete again. (The users should do this but it is hard to learn a old dog a new trick ;-)

The CRM Administrator creates a selection for these documents and set them to complete. There should be an easier way for this and I've created a scheduled script that is searching for open documents so I can set them to complete. The script is working but it sets also documents to complete that are legitimate open. This are locked (Checked out documents).

In the script I use the NSDocumentEntity to set the documents to complete and I've expected that locked documents where left alone, just like in the interface where it is not possible to set a locked document to complete.

I use the folowing script:

//Script to close open documents
//This script will close all documents that are open
//
//Criteria: 
//
//Document is not completed! (Status 1)
//Updated date >1 week in the past
#setLanguageLevel 3;

//Set appointment age in days
Integer documentUpdatedAge = -7;
//Define currentdate
Date currentDate = Date(getCurrentDate());

//Create SearchEngine
SearchEngine se;
se.addFields("appointment","document_id,appointment_id,status,type,updated");

//Check if updated > 1 week compared to current date 
se.addCriteria("appointment.updated", "OperatorLte", currentDate.addDay(documentUpdatedAge).toString());
se.addCriteria("appointment.type", "OperatorEquals", "4"); //ONLY DOCUMENTS!
se.addCriteria("appointment.status", "OperatorEquals", "1"); //ONLY OPEN!
  
//Execute
for (se.execute();!se.eof();se.next())


{
 	//Print lines for Debugging  
	print(se.getField("appointment.document_id"));
	print("\t");
  
  print(se.getField("appointment.status"));
	print("\n");
	//End debugging
  
	//Update document (Set completed = true)
  NSDocumentAgent agent;
	NSDocumentEntity entity;
 	Integer documentId = se.getField("appointment.document_id").toInteger(); 
	
  entity = agent.GetDocumentEntity(documentId);
  entity.SetCompleted(3);
	agent.SaveDocumentEntity(entity);  
}

Is it possible to exclude open documents? In the SDK I've found the GetLockSemantics entity but i'm not sue if I can use this and how I should use this.

Does anyone has some tips for me?

With kind regards,

Alexander Hesselberth
SuperOffice Benelux B.V.

18 Jun 2019 | 12:00 AM

All Replies (2)

Thanks Bas, it is working now :-)

 

19 Jun 2019 | 12:00 AM

Hi Alexander,

 

Please see the script below -> this will check whether the document is locked for editing or not.

 

#setLanguageLevel 3;

	Integer documentId = 133; 

    NSDocumentAgent agent;
	NSDocumentEntity entity;
	NSCheckoutInfo info;

    entity = agent.GetDocumentEntity(documentId);
	info = agent.GetCheckoutState(documentId);

	Integer associate = info.GetAssociateId();
	Integer state = info.GetState();

	if (state == 0) {
    entity.SetCompleted(3); // not locked - set to complete
  	agent.SaveDocumentEntity(entity);  
  }

19 Jun 2019 | 12:00 AM

Add reply