Create new Sync Job Queue entry for the new table mapping record.

I am integrating BC and Sales CRM. Currently, I have created a custom table mapping and field mappings to the custom table in CRM. The manual sync works fine. All data can be synced as expected.

Now I want to automate this process. I want to create a custom Job Queue entry to the scheduled sync through AL extension.

Can you please guide me how to do this?

	//---get the IntegrationTable Mapping record with the name.  if the record is found create the Job queue entry	
		TableMappingRec."Name" := 'VENDOR-VENDOR';
        if TableMappingRec.Find('=') then begin
            Message('About to create Job Queue entry for ' + TableMappingRec.Name);
            CreateCustomSyncJobQueueEntry(TableMappingRec);
        end	
			
			
			
			
			
	procedure CreateCustomSyncJobQueueEntry(TableMappingRec: Record "Integration Table Mapping")
    var
        JobQueueEntry: Record "Job Queue Entry";
    begin


        JobQueueEntry.SetRange("Object Type to Run", JobQueueEntry."Object Type to Run"::Codeunit);
        JobQueueEntry.SetRange("Object ID to Run", Codeunit::"Integration Synch. Job Runner");
        JobQueueEntry.SetRange("Record ID to Process", TableMappingRec.RecordId());
        JobQueueEntry.DeleteTasks();

        JobQueueEntry.InitRecurringJob(10);
        JobQueueEntry."Object Type to Run" := JobQueueEntry."Object Type to Run"::Codeunit;
        JobQueueEntry."Object ID to Run" := Codeunit::"Integration Synch. Job Runner";
        JobQueueEntry."Record ID to Process" := TableMappingRec.RecordId();
        JobQueueEntry."Run in User Session" := false;
        JobQueueEntry.Description := 'VENDOR-VENDOR - Custom Sync Job';
        JobQueueEntry."Maximum No. of Attempts to Run" := 10;
        JobQueueEntry.Status := JobQueueEntry.Status::Ready;
        JobQueueEntry."Rerun Delay (sec.)" := 30;
        JobQueueEntry."Inactivity Timeout Period" := 5;
        JobQueueEntry."Recurring Job" := true;


        JobQueueEntry.Insert(true);

    end;

With the above code, I managed to create job queue entry for the custom table. Not sure if this the appropriate way to create but it has resolved the issue.

There is a standard function on the “CRM Setup Defaults” table named “CreateJobQueueEntry”. You only need to call this with your Integration Table Mapping record and it will create it for you. This should be set when you create the record via the events for the default setup.

For example:

CRMSetupDefaults.CreateJobQueueEntry(IntegrationTableMapping);

Thanks Jon for your response. As I use it directly as you have suggested, I am getting the following message while running the Default Sync setup and the job entry not getting created. That was the reason, I recreated the method and call it to create the job queue entry and it works fine.

The following AL methods are limited during write transactions because one or more tables will be locked: Form.RunModal is not allowed in write transactions. Codeunit.Run is allowed in write transactions only if the return value is not used. For example, ‘OK := Codeunit.Run()’ is not allowed. Report.RunModal is allowed in write transactions only if ‘RequestForm = false’. For example, ‘Report.RunModal(…,false)’ is allowed. XmlPort.RunModal is allowed in write transactions only if ‘RequestForm = false’. For example, ‘XmlPort.RunModal(…,false)’ is allowed. Use the commit method to save the changes before this call, or structure the code differently. Contact your application developer for further assistance.