• Belen Ares Paredes 13
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi!
I am an admin, not a developer and I am trying to overcome the fact that I cannot access the Name of a team member declaratively. 

I found a trigger that does what I need but I am unable to write the test code for the deployment to pass. Can someone help, please??
So frustrating!

Thanks,

Belen
trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update)
{
	Set<Id> setTeamMemberID = new Set<Id>();
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		setTeamMemberID.add(otm.UserId);
	}

	Map<Id, User> usrMap = new Map<Id, User>([SELECT Id, Name, LastName FROM User WHERE Id IN :setTeamMemberId]);
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		User usr = usrMap.get(otm.UserId);

		otm.Team_Member_Name__c = usr.Name;
	}
}

 
Hi, 

I have to figure out why this code does not entirely work. 

The opportunity and the related contact roles are being created fine however, the Competitions object is not being created as a related list to the opportunity.

I have been debugging with alerts (I know its not ideal but i have no expertrise in JS!) and i can see that all is working as expected, however this line at the end of the codeis not successful.

result = sforce.connection.create(newCompetitions); 

I have been trying to debug with a SF debug log but I see nothing. Is there anyway I can see the error related to this line and see why it's not doing as expected?

Alternatively, can you spot if the code is incorrectly formulated?

Thanks so much!

Belen


