• Monika Baricza
  • NEWBIE
  • 25 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hi, 

I wrote a custom lightning opponent, which is finally working. I managed to add it to the Accounts page where the user should be able to see a list of commissions and the contacts who achieved them. I would like the user to see only the commissions that are related to that account. I added force:hasRecordId to the app and it seemed to be working until I realised that the app is only returning the contacts who have no accounts at all. I am a bit baffled by this so I appreciate any help. 
Below is my code: 
<aura:application extends="force:slds" implements="force:hasRecordId" >
  
    <c:commissionsByAccount/>
    
</aura:application>
The component:
<aura:component controller="commissionController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:attribute name="contacts" type="Commission__c[]" />
    <aura:attribute name="commissions" type="Commission__c[]"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
   
    <lightning:layout class="slds-page-header slds-page-header--object-home">
    	<lightning:layoutItem padding="around-small" size="4">
            <lightning:icon iconName="standard:scan_card" alternativeText="Commissions"/>
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-small">
            <div class="page-section page-header">
            	<h1 class="slds-text-heading--label">Commissions for this account</h1>
                <h1 class="slds-text-heading--medium">Commissions</h1>
            </div>
        </lightning:layoutItem>
    </lightning:layout>
    
    <lightning:layout>
    	<lightning:layoutItem padding="around-small" size="12">
	<table class="slds-table slds-table_cell-buffer slds-table_bordered">
  <thead>
    <tr class="slds-line-height_reset">
      <th aria-label="Date" aria-sort="descending" class="sdls-is-sortable" scope="col">
        <div class="slds-truncate" title="Date">Date</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Name">Name</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Amount">Amount</div>
      </th>
    </tr>
  </thead>
  <tbody>
      <aura:iteration items="{!v.contacts}" var="com">
    <tr class="slds-hint-parent">
      <th data-label="Date" scope="row">
        <div class="slds-truncate" title="Date">
          <a href="javascript:void(0);" tabindex="-1">{!com.Date__c}</a>
        </div>
      </th>
      <td data-label="Name">
        <div class="slds-truncate" title="Name">{!com.Contact_Name__r.Name}</div>
      </td>
      <td data-label="Amount">
        <div class="slds-truncate" title="Amount">{!com.Commission_Amount__c}</div>
      </td>
      </tr>
      </aura:iteration>
  </tbody>
</table>
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>

The Controller:

 
public class commissionController {
@AuraEnabled
	public static List<Commission__c> getCommissions(String AccountID) {
       return [SELECT Commission_Amount__c, Date__c, Commission__c.Contact_Name__r.Name FROM Commission__c WHERE Commission__c.Contact_Name__r.Account.Id =: AccountID ];
    	
    }
}

And the other controller: 
({
	doInit: function(component, event, helper) {
        var action = component.get("c.getCommissions");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var res=response.getReturnValue();
                component.set("v.contacts", response.getReturnValue());
             
                console.log("Success");
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },
})

Thank you very much​​​​​​​
Hi All, 

I am new to Salesforce and I am trying to develop an application where I created a custom object called Commissions which is related to the Contact object via Master-Detail relationship (Contact being the master). I created a custom checkbox in the Contact object which should be checked if the Contact has the most commissions within the Account where they belong to. I created a rollup field for this. 
My task is to create a trigger which automatically checks the custom checkbox if the contact is the contact who has the most commissions. I managed to create a working trigger however it only seems to execute after I add a commission for the second time, even though they have the biggest commission after the first addition already. I assume it has something to do with when the trigger is triggered but I can't figure out how to correct the code. Any help is appreciated. 
Here is my code: 
trigger UpdateContactOnCommissionUpdate on Commission__c ( after insert, after update, before insert, before update) {
    double maxCommission=0;
    Id ContactId;
    Id AccId;
    for(Commission__c comm:Trigger.new){
        ContactId=comm.Contact_Name__c;
    }
    
    System.debug('Contact id: '+ContactId);
  
    List<Contact> contacts=[SELECT AccountId FROM Contact WHERE Id=:ContactId];
    System.debug('Account ID: '+contacts.get(0).AccountId);
    AccId=contacts.get(0).AccountId;
   
    List<Contact> contactsToUpdate = new List<Contact>{};
     
    for(Contact c:[SELECT Id, Total_Commission__c, Primary__c FROM Contact WHERE AccountID=:AccId]){
        if(c.Total_Commission__c>maxCommission){
            maxCommission=c.Total_Commission__c;
            contactsToUpdate.add(c);
            }
    } 
    System.debug('Maximum commission'+ maxCommission);
    for(Contact c:contactsToUpdate){
        if(c.Total_Commission__c==maxCommission)
            c.Primary__c=true;
        else
            c.Primary__c=false;
    }
    if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
    
    
   
}

 
Hi, 

