function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nuno.CarvalhoNuno.Carvalho 

Hello, i need some help

I need to copy a currency field on the standard object “contract” called salary__c to the right player on the costum object “players__c” and the field has the same name. I got 5 record types on contract , the one with that field is Transfer.
Thanks in advance.
Nuno C
Ahmad J. KoubeissyAhmad J. Koubeissy
so you have a lookup field on the contract object to player__c ? i mean the player object is the parent and contract is the child ?
Shruti SShruti S
Hey Nunu,

Please let me know if the objects are related. If so can you tell me the type of relationship ? Who is the Parent/Child ?

Happy to help,
Shruti
Nuno.CarvalhoNuno.Carvalho
Hey Shruti,
The parent object is Contract and the child object is players__c,
Tell me if you need more info.
Nuno.CarvalhoNuno.Carvalho
Did you understand everything Shruti?
Or do you need some more info?
Ahmad J. KoubeissyAhmad J. Koubeissy
if many players are related to this contract, how do you to which player you copy the currency ?
Nuno.CarvalhoNuno.Carvalho
Hi Ahmad,
Each contract is unique for each player, they just have different record types.
Ahmad J. KoubeissyAhmad J. Koubeissy
try this code:
 
​trigger playerbeforeInsert on players__c(before insert){
     set<contract> ContractsID = new set<contract>();
     string RecordTypeId = [Select Id 
                                      From RecordType 
                                      where sObjectType = 'Contract' and DeveloperName = 'Transfer' limit 1].id;
     for(players__c player: trigger.new){
           if(player.recordtypeid == RecordTypeId ){
                ContractsID.add(player.contract__c);//where contract is the lookup field
           }
     }
     map<id,double> mapcontracttosalary = new map<id,double>();
     for(contract contract:[select id,salary__c  from contract where id in:ContractsID){
          mapcontracttosalary.put(contract.id,contract.salary__c);
     }
     for(players__c player: trigger.new){
          player.salary__c  = mapcontracttosalary.get(player.contract__c);
     }
}

Mark as best answer if this solves your problem.
Nuno.CarvalhoNuno.Carvalho
Hi Koubeissy,
It shows an error on line 3 , on RecordTypeId,
I tried to change the name , but it wont work...
Ahmad J. KoubeissyAhmad J. Koubeissy
Attach the error plz
Nuno.CarvalhoNuno.Carvalho
Now it shows a different error, 

Compile Error: unexpected syntax: 'missing RSQUARE at ')'' at line 12 column 99.
Ahmad J. KoubeissyAhmad J. Koubeissy
fix:
​trigger playerbeforeInsert on players__c(before insert){
     set<contract> ContractsID = new set<contract>();
     string RecordTypeId = [Select Id 
                                      From RecordType 
                                      where sObjectType = 'Contract' and DeveloperName = 'Transfer' limit 1].id;
     for(players__c player: trigger.new){
           if(player.recordtypeid == RecordTypeId ){
                ContractsID.add(player.contract__c);//where contract is the lookup field
           }
     }
     map<id,double> mapcontracttosalary = new map<id,double>();
     for(contract contract:[select id,salary__c  from contract where id in:ContractsID]){
          mapcontracttosalary.put(contract.id,contract.salary__c);
     }
     for(players__c player: trigger.new){
          player.salary__c  = mapcontracttosalary.get(player.contract__c);
     }
}

 
Nuno.CarvalhoNuno.Carvalho
Hello once again Koubeissy,
Now it shows the error shown before, "Compile Error: Variable does not exist: recordtypeid at line 7 column 22".
Ahmad J. KoubeissyAhmad J. Koubeissy
Sorry for the error, i fixed the code:
 
trigger playerbeforeInsert on players__c(before insert){
     set<contract> ContractsID = new set<contract>();
     for(players__c player: trigger.new){
            ContractsID.add(player.contract__c);//where contract is the lookup field
     }
     map<id,double> mapcontracttosalary = new map<id,double>();
     for(contract contract:[select id,salary__c  from contract where id in:ContractsID and recordType.developerName='Transfer']){
          mapcontracttosalary.put(contract.id,contract.salary__c);
     }
     for(players__c player: trigger.new){
          if(mapcontracttosalary.get(player.contract__c)!=null){
               player.salary__c  = mapcontracttosalary.get(player.contract__c);
          }
     }
}

Kindly mark as Best Answer 
Nuno.CarvalhoNuno.Carvalho
Hi Ahmad,
Now it gives his error... Compile Error: Method does not exist or incorrect signature: void add(Id) from the type Set<Contract> at line 4 column 25, never saw this one could you help? sorry.
Ahmad J. KoubeissyAhmad J. Koubeissy
trigger playerbeforeInsert on players__c(before insert){
     set<id> ContractsID = new set<id>();
     for(players__c player: trigger.new){
            ContractsID.add(player.contract__c);//where contract is the lookup field
     }
     map<id,double> mapcontracttosalary = new map<id,double>();
     for(contract contract:[select id,salary__c  from contract where id in:ContractsID and recordType.developerName='Transfer']){
          mapcontracttosalary.put(contract.id,contract.salary__c);
     }
     for(players__c player: trigger.new){
          if(mapcontracttosalary.get(player.contract__c)!=null){
               player.salary__c  = mapcontracttosalary.get(player.contract__c);
          }
     }
}

Kindly mark as Best Answer if this is fixed
Shruti SShruti S

Hi Nuno,

You can simply create a Formula field of return type Currency on the Player__c object which would capture the Salary__c value from the Contract object which is its parent object.

In the formula field, you can write the below code -

Contract__r.Salary__c
This method works if you are sure that the salary on the Player__c object never changes. The advantage of this method is, it does not require any code and if you change the salary on the Contract object, the salary value on the corresponding player object will also change automatically.