+ Start a Discussion
Salesforce2015Salesforce2015 

Apex code help

Hi Experts,
 
We are using "Project" and "Charge Code" Custom objects.
We create projects and for each project 10 charge codes common.
Our end user after creation of project, he manually adds 10 charge codes to project.

Scenario: After creation of project automatically those ten charge code records added to project record.

Please find below is Project record with charge code records.

Project Record
 
Thanks,
Manu
Best Answer chosen by Salesforce2015
ManojjenaManojjena
Hi Mannu,

Try to use your API names as you have in system and implement belwo code .
trigger CreateChargeAutomatically On Project(after insert ){
 Set<Id> projectidWherechageCodeExist=new Set<Id>();
 Set<Id> prjIdSet=new Set<Id>();
 for(project prj :trigger.new ){
   prjIdSet.add(prj.Id);
 }
 List<ChargeCodeWhereNeedsToInsert> chargeCodeList=[SELECT Id,ProjectId FROM ChargeCodeWhereNeedsToInsert WHERE ID IN :prjIdSet];
 if(chargeCode.size() > 0){
   for(ChargeCodeWhereNeedsToInsert chr :chargeCodeList){
     projectidWherechageCodeExist.add(chr.ProjectId);
   }
 }
 prjIdSet.removeAll(projectidWherechageCodeExist);
 List<ChargeCodeWhereNeedsToInsert> listToCreate=new List<ChargeCodeWhereNeedsToInsert>();
 For(Project prj :[SELECT id FROM Project WHERE ID IN : prjIdSet]){
   for(ChareCodeWhichNeedsToRefer chrge : [SELECT id,ChargeCode FROM ChareCodeWhichNeedsToRefer]){
     ChargeCodeWhereNeedsToInsert chrgg=new ChargeCodeWhereNeedsToInsert();
	 chrgg.Code=chrge.Code;
	 chrgg.projectId=prj.Id;
	 listToCreate.add(chrgg);
	 
   }
 }
 try{
 insert listToCreate;
 }catch(DmlException de){
   System.debug(de);
 }
 
}

Let me know any issue .

All Answers

Darshan Shah2Darshan Shah2
Hello Manu99,

Write one trigger on Project object

trigger ProjectInsert on Project (after insert)
{
     // write for loop which will create 10 records of 'Charge Code' with parent id of 'Project'.
     // In for loop add that records in list
     // outside the for loop, insert all records of 'Charge Code'.
}

Let me know whether it helps you.

Warm Regards,
Darshan
 
Mudasir WaniMudasir Wani
Hello Manu,

The above code will definitely help.
But you your code may look ordinary because you need to add 10 values which may require many if statements
I would prefer to use a list custom setting.
Which is also utilized for dynamic purpose.
Also a part of best practice.

http://blog.jeffdouglas.com/2010/01/07/using-list-custom-settings-in-salesforce-com/

https://help.salesforce.com/HTViewHelpDoc?id=cs_accessing.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=cs_accessing.htm&language=en_US)

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm


Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
ManojjenaManojjena
Hi Manu ,

What is your requirment here .Your post has not mentioned any issue . 
Salesforce2015Salesforce2015
Hi All,

Thanks for quick response.

Manoj - If user creates a new project, project associated child object (Lookup) i.e. Charge Code Object 10 records should add automated. Not add those 10 records in manual way. 10 child records common for all projects.

Mudasir Wani - I'll look into your advice. If I need more information, I’ll reply you.

Thanks,
Manu
ManojjenaManojjena
HI manu ,

What I observe in your image the charge code name is auto number an dcharge codes is the value you need to enter ,Please correct me if I am wrong .
So are the chnage code for all project same .,If so please share teh chare code .
 
Salesforce2015Salesforce2015

Hi Manoj,

in our sfdc org, we have two objects name as Charge Codes, But Api name is different.

One Charge Code - contains all 10 records. (Here charge codes denotes with Name. Ex: Time, Car Rental, Food, Gas etc.)
Second Charge Code - it is child of Project. We manually add those 10 charge code recods in to here from other charge code object. (Here Charge Codes are automated no. Ex: CC-002264, CC-002265 etc)

Thanks,
Manu
ManojjenaManojjena
Hi Mannu,
Share me the API names of objects and related field API names .
ManojjenaManojjena
Hi Mannu,

Try to use your API names as you have in system and implement belwo code .
trigger CreateChargeAutomatically On Project(after insert ){
 Set<Id> projectidWherechageCodeExist=new Set<Id>();
 Set<Id> prjIdSet=new Set<Id>();
 for(project prj :trigger.new ){
   prjIdSet.add(prj.Id);
 }
 List<ChargeCodeWhereNeedsToInsert> chargeCodeList=[SELECT Id,ProjectId FROM ChargeCodeWhereNeedsToInsert WHERE ID IN :prjIdSet];
 if(chargeCode.size() > 0){
   for(ChargeCodeWhereNeedsToInsert chr :chargeCodeList){
     projectidWherechageCodeExist.add(chr.ProjectId);
   }
 }
 prjIdSet.removeAll(projectidWherechageCodeExist);
 List<ChargeCodeWhereNeedsToInsert> listToCreate=new List<ChargeCodeWhereNeedsToInsert>();
 For(Project prj :[SELECT id FROM Project WHERE ID IN : prjIdSet]){
   for(ChareCodeWhichNeedsToRefer chrge : [SELECT id,ChargeCode FROM ChareCodeWhichNeedsToRefer]){
     ChargeCodeWhereNeedsToInsert chrgg=new ChargeCodeWhereNeedsToInsert();
	 chrgg.Code=chrge.Code;
	 chrgg.projectId=prj.Id;
	 listToCreate.add(chrgg);
	 
   }
 }
 try{
 insert listToCreate;
 }catch(DmlException de){
   System.debug(de);
 }
 
}

Let me know any issue .
This was selected as the best answer
Salesforce2015Salesforce2015
Hi Manoj,
 
I did some changes given code. Trigger code successfully work.
Below is a final code change.

trigger CreateChargeCodeAutomatically on MPM4_BASE__Milestone1_Project__c (after insert) {
    
    Set<Id> projectidWherechageCodeExist= new Set<Id>();
    Set<Id> prjIdSet= new Set<Id>();
    for(MPM4_BASE__Milestone1_Project__c prj :trigger.new){
       prjIdSet.add(prj.Id);
    }

    List<Charge_Code__c> chargeCodeList=[SELECT Id,Project__r.Id FROM Charge_Code__c WHERE ID IN :prjIdSet];
    if(chargeCodeList.size() > 0){
       for(Charge_Code__c chr :chargeCodeList){
         projectidWherechageCodeExist.add(chr.Project__c);
       }
    }

    prjIdSet.removeAll(projectidWherechageCodeExist);
    
    List<Charge_Code__c> listToCreate=new List<Charge_Code__c>();
    
    for(MPM4_BASE__Milestone1_Project__c prj :[SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE ID IN : prjIdSet]){
       
       for(Project_Charge_Code__c chrge : [SELECT Id,Name FROM Project_Charge_Code__c ]){
            
            Charge_Code__c chrgg=new Charge_Code__c();
            chrgg.Charge_Code__c = chrge.Id;
            chrgg.Project__c = prj.Id;
            listToCreate.add(chrgg);
                   
       }
    }
    
    try{
        insert listToCreate;
    }
    catch(DmlException de){
       System.debug('@@@@SFDC::'+de);
    }

}

Thanks,
Manu