+ Start a Discussion
Glenn DalyGlenn Daly 

Creation of many to many record through trigger

When field `review_date__c` on object `knowledge_article__c` changes to a specific date, it should create a new record on object `Incident__c`. I have achieved this with process builder.

However I then wish to be able to create a `Knowledge_Incident_Link__c` record and link the  `knowledge_article__c` record with the `Incident__c`. Ideally I'd like to do this with another process, but what are my options? How can I implement this idea? What steps would I need to take if I create a trigger?

Would somebody be able to get me started as I'm not an apex developer.
Below are the object relationships.

User-added image
v varaprasadv varaprasad
HI Glenn,

Creating  everything with trigger like below: 
Trigger createMTM on knowlwdgArticle(After update){
if(trigger.isAfter && Trigger.isUpdate){
  for(knowlwdgArticle kr : trigger.new){
    if(kr.review_date__c != trigger.oldmap.get(kr.id).review_date__c){
	    incident in = new incident();
		in.somefield = kr.spmefield;
	
	
	    insert in;
		
		Knowledge_Incident_Link__c` ki = new Knowledge_Incident_Link__c`();
		ki.knowlwdgArticlerelation = kr.id;
		Ki.incidentreltio = in.id;
		
		insert ki;
	}  
  }
}
}

Thanks
Varaprasad.
Glenn DalyGlenn Daly
Thanks very much, it  says I have a problem on line 5. It says it's "expecting a semi-colon, found 'in'"

What do i need to change, as when add a semi colon, it fails again!
v varaprasadv varaprasad
Hi Glenn.

based on your req add fields in objects below.
v varaprasadv varaprasad
Trigger createMTM on knowledge_article__c(After update){
list<Incident__c> lstIncidents = new list<Incident__c>();
list<Knowledge_Incident_Link__c> lstkrs = new list<Knowledge_Incident_Link__c>();

if(trigger.isAfter && Trigger.isUpdate){
  for(knowledge_article__c kr : trigger.new){
    if(kr.review_date__c != trigger.oldmap.get(kr.id).review_date__c){
	    Incident__c in = new Incident__c();
		in.field = kr.spmefield;
	     just add field here;
	
	   lstIncidents.add(in);
		
		Knowledge_Incident_Link__c` ki = new Knowledge_Incident_Link__c`();
		ki.knowlwdgArticlerelation = kr.id;
		Ki.incidentreltio = in.id;
		==just add field here==
		lstkrs.add(ki);
	}  
  }
  insert lstIncidents;
  insert lstkrs;
}
}

Thanks
Varaprasad
Glenn DalyGlenn Daly
So i still get the same problem. Please see my code below, some of the field names are slightly different.

Trigger createMTM on BMCServiceDesk__KM_KnowledgeArticle__c(After update){
    list<BMCServiceDesk__Incident__c> lstIncidents = new list<BMCServiceDesk__Incident__c>();
    list<Knowledge_Incident_Link__c> lstkrs = new list<Knowledge_Incident_Link__c>();
    
    if(trigger.isAfter && Trigger.isUpdate){
        for(BMCServiceDesk__KM_KnowledgeArticle__c kr : trigger.new){
            if(kr.review_date__c != trigger.oldmap.get(kr.id).review_date__c){
                BMCServiceDesk__Incident__c in = new BMCServiceDesk__Incident__c();
                in.BMCServiceDesk__incidentDescription__c = kr.id;
                
                lstIncidents.add(in);
                
                BMCServiceDesk__KM_Knowledge_Incident_Link__c ki = new   BMCServiceDesk__KM_Knowledge_Incident_Link__c();
                ki.BMCServiceDesk__Knowledge_Article__c = kr.id;
                Ki.BMCServiceDesk__Incident__c = in.id;
                
                lstkrs.add(ki);
            }  
        }
        insert lstIncidents;
        insert lstkrs;
    }
}