{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var newOpportunity = new sforce.SObject("Opportunity");
newOpportunity.AccountId =  '{!Opportunity.AccountId}';
newOpportunity.LeadEventName__c = '{!JSENCODE(Opportunity.LeadEventName__c)}';
newOpportunity.Recurrence__c = '{!JSENCODE(TEXT(Opportunity.Recurrence__c))}';
newOpportunity.RecordTypeId = '{!Opportunity.RecordTypeId}';
newOpportunity.LeadEventType__c = '{!JSENCODE(TEXT(Opportunity.LeadEventType__c))}';
newOpportunity.EventIndustry__c = '{!JSENCODE(TEXT(Opportunity.EventIndustry__c))}';
newOpportunity.CloseDate = new Date();
newOpportunity.Name = '{!JSENCODE(Opportunity.Name)}';
newOpportunity.StageName = 'Future Opportunity';
newOpportunity.EventDescription_del__c = '{!Opportunity.EventDescription_del__c}'; 
newOpportunity.Industry_Subtype__c = '{!Opportunity.Industry_Subtype__c}'; 
newOpportunity.BESydneyTeam__c = '{!Opportunity.BESydneyTeam__c}'; 
newOpportunity.Delegate_Profile__c = '{!Opportunity.Delegate_Profile__c}'; 
newOpportunity.Past_Registration_Fees__c ='{!Opportunity.Past_Registration_Fees__c}'; 
var result = sforce.connection.create([newOpportunity]);

var newOpportunityId;
if (result[0].getBoolean("success")) {
   newOpportunityId = result[0].id;
}
else {
   alert('Unable to clone Opportunity.');
}

if (newOpportunityId) {
   //Clone Opportunity Contact Roles
   var opportunityContactRolesQuery = sforce.connection.query("" +
      "Select ContactId, Role, IsPrimary " +
      "From OpportunityContactRole " +
      "Where OpportunityId = '{!Opportunity.Id}'");

   var opportunityContactRoles = opportunityContactRolesQuery.getArray("records");
   var newOpportunityContactRoles = [];
   var newOpportunityContactRole;
   for (var counter = 0; counter < opportunityContactRoles.length; counter++) {
      newOpportunityContactRole = new sforce.SObject("OpportunityContactRole");
      newOpportunityContactRole.ContactId = opportunityContactRoles[counter].ContactId;
      newOpportunityContactRole.IsPrimary = opportunityContactRoles[counter].IsPrimary;
      newOpportunityContactRole.Role = opportunityContactRoles[counter].Role;
      newOpportunityContactRole.OpportunityId = newOpportunityId;
      newOpportunityContactRoles.push(newOpportunityContactRole);
   }
   if(newOpportunityContactRoles.length > 0) {
      result = sforce.connection.create(newOpportunityContactRoles);
   }

   //Clone Competitions
   var competitionQuery = sforce.connection.query("" +
      "Select City__c, DelegatesAttended__c, DestinationOffers__c, EventHeldYear__c, KeyLearnings__c, Strength_of_Competitor_Bid__c, Weakness_of_Competitor_Bid__c, WinningCity__c, RecordTypeId, RecordType.DeveloperName " +
      "From Competition_Event_History__c " +
      "Where Opportunity__c = '{!Opportunity.Id}'");
   
   var competitions = competitionQuery.getArray("records");
   var newCompetitions = [];
   var newCompetition;
   for (var counter = 0; counter < competitions.length; counter++) {
      if (competitions[counter].RecordType.DeveloperName == 'Historical_Destination') {
         newCompetition = new sforce.SObject("Competition_Event_History__c");
         newCompetition.City__c = competitions[counter].City__c;
         newCompetition.DelegatesAttended__c = competitions[counter].DelegatesAttended__c;
         newCompetition.DestinationOffers__c = competitions[counter].DestinationOffers__c;
         newCompetition.EventHeldYear__c = competitions[counter].EventHeldYear__c;
         newCompetition.KeyLearnings__c = competitions[counter].KeyLearnings__c;
         newCompetition.Strength_of_Competitor_Bid__c = competitions[counter].Strength_of_Competitor_Bid__c;
         newCompetition.Weakness_of_Competitor_Bid__c = competitions[counter].Weakness_of_Competitor_Bid__c;
         newCompetition.WinningCity__c = competitions[counter].WinningCity__c;
         newCompetition.RecordTypeId = competitions[counter].RecordTypeId;
         newCompetition.Opportunity__c = newOpportunityId;
         newCompetitions.push(newCompetition);
      }
   }
   if (newCompetitions.length > 0) {
      result = sforce.connection.create(newCompetitions);
   }
   window.location.href = '/' + newOpportunityId;
}

 
Hi, 

I have to figure out why this code does not entirely work. 

The opportunity and the related contact roles are being created fine however, the Competitions object is not being created as a related list to the opportunity.

I have been debugging with alerts (I know its not ideal but i have no expertrise in JS!) and i can see that all is working as expected, however this line at the end of the codeis not successful.

result = sforce.connection.create(newCompetitions); 

I have been trying to debug with a SF debug log but I see nothing. Is there anyway I can see the error related to this line and see why it's not doing as expected?

Alternatively, can you spot if the code is incorrectly formulated?

Thanks so much!

Belen


{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var newOpportunity = new sforce.SObject("Opportunity");
newOpportunity.AccountId =  '{!Opportunity.AccountId}';
newOpportunity.LeadEventName__c = '{!JSENCODE(Opportunity.LeadEventName__c)}';
newOpportunity.Recurrence__c = '{!JSENCODE(TEXT(Opportunity.Recurrence__c))}';
newOpportunity.RecordTypeId = '{!Opportunity.RecordTypeId}';
newOpportunity.LeadEventType__c = '{!JSENCODE(TEXT(Opportunity.LeadEventType__c))}';
newOpportunity.EventIndustry__c = '{!JSENCODE(TEXT(Opportunity.EventIndustry__c))}';
newOpportunity.CloseDate = new Date();
newOpportunity.Name = '{!JSENCODE(Opportunity.Name)}';
newOpportunity.StageName = 'Future Opportunity';
newOpportunity.EventDescription_del__c = '{!Opportunity.EventDescription_del__c}'; 
newOpportunity.Industry_Subtype__c = '{!Opportunity.Industry_Subtype__c}'; 
newOpportunity.BESydneyTeam__c = '{!Opportunity.BESydneyTeam__c}'; 
newOpportunity.Delegate_Profile__c = '{!Opportunity.Delegate_Profile__c}'; 
newOpportunity.Past_Registration_Fees__c ='{!Opportunity.Past_Registration_Fees__c}'; 
var result = sforce.connection.create([newOpportunity]);

var newOpportunityId;
if (result[0].getBoolean("success")) {
   newOpportunityId = result[0].id;
}
else {
   alert('Unable to clone Opportunity.');
}

if (newOpportunityId) {
   //Clone Opportunity Contact Roles
   var opportunityContactRolesQuery = sforce.connection.query("" +
      "Select ContactId, Role, IsPrimary " +
      "From OpportunityContactRole " +
      "Where OpportunityId = '{!Opportunity.Id}'");

   var opportunityContactRoles = opportunityContactRolesQuery.getArray("records");
   var newOpportunityContactRoles = [];
   var newOpportunityContactRole;
   for (var counter = 0; counter < opportunityContactRoles.length; counter++) {
      newOpportunityContactRole = new sforce.SObject("OpportunityContactRole");
      newOpportunityContactRole.ContactId = opportunityContactRoles[counter].ContactId;
      newOpportunityContactRole.IsPrimary = opportunityContactRoles[counter].IsPrimary;
      newOpportunityContactRole.Role = opportunityContactRoles[counter].Role;
      newOpportunityContactRole.OpportunityId = newOpportunityId;
      newOpportunityContactRoles.push(newOpportunityContactRole);
   }
   if(newOpportunityContactRoles.length > 0) {
      result = sforce.connection.create(newOpportunityContactRoles);
   }

   //Clone Competitions
   var competitionQuery = sforce.connection.query("" +
      "Select City__c, DelegatesAttended__c, DestinationOffers__c, EventHeldYear__c, KeyLearnings__c, Strength_of_Competitor_Bid__c, Weakness_of_Competitor_Bid__c, WinningCity__c, RecordTypeId, RecordType.DeveloperName " +
      "From Competition_Event_History__c " +
      "Where Opportunity__c = '{!Opportunity.Id}'");
   
   var competitions = competitionQuery.getArray("records");
   var newCompetitions = [];
   var newCompetition;
   for (var counter = 0; counter < competitions.length; counter++) {
      if (competitions[counter].RecordType.DeveloperName == 'Historical_Destination') {
         newCompetition = new sforce.SObject("Competition_Event_History__c");
         newCompetition.City__c = competitions[counter].City__c;
         newCompetition.DelegatesAttended__c = competitions[counter].DelegatesAttended__c;
         newCompetition.DestinationOffers__c = competitions[counter].DestinationOffers__c;
         newCompetition.EventHeldYear__c = competitions[counter].EventHeldYear__c;
         newCompetition.KeyLearnings__c = competitions[counter].KeyLearnings__c;
         newCompetition.Strength_of_Competitor_Bid__c = competitions[counter].Strength_of_Competitor_Bid__c;
         newCompetition.Weakness_of_Competitor_Bid__c = competitions[counter].Weakness_of_Competitor_Bid__c;
         newCompetition.WinningCity__c = competitions[counter].WinningCity__c;
         newCompetition.RecordTypeId = competitions[counter].RecordTypeId;
         newCompetition.Opportunity__c = newOpportunityId;
         newCompetitions.push(newCompetition);
      }
   }
   if (newCompetitions.length > 0) {
      result = sforce.connection.create(newCompetitions);
   }
   window.location.href = '/' + newOpportunityId;
}