Integration Sample Script to vWorkApp

Integration Sample Script to vWorkApp

vWorkApp is a thrid party schedulling application for job management. 

http://www.vworkapp.com/dispatch-and-scheduling.html

Below is an scripting example of Job output to vWorkApp

This is a basic script that can be amended to suit your requirements:

 

{vWork sample for version 2.0 vWork API}

{vWork api docs here: http://api.vworkapp.com/_index.html}

 

var

  XMLDoc, XMLResult: TNativeXML;

  IdNode: TXMLNode;

  OrderNumber, VWorkID, ExistingVWorkID, ReturnString: String;

  Qry_JobHeader, Qry_JobTasks : TpFIBQuery;

  DebugList: TStringList;

const

  APIKey = 'H9XQxBXLi9DfyMkjdZZH'; //put your vWork API Key here

  APIPath = 'http://api.vworkapp.com/2.0/';

 

begin

  DebugList := TStringList.Create;

  XMLDoc :=  TNativeXML.Create(nil);

  XMLResult :=  TNativeXML.Create(nil);

  OrderNumber := GetSourceFieldValue('ORDERNUMBER','HEADER');

  Qry_JobHeader := SQLToTpFIBQuery('Select * from JOBHEADER where ORDERNUMBER=''' + OrderNumber + '''');

  Qry_JobTasks := SQLToTpFIBQuery('Select TASKNAME, TASKDESCRIPTION from JOBTASKS where ORDERNUMBER=''' + OrderNumber + ''' order by TASKSEQUENCE');

  if trim(OrderNumber) = '' then exit;

  ExistingVWorkID := trim(GetSourceFieldValue('ADDITIONALFIELD_1','HEADER'));

  {If using ADDITIONALFIELD_1 for VWorkID then can check it first}

  {Could/Should also have a check here to see if the order still exists in vwork by using: [GET] /api/2.0/jobs/[id].xml?[arguments]}

  if ExistingVWorkID <> '' then

  begin

    if MessageDlg('A VWork Job exists for this Job Order, Yes to update or No to Quit',mtInformation,mbYes+mbNo,0) <> mrYes then

      exit;

  end;

  ShowWaiting('Sending to vWork...');

  try

   {Root Element}

   XMLDoc.XmlFormat := xfReadable;

   XMLDoc.Root.Name := 'job';

    with XMLDoc.Root.NodeNew('customer_name') do begin

      Value := Qry_JobHeader.FN('CUSTOMER').AsString;

    end;

    with XMLDoc.Root.NodeNew('template_name') do begin

      Value := '';

    end;

    with XMLDoc.Root.NodeNew('worker_id') do begin

      Value := '';

    end;

    with XMLDoc.Root.NodeNew('planned_duration') do begin

      if Qry_JobHeader.FN('ESTIMATEDDURATION').IsNull then

         Value := '0'

      else

      if Qry_JobHeader.FN('DURATIONSCALE').AsString = 'Hours' then

         Value := (floattostr((Qry_JobHeader.FN('ESTIMATEDDURATION').Value * 60) * 60))

      else {minutes}

         Value := (floattostr(Qry_JobHeader.FN('ESTIMATEDDURATION').Value * 60));

    end;

    with XMLDoc.Root.NodeNew('steps') do begin

      AttributeAdd('type','array');

      while not Qry_JobTasks.Eof do

      begin

        {Step}

        with NodeNew('Step') do begin

          WriteString('Name',(Qry_JobTasks.FN('TASKNAME').AsString + ': ' + Qry_JobTasks.FN('TASKDESCRIPTION').AsString));

          with NodeNew('location') do begin

            with NodeNew('formatted_address') do begin

              Value := Qry_JobHeader.FN('ORDERADDRESS1').AsString

                + ',' + Qry_JobHeader.FN('ORDERADDRESS2').AsString

                  + ',' + Qry_JobHeader.FN('ORDERADDRESS3').AsString;

            end;

            WriteString('lat','30');

            WriteString('lng','40');

          end;

        end;

        Qry_JobTasks.Next;

      end;

    end;

 

    {Custom Fields}

    with XMLDoc.Root.NodeNew('custom_fields') do begin

      AttributeAdd('type','array');

      with NodeNew('custom_field') do begin

        WriteString('name','Job No.');

        WriteString('value',OrderNumber);

      end;

      with NodeNew('custom_field') do begin

        WriteString('name','Notes');

        WriteString('value',Qry_JobHeader.FN('ORDERNOTES').AsString);

      end;

    end;

 

    {Debug/view XML: uncomment this}

    {DebugList.Text := XMLDoc.WriteToString;

    DebugList.SaveToFile(OstendoPath + 'vworkxml.txt');

    run(OstendoPath + 'vworkxml.txt');

    exit;}

 

    {other http script functions available are HttpDelete and HttpGet to see use F2}

    if ExistingVWorkID <> '' then

      ReturnString := HttpPut(APIPath + 'jobs/' + ExistingVWorkID + '.xml?api_key=' + APIKey, XMLDoc.WriteToString, 'application/xml; charset=UTF-8')

    else

      ReturnString := HttpPost(APIPath + 'jobs.xml?api_key=' + APIKey, XMLDoc.WriteToString,'application/xml; charset=UTF-8');

    {End progress before showmessage}

    EndProgress;

    if ExistingVWorkID <> '' then

      Showmessage('VWork-ID: ' + ExistingVWorkID + ' was updated')

    else

    begin

      {Debug/view Return XML: uncomment this}

      {showmessage(ReturnString);

      DebugList.text := ReturnString;

      DebugList.SaveToFile(OstendoPath + 'vworkxml.xml');

      run(OstendoPath + 'vworkxml.xml');

      exit;}

 

      {Process Results}

      XMLResult.ReadFromString(ReturnString);

      IdNode := XMLResult.Root.FindNode('id');

      if IdNode <> nil then

        VWorkID := IdNode.Value;

      {Prob you would save this to the job header additional field so there is a link between Ostendo and VWork}

      ExecuteSQL('update JOBHEADER set ADDITIONALFIELD_1=''' + VWorkID + ''' where ORDERNUMBER=''' + OrderNumber + '''');

      Showmessage('VWork-ID: ' + VWorkID + ' was created');

      RelatedScreenRefreshData;

    end;

  finally

    {Ensure progress is ended}

    EndProgress;

    DebugList.Free;

    XMLResult.Free;

    XMLDoc.Free;

  end;

end.