I wrote a custom lightning opponent, which is finally working. I managed to add it to the Accounts page where the user should be able to see a list of commissions and the contacts who achieved them. I would like the user to see only the commissions that are related to that account. I added force:hasRecordId to the app and it seemed to be working until I realised that the app is only returning the contacts who have no accounts at all. I am a bit baffled by this so I appreciate any help. 
Below is my code: 
<aura:application extends="force:slds" implements="force:hasRecordId" >
  
    <c:commissionsByAccount/>
    
</aura:application>
The component:
<aura:component controller="commissionController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:attribute name="contacts" type="Commission__c[]" />
    <aura:attribute name="commissions" type="Commission__c[]"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
   
    <lightning:layout class="slds-page-header slds-page-header--object-home">
    	<lightning:layoutItem padding="around-small" size="4">
            <lightning:icon iconName="standard:scan_card" alternativeText="Commissions"/>
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-small">
            <div class="page-section page-header">
            	<h1 class="slds-text-heading--label">Commissions for this account</h1>
                <h1 class="slds-text-heading--medium">Commissions</h1>
            </div>
        </lightning:layoutItem>
    </lightning:layout>
    
    <lightning:layout>
    	<lightning:layoutItem padding="around-small" size="12">
	<table class="slds-table slds-table_cell-buffer slds-table_bordered">
  <thead>
    <tr class="slds-line-height_reset">
      <th aria-label="Date" aria-sort="descending" class="sdls-is-sortable" scope="col">
        <div class="slds-truncate" title="Date">Date</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Name">Name</div>
      </th>
      <th class="" scope="col">
        <div class="slds-truncate" title="Amount">Amount</div>
      </th>
    </tr>
  </thead>
  <tbody>
      <aura:iteration items="{!v.contacts}" var="com">
    <tr class="slds-hint-parent">
      <th data-label="Date" scope="row">
        <div class="slds-truncate" title="Date">
          <a href="javascript:void(0);" tabindex="-1">{!com.Date__c}</a>
        </div>
      </th>
      <td data-label="Name">
        <div class="slds-truncate" title="Name">{!com.Contact_Name__r.Name}</div>
      </td>
      <td data-label="Amount">
        <div class="slds-truncate" title="Amount">{!com.Commission_Amount__c}</div>
      </td>
      </tr>
      </aura:iteration>
  </tbody>
</table>
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>

The Controller:

 
public class commissionController {
@AuraEnabled
	public static List<Commission__c> getCommissions(String AccountID) {
       return [SELECT Commission_Amount__c, Date__c, Commission__c.Contact_Name__r.Name FROM Commission__c WHERE Commission__c.Contact_Name__r.Account.Id =: AccountID ];
    	
    }
}

And the other controller: 
({
	doInit: function(component, event, helper) {
        var action = component.get("c.getCommissions");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var res=response.getReturnValue();
                component.set("v.contacts", response.getReturnValue());
             
                console.log("Success");
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },
})

Thank you very much​​​​​​​
Hi All, 

I am new to Salesforce and I am trying to develop an application where I created a custom object called Commissions which is related to the Contact object via Master-Detail relationship (Contact being the master). I created a custom checkbox in the Contact object which should be checked if the Contact has the most commissions within the Account where they belong to. I created a rollup field for this. 
My task is to create a trigger which automatically checks the custom checkbox if the contact is the contact who has the most commissions. I managed to create a working trigger however it only seems to execute after I add a commission for the second time, even though they have the biggest commission after the first addition already. I assume it has something to do with when the trigger is triggered but I can't figure out how to correct the code. Any help is appreciated. 
Here is my code: 
trigger UpdateContactOnCommissionUpdate on Commission__c ( after insert, after update, before insert, before update) {
    double maxCommission=0;
    Id ContactId;
    Id AccId;
    for(Commission__c comm:Trigger.new){
        ContactId=comm.Contact_Name__c;
    }
    
    System.debug('Contact id: '+ContactId);
  
    List<Contact> contacts=[SELECT AccountId FROM Contact WHERE Id=:ContactId];
    System.debug('Account ID: '+contacts.get(0).AccountId);
    AccId=contacts.get(0).AccountId;
   
    List<Contact> contactsToUpdate = new List<Contact>{};
     
    for(Contact c:[SELECT Id, Total_Commission__c, Primary__c FROM Contact WHERE AccountID=:AccId]){
        if(c.Total_Commission__c>maxCommission){
            maxCommission=c.Total_Commission__c;
            contactsToUpdate.add(c);
            }
    } 
    System.debug('Maximum commission'+ maxCommission);
    for(Contact c:contactsToUpdate){
        if(c.Total_Commission__c==maxCommission)
            c.Primary__c=true;
        else
            c.Primary__c=false;
    }
    if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
    
    
   
}